Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node->getSubNodes() #1033

Open
staabm opened this issue Oct 6, 2024 · 1 comment · May be fixed by #1035
Open

Add Node->getSubNodes() #1033

staabm opened this issue Oct 6, 2024 · 1 comment · May be fixed by #1035

Comments

@staabm
Copy link
Contributor

staabm commented Oct 6, 2024

the current Node->getSubNodeNames() api leads to consuming code with dynamic property fetches like:

			foreach ($node->getSubNodeNames() as $subNodeName) {
				if ($node instanceof Node\Expr\Closure && $subNodeName !== 'uses') {
					continue;
				}
				$subNode = $node->{$subNodeName};
				$variableNames = array_merge($variableNames, $this->getUsedVariables($scope, $subNode));
			}

In PHPStan-src alone this pattern repeats 8 times.

Its also visibile in the php-parser codebase 2 times:

$result = '';
$pos = $startPos;
foreach ($node->getSubNodeNames() as $subNodeName) {
$subNode = $node->$subNodeName;
$origSubNode = $origNode->$subNodeName;
if ((!$subNode instanceof Node && $subNode !== null)
|| (!$origSubNode instanceof Node && $origSubNode !== null)
) {

protected function traverseNode(Node $node): void {
foreach ($node->getSubNodeNames() as $name) {
$subNode = $node->$name;
if (\is_array($subNode)) {
$node->$name = $this->traverseArray($subNode);

this dynamic property fetches lead to badly static analysable code

as soon as a class contains a single dynamic property lookup, phpstan is no longer able to tell you whether one of the declared properties is unused (analog for methods).

what do you think about adding a new Node->getSubNodes() method, so the dynamic property lookup is only available in one place insider php-parser and caller code would be more static analysis friendly?

@staabm
Copy link
Contributor Author

staabm commented Oct 7, 2024

I have something prepared which can make the code static analysis friendly and does not require a new method. stay tuned.

@staabm staabm linked a pull request Oct 7, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant