Bugs, shortcomings, and other minor issues in the parser

Offen
#343 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
18/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
php
Bereich
compilers

Rechercherichtung

This bundles about twelve separate parser issues, several of which are feature requests or need a maintainer decision on the intended grammar. Start by reproducing one item, such as the variadic-with-default case, through TypeParser::parse() and checking that the whole input is consumed. Ask the maintainers which items to split out before picking one, since several expected behaviours are still open choices.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects.

While running benchmarks, developing a fully-fledged, semantically independent type parser, and performing fuzz testing, I encountered several issues in the current PHPStan type parser.

For your convenience, I have listed them below, ranging from clear-cut bugs to minor issues and potential feature requests.

All PHPStan claims were verified against phpstan/phpdoc-parser 2.4.1 (PHP 8.1) in two modes:

  • raw: TypeParser::parse() followed by a check that the whole input is consumed
  • docblock: the same string as /** @return <type> */ through PhpDocParser

If necessary, I can split this into separate issues. Just let me know the relevant numbers, and I will move them into individual issues for your convenience.

1. Variadic combined with optional
callable(int ...=): void     // accepted
callable(int ...$a=): void   // accepted

Current behavior

Accepted. = marks a parameter as optional, but a variadic parameter cannot be optional

Expected

A ... together with = must be rejected

2. Nested conditional types: then branch versus else branch
(T is int ? A : B is C ? D : E)     // accepted, printed as (T is int ? A : (B is C ? D : E))
(T is int ? (A is C ? D : E) : B)   // accepted
(T is int ? A is C ? D : E : B)     // rejected: Unexpected token "is", expected ':'
(T is int ? A : B|C is D ? E : F)   // rejected: Unexpected token "is", expected ')'

Current behavior

An unparenthesized conditional type is allowed only in the else branch, the then branch needs parentheses.

Expected

  1. If unparenthesized nesting is allowed, it is allowed in both branches
  2. (prefer) Otherwise only the parenthesized form is allowed

The rule is currently asymmetric and undocumented

3. Line breaks inside a type

https://phpstan.org/r/43dd2349-937d-4307-8156-18e219294ff0

Rejected:

"int|\nstring"
"?\nint"
"callable(int $a)\n: void"
"callable(int $a) :\nvoid"
"(T is\nint ? A : B)"
"(T\nis\nint\n?\nA\n:\nB)"

But accepted:

"int\n|string"
"array<\n int,\n string\n>"
"array{\n a: int,\n b: string\n}"
"callable(\n int $a\n): void"

Current behavior

A line break works before | and inside <>, {}, (), but not after |, after ?, around the : of a callable, or inside a parenthesized conditional type. A tab is accepted in the same positions ("?\tint", "int\t|\tstring").

Expected

A line break is equivalent to a space in every position between the tokens of a type, so a long union or a conditional type can be wrapped at any operator.

4. Trailing comma in ...<K, V>
array{a: int, ...<K,>}     // rejected: Unexpected token ">", expected type
array{a: int, ...<K>,}     // accepted
Foo<int,>                  // accepted
callable<T,>(): void       // accepted

Current behavior

A trailing comma is rejected inside the arguments of the unsealed part and accepted in every other list.

Expected

A trailing comma is accepted there too.

5. Variance keywords: covariant as a name
Foo<covariant>                              // rejected
Foo<contravariant>                          // rejected
Foo<covariant covariant>                    // accepted
callable<covariant>(covariant): covariant   // accepted

Current behavior

The same word covariant is parsed differently in different positions.

Expected

One defined rule:

  • Either it is a reserved word everywhere
  • Or an ordinary name wherever no type follows it
6. Number with an underscore right after the dot
1._5   // accepted, printed as 1.5

Current behavior

Accepted.

Expected

Rejected (Syntax Error). It is a syntax error in PHP

7. Hyphen (-) and backslash (\\) in a class constant name
Foo::BAR-BAR
Foo::BAR\Baz

Current behavior

Accepted, the hyphen and the backslash become part of the constant name.

Expected

Rejected.

8. Odd template names
callable<T\X>(int): int
callable<\T>(int): int
callable<T-X>(int): int
callable<non-empty-string>(int): int
callable<int>(int): int

// AND

@template T\X of A = Default
@template non-empty-string of B = Default
...etc

Current behavior

Accepted, a template name may be anything, including a builtin or a pseudo type.

Expected

A template name is a single word without \ and -. A builtin name is either rejected or explicitly allowed.

9. Conditional type without parentheses

This is a good solution that requires developers to be explicit. However, there are some debatable situations.

// Error, parentheses are required.
Foo<T is int ? A : B>
array{a: T is int ? A : B}

And bugs (https://phpstan.org/r/853c0920-857a-4d8f-8ff0-384382af1dbf).

The type ends before is, the rest becomes the description:

T is int ? A : B            // Silently "truncates" the type as "T", which can lead to problems
callable(): T is X ? A : B  // Truncates to "callable(): T"
C|T is int ? A : B          // Truncates to "C|T"
?T is int ? A : B           // Truncates to "?T"

The results are T, callable(): T, (C | T) and ?T

10. Space or line break before [, {, <, (
T [K]
array {a: int}
"array\n{a: int}"
"Foo\n<int>"
"callable\n(int): void"

PHPStan truncates to T, array, array, Foo, callable. The rest becomes the description.

BUT a space before < is accepted (array <int>, Foo <T> (T): T), so the rule differs between brackets.

11. Class constant on a variable

(?) Feature request

$this::BAR
$this::class
$a::BAR

Current behavior

Syntax error

12. Space after the sign of a number
- 1
- 42
- 1.5

Current behavior

Syntax error

Expected

In PHP - 42.0 is a valid expression.

Vorherrschende Sprache
PHP
Sterne
1.5k
Forks
81
Ø Merge
7 Std. 46 Min.
Gemergte PRs (30 T.)
20

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus phpstan/phpdoc-parser

Alle Issues in phpstan/phpdoc-parser

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.