Skip to content

Commit 2e342de

Browse files
authored
Merge pull request #16 from BusterNeece/master
Update dependencies and update to reflect new PHP Parser changes.
2 parents 989a2cf + 7ae89f3 commit 2e342de

8 files changed

+55
-78
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jobs:
1414
strategy:
1515
matrix:
1616
php:
17-
- 7.2
18-
- 7.3
1917
- 7.4
2018
- 8.0
2119
- 8.1
20+
- 8.2
21+
- 8.3
2222
fail-fast: false
2323

2424
steps:
@@ -41,4 +41,4 @@ jobs:
4141
run: composer install --prefer-dist --no-progress --no-suggest
4242

4343
- name: Tests
44-
run: composer test
44+
run: composer test

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"issues": "https://github.com/php-gettext/PHP-Scanner/issues"
1919
},
2020
"require": {
21-
"php": ">=7.2",
22-
"nikic/php-parser": "^4.2",
21+
"php": ">=7.4",
22+
"nikic/php-parser": "^5",
2323
"gettext/gettext": "^5.5.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^8.0",
26+
"phpunit/phpunit": "^9",
2727
"squizlabs/php_codesniffer": "^3.0",
28-
"oscarotero/php-cs-fixer-config": "^1.0",
29-
"friendsofphp/php-cs-fixer": "^2.15"
28+
"oscarotero/php-cs-fixer-config": "^2",
29+
"friendsofphp/php-cs-fixer": "^3"
3030
},
3131
"autoload": {
3232
"psr-4": {

phpunit.xml

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<phpunit bootstrap="./vendor/autoload.php">
2-
<testsuites>
3-
<testsuite name="All tests">
4-
<directory>./tests/</directory>
5-
</testsuite>
6-
</testsuites>
7-
<filter>
8-
<whitelist processUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">src</directory>
10-
</whitelist>
11-
</filter>
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="All tests">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
1213
</phpunit>

src/PhpFunctionsScanner.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
namespace Gettext\Scanner;
55

66
use PhpParser\NodeTraverser;
7-
use PhpParser\NodeVisitor;
87
use PhpParser\Parser;
98
use PhpParser\ParserFactory;
109

1110
class PhpFunctionsScanner implements FunctionsScannerInterface
1211
{
13-
protected $parser;
14-
protected $validFunctions;
12+
protected Parser $parser;
13+
protected ?array $validFunctions;
1514

1615
public function __construct(array $validFunctions = null, Parser $parser = null)
1716
{
1817
$this->validFunctions = $validFunctions;
19-
$this->parser = $parser ?: (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
18+
$this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion();
2019
}
2120

2221
public function scan(string $code, string $filename): array
@@ -27,15 +26,15 @@ public function scan(string $code, string $filename): array
2726
return [];
2827
}
2928

30-
$traverser = new NodeTraverser();
3129
$visitor = $this->createNodeVisitor($filename);
32-
$traverser->addVisitor($visitor);
30+
31+
$traverser = new NodeTraverser($visitor);
3332
$traverser->traverse($ast);
3433

3534
return $visitor->getFunctions();
3635
}
3736

38-
protected function createNodeVisitor(string $filename): NodeVisitor
37+
protected function createNodeVisitor(string $filename): PhpNodeVisitor
3938
{
4039
return new PhpNodeVisitor($filename, $this->validFunctions);
4140
}

src/PhpNodeVisitor.php

+23-14
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\FuncCall;
10+
use PhpParser\Node\Expr\MethodCall;
1011
use PhpParser\Node\Identifier;
1112
use PhpParser\Node\Name;
1213
use PhpParser\NodeVisitor;
1314

1415
class PhpNodeVisitor implements NodeVisitor
1516
{
16-
protected $validFunctions;
17-
protected $filename;
18-
protected $functions = [];
19-
protected $bufferComments;
17+
protected ?array $validFunctions;
18+
protected string $filename;
19+
protected array $functions = [];
20+
21+
/** @var Comment[] */
22+
protected array $bufferComments = [];
2023

2124
public function __construct(string $filename, array $validFunctions = null)
2225
{
@@ -40,15 +43,17 @@ public function enterNode(Node $node)
4043
if ($name && ($this->validFunctions === null || in_array($name, $this->validFunctions))) {
4144
$this->functions[] = $this->createFunction($node);
4245
} elseif ($node->getComments()) {
43-
$this->bufferComments = $node;
46+
$this->bufferComments[] = $node;
4447
}
45-
return null;
48+
break;
49+
50+
case 'Stmt_Expression':
4651
case 'Stmt_Echo':
4752
case 'Stmt_Return':
4853
case 'Expr_Print':
4954
case 'Expr_Assign':
50-
$this->bufferComments = $node;
51-
return null;
55+
$this->bufferComments[] = $node;
56+
break;
5257
}
5358

5459
return null;
@@ -85,13 +90,17 @@ protected function createFunction(Expr $node): ParsedFunction
8590
$function->addComment(static::getComment($comment));
8691
}
8792

88-
if ($this->bufferComments && $this->bufferComments->getStartLine() === $node->getStartLine()) {
89-
foreach ($this->bufferComments->getComments() as $comment) {
90-
$function->addComment(static::getComment($comment));
93+
if ($this->bufferComments) {
94+
foreach ($this->bufferComments as $bufferComment) {
95+
if ($bufferComment->getStartLine() === $node->getStartLine()) {
96+
foreach ($bufferComment->getComments() as $comment) {
97+
$function->addComment(static::getComment($comment));
98+
}
99+
}
91100
}
92101
}
93102

94-
$this->bufferComments = null;
103+
$this->bufferComments = [];
95104

96105
foreach ($node->args as $argument) {
97106
$value = $argument->value;
@@ -140,8 +149,8 @@ protected static function getValue(Expr $value)
140149

141150
switch ($type) {
142151
case 'Scalar_String':
143-
case 'Scalar_LNumber':
144-
case 'Scalar_DNumber':
152+
case 'Scalar_Int':
153+
case 'Scalar_Float':
145154
return $value->value;
146155
case 'Expr_BinaryOp_Concat':
147156
$values = [];

src/PhpScanner.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Gettext\Scanner;
55

66
use Gettext\Translation;
7-
use Gettext\Translations;
87

98
/**
109
* Class to scan PHP files and get gettext translations

tests/PhpFunctionsScannerTest.php

-36
Original file line numberDiff line numberDiff line change
@@ -188,40 +188,4 @@ public function testPhpFunctionsExtractor()
188188
$this->assertSame($file, $function->getFilename());
189189
$this->assertCount(0, $function->getComments());
190190
}
191-
192-
public function _testPhpFunctionsScannerWithDisabledComments()
193-
{
194-
$scanner = new PhpFunctionsScanner();
195-
$scanner->includeComments(false);
196-
$file = __DIR__.'/assets/functions.php';
197-
$code = file_get_contents($file);
198-
$functions = $scanner->scan($code, $file);
199-
200-
$this->assertCount(11, $functions);
201-
202-
foreach ($functions as $function) {
203-
$this->assertCount(0, $function->getComments());
204-
}
205-
}
206-
207-
public function _testPhpFunctionsScannerWithPrefixedComments()
208-
{
209-
$scanner = new PhpFunctionsScanner();
210-
$scanner->includeComments(['ALLOW:']);
211-
$file = __DIR__.'/assets/functions.php';
212-
$code = file_get_contents($file);
213-
$functions = $scanner->scan($code, $file);
214-
215-
$this->assertCount(11, $functions);
216-
217-
//fn12
218-
$function = $functions[10];
219-
$this->assertCount(1, $function->getComments());
220-
221-
$comments = $function->getComments();
222-
$comment = $comments[0];
223-
$this->assertSame(23, $comment->getLine());
224-
$this->assertSame(23, $comment->getLastLine());
225-
$this->assertSame('ALLOW: Related comment 3', $comment->getComment());
226-
}
227191
}

tests/PhpScannerTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public function testPhpCodeScanner()
2323

2424
$scanner->scanFile($file);
2525

26-
list('domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3) = $scanner->getTranslations();
26+
/**
27+
* @var Translations $domain1
28+
* @var Translations $domain2
29+
* @var Translations $domain3
30+
*/
31+
['domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3] = $scanner->getTranslations();
2732

2833
$this->assertCount(6, $domain1);
2934
$this->assertCount(4, $domain2);

0 commit comments

Comments
 (0)