Skip to content

Commit b7e3292

Browse files
authored
Merge pull request #6 from EmTeedee/php8-4-compatibility
PHP 8.4 compatibility
2 parents 371bddc + b377d82 commit b7e3292

File tree

7 files changed

+101
-44
lines changed

7 files changed

+101
-44
lines changed

.github/workflows/test.yml

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,63 @@ jobs:
1717
- 7.2
1818
- 7.3
1919
- 7.4
20-
composer-args: [ "" ]
21-
include:
22-
- php: 8.0
23-
composer-args: --ignore-platform-reqs
20+
- 8.0
21+
- 8.1
22+
- 8.2
23+
- 8.3
24+
- 8.4
2425
fail-fast: false
2526

2627
steps:
2728
- name: Checkout
28-
uses: actions/checkout@v2
29+
uses: actions/checkout@v4
2930

3031
- name: Install PHP
3132
uses: shivammathur/setup-php@v2
3233
with:
3334
php-version: ${{ matrix.php }}
3435

3536
- name: Cache PHP dependencies
36-
uses: actions/cache@v2
37+
uses: actions/cache@v4
3738
with:
3839
path: vendor
3940
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
4041
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-
4142

4243
- name: Install dependencies
43-
run: composer install --prefer-dist --no-progress --no-suggest ${{ matrix.composer-args }}
44+
run: composer install --prefer-dist --no-progress --no-suggest
4445

4546
- name: Tests
46-
run: composer test
47+
run: composer test
48+
49+
rector:
50+
name: Rector
51+
runs-on: ubuntu-latest
52+
53+
strategy:
54+
matrix:
55+
php:
56+
- 8.4
57+
fail-fast: false
58+
59+
steps:
60+
- name: Checkout
61+
uses: actions/checkout@v4
62+
63+
- name: Install PHP
64+
uses: shivammathur/setup-php@v2
65+
with:
66+
php-version: ${{ matrix.php }}
67+
68+
- name: Cache PHP dependencies
69+
uses: actions/cache@v4
70+
with:
71+
path: vendor
72+
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
73+
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-
74+
75+
- name: Install dependencies
76+
run: composer install --prefer-dist --no-progress --no-suggest
77+
78+
- name: Tests
79+
run: vendor/bin/rector process --dry-run

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"phpunit/phpunit": "^8.0",
2727
"squizlabs/php_codesniffer": "^3.0",
2828
"oscarotero/php-cs-fixer-config": "^1.0",
29-
"friendsofphp/php-cs-fixer": "^2.15"
29+
"friendsofphp/php-cs-fixer": "^2.15",
30+
"phpstan/phpstan": "^1|^2",
31+
"rector/rector": "^1|^2"
3032
},
3133
"autoload": {
3234
"psr-4": {
@@ -41,8 +43,10 @@
4143
"scripts": {
4244
"test": [
4345
"phpunit",
44-
"phpcs"
46+
"phpcs",
47+
"phpstan"
4548
],
46-
"cs-fix": "php-cs-fixer fix"
49+
"cs-fix": "php-cs-fixer fix",
50+
"rector": "rector process"
4751
}
4852
}

phpstan.dist.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
- tests

rector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\SetList;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
])
13+
->withPhpSets()
14+
->withPreparedSets(typeDeclarations: true)
15+
->withDeadCodeLevel(2)
16+
->withCodeQualityLevel(10)
17+
->withCodingStyleLevel(0)
18+
;

src/JsFunctionsScanner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class JsFunctionsScanner implements FunctionsScannerInterface
1111
protected $validFunctions;
1212
protected $parser;
1313

14-
public function __construct(array $validFunctions = null)
14+
public function __construct(?array $validFunctions = null)
1515
{
1616
$this->validFunctions = $validFunctions;
1717
$this->parser('latest');
@@ -26,7 +26,7 @@ public function parser(string $version, array $options = ['comments' => true]):
2626

2727
public function scan(string $code, string $filename): array
2828
{
29-
list($version, $options) = $this->parser;
29+
[$version, $options] = $this->parser;
3030

3131
$ast = Peast::$version($code, $options)->parse();
3232

src/JsNodeVisitor.php

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33

44
namespace Gettext\Scanner;
55

6+
use InvalidArgumentException;
67
use Peast\Syntax\Node\CallExpression;
78
use Peast\Syntax\Node\Comment;
9+
use Peast\Syntax\Node\Identifier;
10+
use Peast\Syntax\Node\Literal;
11+
use Peast\Syntax\Node\MemberExpression;
812
use Peast\Syntax\Node\Node;
13+
use Peast\Syntax\Node\TemplateLiteral;
914

1015
class JsNodeVisitor
1116
{
1217
protected $validFunctions;
1318
protected $filename;
1419
protected $functions = [];
1520

16-
public function __construct(string $filename, array $validFunctions = null)
21+
public function __construct(string $filename, ?array $validFunctions = null)
1722
{
1823
$this->filename = $filename;
1924
$this->validFunctions = $validFunctions;
2025
}
2126

22-
public function __invoke(Node $node)
27+
public function __invoke(Node $node): void
2328
{
24-
if ($node->getType() === 'CallExpression') {
29+
if ($node instanceof CallExpression) {
2530
$function = $this->createFunction($node);
2631

2732
if ($function) {
@@ -55,24 +60,20 @@ protected function createFunction(CallExpression $node): ?ParsedFunction
5560
static::addComments($function, $node->getCallee());
5661

5762
foreach ($node->getArguments() as $argument) {
58-
switch ($argument->getType()) {
59-
case 'Literal':
60-
$function->addArgument($argument->getValue());
61-
static::addComments($function, $argument);
62-
break;
63-
case 'TemplateLiteral':
64-
if ($argument->getExpressions()) {
65-
$function->addArgument();
66-
break;
67-
}
68-
63+
if ($argument instanceof Literal) {
64+
$function->addArgument($argument->getValue());
65+
static::addComments($function, $argument);
66+
} elseif ($argument instanceof TemplateLiteral) {
67+
if ($argument->getExpressions()) {
68+
$function->addArgument();
69+
} else {
6970
$quasis = $argument->getQuasis();
7071
$quasis = array_shift($quasis);
7172
$function->addArgument($quasis->getValue());
7273
static::addComments($function, $argument);
73-
break;
74-
default:
75-
$function->addArgument();
74+
}
75+
} else {
76+
$function->addArgument();
7677
}
7778
}
7879

@@ -83,19 +84,15 @@ protected static function getFunctionName(CallExpression $node): ?string
8384
{
8485
$callee = $node->getCallee();
8586

86-
switch ($callee->getType()) {
87-
case 'Identifier':
88-
return $callee->getName();
89-
case 'MemberExpression':
90-
$property = $callee->getProperty();
91-
92-
if ($property->getType() === 'Identifier') {
93-
return $property->getName();
94-
}
95-
return null;
96-
default:
97-
return null;
87+
if ($callee instanceof Identifier) {
88+
return $callee->getName();
89+
} elseif ($callee instanceof MemberExpression) {
90+
$property = $callee->getProperty();
91+
if ($property instanceof Identifier) {
92+
return $property->getName();
93+
}
9894
}
95+
return null;
9996
}
10097

10198
protected static function addComments(ParsedFunction $function, Node $node): void
@@ -109,7 +106,7 @@ protected static function getComment(Comment $comment): string
109106
{
110107
$text = $comment->getText();
111108

112-
$lines = array_map(function ($line) {
109+
$lines = array_map(function ($line): string {
113110
$line = ltrim($line, "#*/ \t");
114111
$line = rtrim($line, "#*/ \t");
115112
return trim($line);

tests/JsFunctionsScannerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class JsFunctionsScannerTest extends TestCase
99
{
10-
public function testJsFunctionsExtractor()
10+
public function testJsFunctionsExtractor(): void
1111
{
1212
$scanner = new JsFunctionsScanner();
1313
$file = __DIR__.'/assets/functions.js';

0 commit comments

Comments
 (0)