Skip to content
Merged
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
49 changes: 41 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,63 @@ 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
with:
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
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
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
5 changes: 5 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 5
paths:
- src
- tests
18 changes: 18 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets()
->withPreparedSets(typeDeclarations: true)
->withDeadCodeLevel(2)
->withCodeQualityLevel(10)
->withCodingStyleLevel(0)
;
4 changes: 2 additions & 2 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();

Expand Down
57 changes: 27 additions & 30 deletions src/JsNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@

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
{
protected $validFunctions;
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) {
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/JsFunctionsScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class JsFunctionsScannerTest extends TestCase
{
public function testJsFunctionsExtractor()
public function testJsFunctionsExtractor(): void
{
$scanner = new JsFunctionsScanner();
$file = __DIR__.'/assets/functions.js';
Expand Down