Skip to content

Commit 8d5aa46

Browse files
committed
Update dependencies and update to reflect new PHP Parser changes.
1 parent 989a2cf commit 8d5aa46

File tree

6 files changed

+50
-71
lines changed

6 files changed

+50
-71
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
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": "^11",
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

Lines changed: 12 additions & 11 deletions
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/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
3+
<testsuites>
4+
<testsuite name="All tests">
5+
<directory>./tests/</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source>
9+
<include>
10+
<directory suffix=".php">src</directory>
11+
</include>
12+
</source>
1213
</phpunit>

src/PhpFunctionsScanner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
class PhpFunctionsScanner implements FunctionsScannerInterface
1212
{
13-
protected $parser;
14-
protected $validFunctions;
13+
protected Parser $parser;
14+
protected ?array $validFunctions;
1515

1616
public function __construct(array $validFunctions = null, Parser $parser = null)
1717
{
1818
$this->validFunctions = $validFunctions;
19-
$this->parser = $parser ?: (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
19+
$this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion();
2020
}
2121

2222
public function scan(string $code, string $filename): array
@@ -35,7 +35,7 @@ public function scan(string $code, string $filename): array
3535
return $visitor->getFunctions();
3636
}
3737

38-
protected function createNodeVisitor(string $filename): NodeVisitor
38+
protected function createNodeVisitor(string $filename): PhpNodeVisitor
3939
{
4040
return new PhpNodeVisitor($filename, $this->validFunctions);
4141
}

src/PhpNodeVisitor.php

Lines changed: 21 additions & 12 deletions
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;

tests/PhpFunctionsScannerTest.php

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testPhpFunctionsExtractor()
2929
$function = array_shift($functions);
3030
$this->assertSame('fn1', $function->getName());
3131
$this->assertSame(3, $function->countArguments());
32-
$this->assertSame(['arg1', 'arg2', 3], $function->getArguments());
32+
$this->assertSame(['arg1', 'arg2', null], $function->getArguments());
3333
$this->assertSame(4, $function->getLine());
3434
$this->assertSame(4, $function->getLastLine());
3535
$this->assertSame($file, $function->getFilename());
@@ -71,7 +71,7 @@ public function testPhpFunctionsExtractor()
7171
$function = array_shift($functions);
7272
$this->assertSame('fn5', $function->getName());
7373
$this->assertSame(2, $function->countArguments());
74-
$this->assertSame([6, 7.5], $function->getArguments());
74+
$this->assertSame([null, null], $function->getArguments());
7575
$this->assertSame(6, $function->getLine());
7676
$this->assertSame(6, $function->getLastLine());
7777
$this->assertSame($file, $function->getFilename());
@@ -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

Lines changed: 6 additions & 1 deletion
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)