Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Upgrade nikic/php-parser to v5+ #63

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

Open
BrianHenryIE wants to merge 8 commits into voku:master
base: master
Choose a base branch
Loading
from BrianHenryIE:upgrade-phpparser-5
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ composer.lock
# php (phpunit)
build/logs/
.phpunit.result.cache
/.phpunit.result/

# phpcs fixer
.php_cs.cache
Expand Down
13 changes: 12 additions & 1 deletion composer.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"phpdocumentor/reflection-common": "~2.2",
"phpstan/phpdoc-parser": "~1.23",
"voku/simple-cache": "~4.1",
"nikic/php-parser": "~4.16"
"nikic/php-parser": "^4.18 || ^5"
},
"require-dev": {
"phpunit/phpunit": "~6.0 || ~7.0 || ~9.0"
Expand All @@ -38,5 +38,16 @@
"psr-4": {
"voku\\tests\\": "tests/"
}
},
"scripts": {
"test-coverage": [
"Composer\\Config::disableProcessTimeout",
"if [ \"$XDEBUG_MODE\" != \"coverage\" ]; then echo \"Run with 'XDEBUG_MODE=coverage composer test-coverage'\"; exit 1; fi;",
"phpunit --coverage-text --coverage-clover .phpunit.result/unitclover.xml --coverage-php .phpunit.result/unitphp.cov --coverage-html .phpunit.result/html -d memory_limit=-1 --order-by=random",
"# Run 'open ./.phpunit.result/html/index.html' to view report."
]
},
"scripts-descriptions": {
"test-coverage": "Run PHPUnit tests with coverage. Use 'XDEBUG_MODE=coverage composer test-coverage' to run, 'open ./.phpunit.result/html/index.html' to view."
}
}
9 changes: 8 additions & 1 deletion src/voku/SimplePhpParser/Model/BasePHPElement.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ protected static function getFQN($node): string

protected function prepareNode(Node $node): void
{
$this->line = $node->getLine();
$this->line = method_exists($node, 'getStartLine')
? $node->getStartLine()
/**
* Deprecated in PHP-Parser v5
*
* @see https://github.com/nikic/PHP-Parser/blob/master/UPGRADE-5.0.md#miscellaneous-changes
*/
: $node->getLine();
}
}
23 changes: 21 additions & 2 deletions src/voku/SimplePhpParser/Parsers/Helper/Utils.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public static function getPhpParserValueFromNode(
&&
$node->value->name
) {
$value = implode('\\', $node->value->name->getParts()) ?: $node->value->name->name;
$value = method_exists($node->value->name,'getParts')
? implode('\\', $node->value->name->getParts())
: $node->value->name->name;
Comment on lines +115 to +117
Copy link

@gemini-code-assist gemini-code-assist bot Jun 1, 2025

Choose a reason for hiding this comment

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

high

This change correctly adapts for php-parser v5 by checking for getParts(). However, the fallback : $node->value->name->name for the v5 path might not be robust for all cases, especially for fully qualified names.

  1. PHP-Parser v5 Name Handling: For php-parser v5, PhpParser\Node\Name objects (including FullyQualified, Relative, etc.) provide a toString() method for reliable string conversion. Using $node->value->name->name can be problematic because:

    • For fully qualified names (e.g., new \My\Namespace\MyClass()), the name property might not exist on the FullyQualified object, or it might only contain the last part of the name, not the full FQN.
    • $node->value->name->toString() is the recommended way to get the complete string representation of any Name node in v5.
  2. Original Elvis Operator: It appears the original Elvis operator ?: $node->value->name->name (which was part of implode(...) ?: $node->value->name->name) has been removed from the v4 path in this PR (the true branch of the ternary). If implode('\\', $node->value->name->getParts()) results in an empty string, the behavior for the v4 path will now differ from the original code. Was this change in behavior for the v4 path intentional?

For the v5 path, could we use $node->value->name->toString() for more reliable name resolution? This would align better with the php-parser v5 API.

 $value = method_exists($node->value->name,'getParts')
 ? implode('\\', $node->value->name->getParts()) // v4 path (Note: original Elvis operator was removed in this PR)
 : $node->value->name->toString(); // v5 path: use toString() for robust name resolution

BrianHenryIE reacted with eyes emoji
return $value === 'null' ? null : $value;
}
}
Expand Down Expand Up @@ -468,7 +470,7 @@ public static function getCpuCores(): int
}

/** @noinspection PhpUsageOfSilenceOperatorInspection */
$ret = @\shell_exec('nproc');
$ret = @\shell_exec('nproc 2>&1');
if (\is_string($ret)) {
$ret = \trim($ret);
/** @noinspection PhpAssignmentInConditionInspection */
Expand All @@ -495,6 +497,23 @@ public static function getCpuCores(): int
}
}

/**
* macOS (FreeBSD)
*/
$ret = @\shell_exec('sysctl -n hw.ncpu');
if (\is_string($ret)) {
$ret = \trim($ret);
/** @noinspection PhpAssignmentInConditionInspection */
if ($ret && ($tmp = \filter_var($ret, \FILTER_VALIDATE_INT)) !== false) {
$return = (int)round($tmp / 2);
if ($return > 1) {
return $return;
}

return 1;
}
}

return 1;
}

Expand Down
16 changes: 1 addition & 15 deletions src/voku/SimplePhpParser/Parsers/PhpCodeParser.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace voku\SimplePhpParser\Parsers;

use FilesystemIterator;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
Expand Down Expand Up @@ -200,20 +199,7 @@ public static function process(
ParserContainer $parserContainer,
ASTVisitor $visitor
) {
$parser = (new ParserFactory())->create(
ParserFactory::PREFER_PHP7,
new Emulative(
[
'usedAttributes' => [
'comments',
'startLine',
'endLine',
'startTokenPos',
'endTokenPos',
],
]
)
);
$parser = (new ParserFactory())->createForNewestSupportedVersion();

$errorHandler = new ParserErrorHandler();

Expand Down

AltStyle によって変換されたページ (->オリジナル) /