Bugs, shortcomings, and other minor issues in the parser
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 18/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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> */throughPhpDocParser
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
- If unparenthesized nesting is allowed, it is allowed in both branches
- (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.
- Langage dominant
- PHP
- Étoiles
- 1.5k
- Forks
- 81
- Merge moyen
- 7 h 46 min
- PR mergées (30 j)
- 20
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de phpstan/phpdoc-parser
-
support for class-string-map<T of Foo>Peut-être pris @Debasishhh l’a pris il y a 9 jours. Ouverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
phpstan/phpdoc-parser#281 · 4 commentaires · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
phpstan/phpdoc-parser#271 · 1 commentaire · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
phpstan/phpdoc-parser#219 ·
Les mainteneurs répondent en général sous 1 jour
-
Extension for custom tagsOuverte
Difficulté 4/5 3-5 jours Accessibilité débutants 38/100
phpstan/phpdoc-parser#178 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 42/100
phpstan/phpdoc-parser#173 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de phpstan/phpdoc-parser
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 63/100
phpstan/phpstan-phpunit#361 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
HE-Arc/le-panier-du-coin#49 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
Les mainteneurs répondent en général sous 2 jours
-
15.0
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·