From 605babbc70de74fe31de626a03e50786b49bada7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 18 Nov 2026 12:58:57 +0000 Subject: [PATCH] Support PHP 7.2 and nikic/php-parser 4 `gettext/gettext` 5 supports PHP 7.2, but the scanner needed PHP 7.4 because of typed properties and nikic/php-parser 5. * Allow nikic/php-parser ^4.18 next to ^5 and create the parser and traverser in a way that works with both. * Handle the scalar node names of nikic/php-parser 4. * Skip duplicate comments. nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one. * Replace typed properties with docblocks. * Allow PHPUnit 8.5 and run the tests on PHP 7.2 and 7.3. --- .github/workflows/test.yml | 6 +++++ composer.json | 6 ++--- src/PhpFunctionsScanner.php | 25 ++++++++++++++++---- src/PhpNodeVisitor.php | 47 +++++++++++++++++++++++-------------- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index deecc00..c172421 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,8 @@ jobs: strategy: matrix: php: + - 7.2 + - 7.3 - 7.4 - 8.0 - 8.1 @@ -38,6 +40,10 @@ jobs: key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer- + - name: Remove tools that need a newer PHP version + if: matrix.php == '7.2' || matrix.php == '7.3' + run: composer remove --dev --no-update friendsofphp/php-cs-fixer oscarotero/php-cs-fixer-config + - name: Install dependencies run: composer install --prefer-dist --no-progress --no-suggest diff --git a/composer.json b/composer.json index 84684b4..cf3c5f4 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,12 @@ "issues": "https://github.com/php-gettext/PHP-Scanner/issues" }, "require": { - "php": ">=7.4", - "nikic/php-parser": "^5", + "php": ">=7.2", + "nikic/php-parser": "^4.18 || ^5", "gettext/gettext": "^5.5.0" }, "require-dev": { - "phpunit/phpunit": "^9", + "phpunit/phpunit": "^8.5 || ^9", "squizlabs/php_codesniffer": "^3.0", "oscarotero/php-cs-fixer-config": "^2", "friendsofphp/php-cs-fixer": "^3" diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index a3fd913..4a42a33 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -9,13 +9,29 @@ class PhpFunctionsScanner implements FunctionsScannerInterface { - protected Parser $parser; - protected ?array $validFunctions; + /** @var Parser */ + protected $parser; + + /** @var array|null */ + protected $validFunctions; public function __construct(?array $validFunctions = null, ?Parser $parser = null) { $this->validFunctions = $validFunctions; - $this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion(); + $this->parser = $parser ?: static::createParser(); + } + + protected static function createParser(): Parser + { + $factory = new ParserFactory(); + + // nikic/php-parser 5 + if (method_exists($factory, 'createForNewestSupportedVersion')) { + return $factory->createForNewestSupportedVersion(); + } + + // nikic/php-parser 4 + return $factory->create(ParserFactory::PREFER_PHP7); } public function scan(string $code, string $filename): array @@ -28,7 +44,8 @@ public function scan(string $code, string $filename): array $visitor = $this->createNodeVisitor($filename); - $traverser = new NodeTraverser($visitor); + $traverser = new NodeTraverser(); + $traverser->addVisitor($visitor); $traverser->traverse($ast); return $visitor->getFunctions(); diff --git a/src/PhpNodeVisitor.php b/src/PhpNodeVisitor.php index 8937b9d..f842eb5 100644 --- a/src/PhpNodeVisitor.php +++ b/src/PhpNodeVisitor.php @@ -14,12 +14,17 @@ class PhpNodeVisitor implements NodeVisitor { - protected ?array $validFunctions; - protected string $filename; - protected array $functions = []; + /** @var array|null */ + protected $validFunctions; - /** @var Comment[] */ - protected array $bufferComments = []; + /** @var string */ + protected $filename; + + /** @var ParsedFunction[] */ + protected $functions = []; + + /** @var Node[] */ + protected $bufferComments = []; public function __construct(string $filename, ?array $validFunctions = null) { @@ -86,18 +91,25 @@ protected function createFunction(Expr $node): ParsedFunction $node->getEndLine() ); - foreach ($node->getComments() as $comment) { - $function->addComment(static::getComment($comment)); - } + // nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one. + $seen = []; + $addComments = static function (Node $commentedNode) use ($function, &$seen): void { + foreach ($commentedNode->getComments() as $comment) { + $position = $comment->getStartFilePos(); - if ($this->bufferComments) { - foreach ($this->bufferComments as $bufferComment) { - if ($bufferComment->getStartLine() === $node->getStartLine()) { - foreach ($bufferComment->getComments() as $comment) { - $function->addComment(static::getComment($comment)); - } + if (!isset($seen[$position])) { + $seen[$position] = true; + $function->addComment(static::getComment($comment)); } } + }; + + $addComments($node); + + foreach ($this->bufferComments as $bufferComment) { + if ($bufferComment->getStartLine() === $node->getStartLine()) { + $addComments($bufferComment); + } } $this->bufferComments = []; @@ -105,9 +117,7 @@ protected function createFunction(Expr $node): ParsedFunction foreach ($node->args as $argument) { $value = $argument->value; - foreach ($argument->getComments() as $comment) { - $function->addComment(static::getComment($comment)); - } + $addComments($argument); $function->addArgument(static::getValue($value)); } @@ -151,6 +161,9 @@ protected static function getValue(Expr $value) case 'Scalar_String': case 'Scalar_Int': case 'Scalar_Float': + // nikic/php-parser 4 + case 'Scalar_LNumber': + case 'Scalar_DNumber': return $value->value; case 'Expr_BinaryOp_Concat': $values = [];