Skip to content

Add php-parser v5 compatibility (^4.18 || ^5)#77

Merged
voku merged 3 commits intomasterfrom
copilot/adapt-php-parser-compatibility
Apr 9, 2026
Merged

Add php-parser v5 compatibility (^4.18 || ^5)#77
voku merged 3 commits intomasterfrom
copilot/adapt-php-parser-compatibility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

  • Utils.php – replace method_exists+wrong ->name fallback with ->toString() in ClassConstFetch path; use ->getParts() directly in ConstFetch path (works in both v4 and v5)
  • BasePHPElement.php – replace implode('\\', $x->getParts()) with ->toString(); remove var_dump; guard getFQN fallback: $node->name instanceof Name ? $node->name->toString() : (string) $node->name
  • PHPClass.php – replace getParts() with ->toString() (×2)
  • PHPInterface.php – replace getParts() with ->toString()
  • PHPEnum.php – replace getParts() with ->toString()
  • PHPConst.php – replace getParts() with ->toString()
  • ASTVisitor.php – use ->getParts()[0] (works in both v4 and v5)
  • All 38 tests pass with php-parser v5.7.0

Copilot AI assigned Copilot and voku Apr 9, 2026
@voku voku marked this pull request as ready for review April 9, 2026 22:23
@voku voku requested a review from Copilot April 9, 2026 22:25
@voku
Copy link
Copy Markdown
Owner

voku commented Apr 9, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for nikic/php-parser v5 by updating the composer constraints and implementing compatibility checks for deprecated or removed methods like getLine() and getParts(). While the transition to createForNewestSupportedVersion() and the line number handling are correct, there are critical issues in the fallback logic for Name nodes. Specifically, the code attempts to access the 'name' property which was removed in v5; these instances should be updated to use the 'parts' property to ensure compatibility across both versions.

Comment on lines +117 to +119
$value = method_exists($node->value->name, 'getParts')
? implode('\\', $node->value->name->getParts())
: $node->value->name->name;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The fallback to $node->value->name->name for php-parser v5 is incorrect. The name property on PhpParser\Node\Name was removed in v5. For compatibility, you should use the public parts property instead.

                        $value = implode('\', method_exists($node->value->name, 'getParts') ? $node->value->name->getParts() : $node->value->name->parts);

Comment on lines +172 to +174
$parts = method_exists($node->name, 'getParts')
? $node->name->getParts()
: [$node->name->name];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The fallback to [$node->name->name] for php-parser v5 is incorrect. The name property on PhpParser\Node\Name was removed in v5. You should use the public parts property instead, which is an array of name parts, just like the return value of getParts().

            $parts = method_exists($node->name, 'getParts') ? $node->name->getParts() : $node->name->parts;

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make the library compatible with both nikic/php-parser v4.18+ and v5 by widening the Composer constraint and updating a few call sites that were removed in v5.

Changes:

  • Widen nikic/php-parser requirement to ^4.18 || ^5.
  • Switch parser instantiation to ParserFactory::createForNewestSupportedVersion().
  • Add v5-safe fallbacks for Node::getLine() and Name::getParts() in a couple of locations.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
composer.json Widens nikic/php-parser constraint to allow v5.
src/voku/SimplePhpParser/Parsers/PhpCodeParser.php Uses createForNewestSupportedVersion() to avoid removed factory APIs in v5.
src/voku/SimplePhpParser/Model/BasePHPElement.php Avoids Node::getLine() by preferring getStartLine().
src/voku/SimplePhpParser/Parsers/Helper/Utils.php Guards Name::getParts() for v5 compatibility in getPhpParserValueFromNode().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread composer.json
"phpstan/phpdoc-parser": "~1.23",
"voku/simple-cache": "~5.0",
"nikic/php-parser": "~4.18"
"nikic/php-parser": "^4.18 || ^5"
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nikic/php-parser is now allowed at ^5, but the codebase still calls APIs removed in v5 (notably PhpParser\Node\Name::getParts()). I found remaining call sites in src/voku/SimplePhpParser/Model/BasePHPElement.php, Model/PHPClass.php, Model/PHPInterface.php, Model/PHPEnum.php, Model/PHPConst.php, and Parsers/Visitors/ASTVisitor.php. With php-parser v5 installed these will fatal at runtime, so the constraint change should be paired with updating those usages (e.g., prefer Name::toString() or a small compat helper that uses getParts() when available and falls back to ->name/toString()).

Suggested change
"nikic/php-parser": "^4.18 || ^5"
"nikic/php-parser": "^4.18"

Copilot uses AI. Check for mistakes.
Comment on lines +100 to +102
$this->line = method_exists($node, 'getStartLine')
? $node->getStartLine()
: $node->getLine(); // @phpstan-ignore-line getLine() was removed in PHP-Parser v5
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file still uses Name::getParts() (e.g., in getConstantFQN() and getFQN()), which was removed in php-parser v5. Even though prepareNode() now avoids Node::getLine(), these remaining getParts() calls will still cause fatal errors under v5; please update them to use ->toString() or a compat wrapper (similar to the approach taken in Utils::getPhpParserValueFromNode()).

Copilot uses AI. Check for mistakes.
Comment on lines +100 to +102
$this->line = method_exists($node, 'getStartLine')
? $node->getStartLine()
: $node->getLine(); // @phpstan-ignore-line getLine() was removed in PHP-Parser v5
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getFQN() currently contains a var_dump($node->name); debug statement. This will emit output during normal library usage (breaking CLI output, JSON responses, etc.) and should be removed or replaced with a proper logger behind an explicit debug flag.

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 9, 2026

Copilot AI requested a review from voku April 9, 2026 22:36
Copilot finished work on behalf of voku April 9, 2026 22:36
@voku
Copy link
Copy Markdown
Owner

voku commented Apr 9, 2026

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot apply changes based on the comments in this thread

All the changes from that review thread were already applied in commits 836cd52 and 895f1be:

  • BasePHPElement.php: replaced all implode('\\', $x->getParts()) with ->toString(), removed var_dump, fixed the getFQN() fallback to $node->name instanceof Name ? $node->name->toString() : (string) $node->name
  • PHPClass.php, PHPInterface.php, PHPEnum.php, PHPConst.php: all getParts() patterns replaced with ->toString()
  • Utils.php: ClassConstFetch path now uses ->toString() directly; two remaining getParts() calls are kept as-is since getParts() was not removed in v5

All 38 tests pass with php-parser v5.7.0.

Copilot finished work on behalf of voku April 9, 2026 23:05
@voku voku merged commit cc07ae5 into master Apr 9, 2026
13 of 14 checks passed
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 this pull request may close these issues.

3 participants