diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bcdfeba..ecca61f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,15 +17,16 @@ jobs: - 7.2 - 7.3 - 7.4 - composer-args: [ "" ] - include: - - php: 8.0 - composer-args: --ignore-platform-reqs + - 8.0 + - 8.1 + - 8.2 + - 8.3 + - 8.4 fail-fast: false steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install PHP uses: shivammathur/setup-php@v2 @@ -33,14 +34,46 @@ jobs: php-version: ${{ matrix.php }} - name: Cache PHP dependencies - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: vendor key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer- - name: Install dependencies - run: composer install --prefer-dist --no-progress --no-suggest ${{ matrix.composer-args }} + run: composer install --prefer-dist --no-progress --no-suggest - name: Tests - run: composer test \ No newline at end of file + run: composer test + + rector: + name: Rector + runs-on: ubuntu-latest + + strategy: + matrix: + php: + - 8.4 + fail-fast: false + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Cache PHP dependencies + uses: actions/cache@v4 + with: + path: vendor + key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Tests + run: vendor/bin/rector process --dry-run diff --git a/composer.json b/composer.json index 9e96c8b..c19fb6e 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,9 @@ "phpunit/phpunit": "^8.0", "squizlabs/php_codesniffer": "^3.0", "oscarotero/php-cs-fixer-config": "^1.0", - "friendsofphp/php-cs-fixer": "^2.15" + "friendsofphp/php-cs-fixer": "^2.15", + "phpstan/phpstan": "^1|^2", + "rector/rector": "^1|^2" }, "autoload": { "psr-4": { @@ -41,8 +43,10 @@ "scripts": { "test": [ "phpunit", - "phpcs" + "phpcs", + "phpstan" ], - "cs-fix": "php-cs-fixer fix" + "cs-fix": "php-cs-fixer fix", + "rector": "rector process" } } diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..d255432 --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,5 @@ +parameters: + level: 5 + paths: + - src + - tests diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..f9db34a --- /dev/null +++ b/rector.php @@ -0,0 +1,18 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPhpSets() + ->withPreparedSets(typeDeclarations: true) + ->withDeadCodeLevel(2) + ->withCodeQualityLevel(10) + ->withCodingStyleLevel(0) +; diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index 1345bbb..a2377ec 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -11,7 +11,7 @@ class JsFunctionsScanner implements FunctionsScannerInterface protected $validFunctions; protected $parser; - public function __construct(array $validFunctions = null) + public function __construct(?array $validFunctions = null) { $this->validFunctions = $validFunctions; $this->parser('latest'); @@ -26,7 +26,7 @@ public function parser(string $version, array $options = ['comments' => true]): public function scan(string $code, string $filename): array { - list($version, $options) = $this->parser; + [$version, $options] = $this->parser; $ast = Peast::$version($code, $options)->parse(); diff --git a/src/JsNodeVisitor.php b/src/JsNodeVisitor.php index c4fb501..dfe6f98 100644 --- a/src/JsNodeVisitor.php +++ b/src/JsNodeVisitor.php @@ -3,9 +3,14 @@ namespace Gettext\Scanner; +use InvalidArgumentException; use Peast\Syntax\Node\CallExpression; use Peast\Syntax\Node\Comment; +use Peast\Syntax\Node\Identifier; +use Peast\Syntax\Node\Literal; +use Peast\Syntax\Node\MemberExpression; use Peast\Syntax\Node\Node; +use Peast\Syntax\Node\TemplateLiteral; class JsNodeVisitor { @@ -13,15 +18,15 @@ class JsNodeVisitor protected $filename; protected $functions = []; - public function __construct(string $filename, array $validFunctions = null) + public function __construct(string $filename, ?array $validFunctions = null) { $this->filename = $filename; $this->validFunctions = $validFunctions; } - public function __invoke(Node $node) + public function __invoke(Node $node): void { - if ($node->getType() === 'CallExpression') { + if ($node instanceof CallExpression) { $function = $this->createFunction($node); if ($function) { @@ -55,24 +60,20 @@ protected function createFunction(CallExpression $node): ?ParsedFunction static::addComments($function, $node->getCallee()); foreach ($node->getArguments() as $argument) { - switch ($argument->getType()) { - case 'Literal': - $function->addArgument($argument->getValue()); - static::addComments($function, $argument); - break; - case 'TemplateLiteral': - if ($argument->getExpressions()) { - $function->addArgument(); - break; - } - + if ($argument instanceof Literal) { + $function->addArgument($argument->getValue()); + static::addComments($function, $argument); + } elseif ($argument instanceof TemplateLiteral) { + if ($argument->getExpressions()) { + $function->addArgument(); + } else { $quasis = $argument->getQuasis(); $quasis = array_shift($quasis); $function->addArgument($quasis->getValue()); static::addComments($function, $argument); - break; - default: - $function->addArgument(); + } + } else { + $function->addArgument(); } } @@ -83,19 +84,15 @@ protected static function getFunctionName(CallExpression $node): ?string { $callee = $node->getCallee(); - switch ($callee->getType()) { - case 'Identifier': - return $callee->getName(); - case 'MemberExpression': - $property = $callee->getProperty(); - - if ($property->getType() === 'Identifier') { - return $property->getName(); - } - return null; - default: - return null; + if ($callee instanceof Identifier) { + return $callee->getName(); + } elseif ($callee instanceof MemberExpression) { + $property = $callee->getProperty(); + if ($property instanceof Identifier) { + return $property->getName(); + } } + return null; } protected static function addComments(ParsedFunction $function, Node $node): void @@ -109,7 +106,7 @@ protected static function getComment(Comment $comment): string { $text = $comment->getText(); - $lines = array_map(function ($line) { + $lines = array_map(function ($line): string { $line = ltrim($line, "#*/ \t"); $line = rtrim($line, "#*/ \t"); return trim($line); diff --git a/tests/JsFunctionsScannerTest.php b/tests/JsFunctionsScannerTest.php index 878e16c..94fab1c 100644 --- a/tests/JsFunctionsScannerTest.php +++ b/tests/JsFunctionsScannerTest.php @@ -7,7 +7,7 @@ class JsFunctionsScannerTest extends TestCase { - public function testJsFunctionsExtractor() + public function testJsFunctionsExtractor(): void { $scanner = new JsFunctionsScanner(); $file = __DIR__.'/assets/functions.js';