phpdoc-parser icon indicating copy to clipboard operation
phpdoc-parser copied to clipboard

Added ability to parse object shapes.

Open mad-briller opened this issue 1 year ago • 3 comments

partially fixes https://github.com/phpstan/phpstan/issues/2923

mad-briller avatar Jul 20 '22 19:07 mad-briller

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.

JanTvrdik avatar Jul 20 '22 22:07 JanTvrdik

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 :)

ondrejmirtes avatar Jul 21 '22 06:07 ondrejmirtes

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

mad-briller avatar Jul 21 '22 08:07 mad-briller

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?

dimitriskr avatar Dec 12 '22 17:12 dimitriskr

superceded by https://github.com/phpstan/phpdoc-parser/releases/tag/1.18.0

mad-briller avatar Apr 06 '23 08:04 mad-briller

Hey, it'd be great if you could update the grammar, I totally forgot about that!

ondrejmirtes avatar Apr 06 '23 11:04 ondrejmirtes