SubNodeNames are known at static analysis time
this PR turns the SubNodeNames into constant arrays and corresponding phpdocs, which makes them available to PHPStan at static analysis time.
this information is useful to better cover codebases which utilize PHP-Parser and in need of dynamic property lookups like
foreach ($node->getSubNodeNames() as $subNodeName) {
$subNode = $node->{$subNodeName};
// ...
}
when the type of $node is known.
closes https://github.com/nikic/PHP-Parser/issues/1033
I can kind of see how this could be useful in theory, but it's not really clear to me where we'd see a benefit in practice. Can you share an example where e.g. this gets rid of a baseline suppression in phpstan-src or similar?
see e.g. https://phpstan.org/r/9041358f-043d-4171-9780-f9d9b4b3c534
before this PR here, the unused property cannot be detected.
after this PR we get
- as initially suggested, the unused property now gets detected
- the error on line 26 is a unrelated PHPStan bug
➜ phpstan-src git:(2.0.x) ✗ php bin/phpstan analyze --debug test.php
Note: Using configuration file /Users/staabm/workspace/phpstan-src/phpstan.neon.dist.
/Users/staabm/workspace/phpstan-src/test.php
------ ----------------------------------------------------------------------------------------------------
Line test.php
------ ----------------------------------------------------------------------------------------------------
11 Property Processor::$unusedProperty is never read, only written.
🪪 property.onlyWritten
💡 See: https://phpstan.org/developing-extensions/always-read-written-properties
26 Access to an undefined property PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall::$var.
🪪 property.notFound
💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
------ ----------------------------------------------------------------------------------------------------
this gets rid of a baseline suppression in phpstan-src or similar?
I can't give you such a example, as because of the dynamic use errors are not detected which would be otherwise. (it does not fix a false-positive, but fixes a false-negative)
hmm.. I tried this PR on the whole phpstan-src codebase and it somehow has strange side-effects. I have another look