phpdoc-parser
phpdoc-parser copied to clipboard
Added ability to parse object shapes.
partially fixes https://github.com/phpstan/phpstan/issues/2923
I don't think it makes any sense to allow optinal keys for object shapes (because object tuples are nonsense), so you likely cannot directly reuse array shape parser and/or grammar.
Edit: it also probably does not make sense to allow constant strings / constant integers as object shape keys. probably only identifiers make sense.
Hi, I appreciate this, but before merging this, I'd like to see some work in progress on phpstan-src side too. To make sure there isn't a 6 months (or an infinite) gap between the progress in phpdoc-parser and phpstan-src :) So please try to implement ObjectShapeType
in phpstan-src and change the phpdoc-parser dependency in composer.json to point at your repo and branch.
The ObjectShapeType
we'll have to implement is going to be a combination of ObjectWithoutClassType and ConstantArrayType (https://apiref.phpstan.org/1.8.x/PHPStan.Type.ObjectWithoutClassType.html, https://apiref.phpstan.org/1.8.x/PHPStan.Type.Constant.ConstantArrayType.html).
The hardest part about adding a new type is that it has to play well with all the current types. It has to correctly implement all the Type methods, but especially Type::accepts and Type::isSuperTypeOf().
Type::accepts()
can have some basic tests in a rule that checks type acceptance, like CallMethodsRuleTest. And of course you can add ObjectShapeTypeTest::testAccepts() to comfortably test a lot of combinations.
Type::isSuperTypeOf()
is about this https://phpstan.org/developing-extensions/type-system#querying-a-specific-type and is best tested in TypeCombinatorTest::dataUnion() and TypeCombinatorTest::dataIntersect(). We'll have to answer some difficult questions like which type is the supertype of which another type :)
BTW: About a different issue - implementing the "generic callable" syntax here and in phpstan-src is definitely an easier task so feel free to start with it :)
thank you for the feedback ondrej.
i started with this as it seemed like it would be the easier thing to add to phpdoc-parser to get more familiar with it, to then allow me to do the generic callable syntax! but i guess i was wrong :D
The error of the check is irrelevant to the work of the PR. It is about allowing composer plugin to execute Is it possible to re-run the checks?
superceded by https://github.com/phpstan/phpdoc-parser/releases/tag/1.18.0
Hey, it'd be great if you could update the grammar, I totally forgot about that!