diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 87eb179..13535eb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,11 +14,11 @@ jobs: strategy: matrix: php: - - 7.2 - - 7.3 - 7.4 - 8.0 - 8.1 + - 8.2 + - 8.3 fail-fast: false steps: @@ -41,4 +41,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-suggest - name: Tests - run: composer test \ No newline at end of file + run: composer test diff --git a/composer.json b/composer.json index 8b73a0f..84684b4 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,15 @@ "issues": "https://github.com/php-gettext/PHP-Scanner/issues" }, "require": { - "php": ">=7.2", - "nikic/php-parser": "^4.2", + "php": ">=7.4", + "nikic/php-parser": "^5", "gettext/gettext": "^5.5.0" }, "require-dev": { - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "^3.0", - "oscarotero/php-cs-fixer-config": "^1.0", - "friendsofphp/php-cs-fixer": "^2.15" + "oscarotero/php-cs-fixer-config": "^2", + "friendsofphp/php-cs-fixer": "^3" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 171945a..82af3e5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,13 @@ - - - - ./tests/ - - - - - src - - + + + + + src + + + + + ./tests/ + + diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index 108dfa7..9ffbb04 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -4,19 +4,18 @@ namespace Gettext\Scanner; use PhpParser\NodeTraverser; -use PhpParser\NodeVisitor; use PhpParser\Parser; use PhpParser\ParserFactory; class PhpFunctionsScanner implements FunctionsScannerInterface { - protected $parser; - protected $validFunctions; + protected Parser $parser; + protected ?array $validFunctions; public function __construct(array $validFunctions = null, Parser $parser = null) { $this->validFunctions = $validFunctions; - $this->parser = $parser ?: (new ParserFactory())->create(ParserFactory::PREFER_PHP7); + $this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion(); } public function scan(string $code, string $filename): array @@ -27,15 +26,15 @@ public function scan(string $code, string $filename): array return []; } - $traverser = new NodeTraverser(); $visitor = $this->createNodeVisitor($filename); - $traverser->addVisitor($visitor); + + $traverser = new NodeTraverser($visitor); $traverser->traverse($ast); return $visitor->getFunctions(); } - protected function createNodeVisitor(string $filename): NodeVisitor + protected function createNodeVisitor(string $filename): PhpNodeVisitor { return new PhpNodeVisitor($filename, $this->validFunctions); } diff --git a/src/PhpNodeVisitor.php b/src/PhpNodeVisitor.php index 20fe8a9..a9621f4 100644 --- a/src/PhpNodeVisitor.php +++ b/src/PhpNodeVisitor.php @@ -7,16 +7,19 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\NodeVisitor; class PhpNodeVisitor implements NodeVisitor { - protected $validFunctions; - protected $filename; - protected $functions = []; - protected $bufferComments; + protected ?array $validFunctions; + protected string $filename; + protected array $functions = []; + + /** @var Comment[] */ + protected array $bufferComments = []; public function __construct(string $filename, array $validFunctions = null) { @@ -40,15 +43,17 @@ public function enterNode(Node $node) if ($name && ($this->validFunctions === null || in_array($name, $this->validFunctions))) { $this->functions[] = $this->createFunction($node); } elseif ($node->getComments()) { - $this->bufferComments = $node; + $this->bufferComments[] = $node; } - return null; + break; + + case 'Stmt_Expression': case 'Stmt_Echo': case 'Stmt_Return': case 'Expr_Print': case 'Expr_Assign': - $this->bufferComments = $node; - return null; + $this->bufferComments[] = $node; + break; } return null; @@ -85,13 +90,17 @@ protected function createFunction(Expr $node): ParsedFunction $function->addComment(static::getComment($comment)); } - if ($this->bufferComments && $this->bufferComments->getStartLine() === $node->getStartLine()) { - foreach ($this->bufferComments->getComments() as $comment) { - $function->addComment(static::getComment($comment)); + if ($this->bufferComments) { + foreach ($this->bufferComments as $bufferComment) { + if ($bufferComment->getStartLine() === $node->getStartLine()) { + foreach ($bufferComment->getComments() as $comment) { + $function->addComment(static::getComment($comment)); + } + } } } - $this->bufferComments = null; + $this->bufferComments = []; foreach ($node->args as $argument) { $value = $argument->value; @@ -140,8 +149,8 @@ protected static function getValue(Expr $value) switch ($type) { case 'Scalar_String': - case 'Scalar_LNumber': - case 'Scalar_DNumber': + case 'Scalar_Int': + case 'Scalar_Float': return $value->value; case 'Expr_BinaryOp_Concat': $values = []; diff --git a/src/PhpScanner.php b/src/PhpScanner.php index 97fb32d..a63cd22 100644 --- a/src/PhpScanner.php +++ b/src/PhpScanner.php @@ -4,7 +4,6 @@ namespace Gettext\Scanner; use Gettext\Translation; -use Gettext\Translations; /** * Class to scan PHP files and get gettext translations diff --git a/tests/PhpFunctionsScannerTest.php b/tests/PhpFunctionsScannerTest.php index f107f03..ed4463d 100644 --- a/tests/PhpFunctionsScannerTest.php +++ b/tests/PhpFunctionsScannerTest.php @@ -188,40 +188,4 @@ public function testPhpFunctionsExtractor() $this->assertSame($file, $function->getFilename()); $this->assertCount(0, $function->getComments()); } - - public function _testPhpFunctionsScannerWithDisabledComments() - { - $scanner = new PhpFunctionsScanner(); - $scanner->includeComments(false); - $file = __DIR__.'/assets/functions.php'; - $code = file_get_contents($file); - $functions = $scanner->scan($code, $file); - - $this->assertCount(11, $functions); - - foreach ($functions as $function) { - $this->assertCount(0, $function->getComments()); - } - } - - public function _testPhpFunctionsScannerWithPrefixedComments() - { - $scanner = new PhpFunctionsScanner(); - $scanner->includeComments(['ALLOW:']); - $file = __DIR__.'/assets/functions.php'; - $code = file_get_contents($file); - $functions = $scanner->scan($code, $file); - - $this->assertCount(11, $functions); - - //fn12 - $function = $functions[10]; - $this->assertCount(1, $function->getComments()); - - $comments = $function->getComments(); - $comment = $comments[0]; - $this->assertSame(23, $comment->getLine()); - $this->assertSame(23, $comment->getLastLine()); - $this->assertSame('ALLOW: Related comment 3', $comment->getComment()); - } } diff --git a/tests/PhpScannerTest.php b/tests/PhpScannerTest.php index 5742003..d00ed7a 100644 --- a/tests/PhpScannerTest.php +++ b/tests/PhpScannerTest.php @@ -23,7 +23,12 @@ public function testPhpCodeScanner() $scanner->scanFile($file); - list('domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3) = $scanner->getTranslations(); + /** + * @var Translations $domain1 + * @var Translations $domain2 + * @var Translations $domain3 + */ + ['domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3] = $scanner->getTranslations(); $this->assertCount(6, $domain1); $this->assertCount(4, $domain2);