Skip to content

Commit 605babb

Browse files
committed
Support PHP 7.2 and nikic/php-parser 4
`gettext/gettext` 5 supports PHP 7.2, but the scanner needed PHP 7.4 because of typed properties and nikic/php-parser 5. * Allow nikic/php-parser ^4.18 next to ^5 and create the parser and traverser in a way that works with both. * Handle the scalar node names of nikic/php-parser 4. * Skip duplicate comments. nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one. * Replace typed properties with docblocks. * Allow PHPUnit 8.5 and run the tests on PHP 7.2 and 7.3.
1 parent 43f53b6 commit 605babb

4 files changed

Lines changed: 60 additions & 24 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
strategy:
1515
matrix:
1616
php:
17+
- 7.2
18+
- 7.3
1719
- 7.4
1820
- 8.0
1921
- 8.1
@@ -38,6 +40,10 @@ jobs:
3840
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
3941
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-
4042

43+
- name: Remove tools that need a newer PHP version
44+
if: matrix.php == '7.2' || matrix.php == '7.3'
45+
run: composer remove --dev --no-update friendsofphp/php-cs-fixer oscarotero/php-cs-fixer-config
46+
4147
- name: Install dependencies
4248
run: composer install --prefer-dist --no-progress --no-suggest
4349

‎composer.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
"issues": "https://github.com/php-gettext/PHP-Scanner/issues"
1919
},
2020
"require": {
21-
"php": ">=7.4",
22-
"nikic/php-parser": "^5",
21+
"php": ">=7.2",
22+
"nikic/php-parser": "^4.18 || ^5",
2323
"gettext/gettext": "^5.5.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^9",
26+
"phpunit/phpunit": "^8.5 || ^9",
2727
"squizlabs/php_codesniffer": "^3.0",
2828
"oscarotero/php-cs-fixer-config": "^2",
2929
"friendsofphp/php-cs-fixer": "^3"

‎src/PhpFunctionsScanner.php‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@
99

1010
class PhpFunctionsScanner implements FunctionsScannerInterface
1111
{
12-
protected Parser $parser;
13-
protected ?array $validFunctions;
12+
/** @var Parser */
13+
protected $parser;
14+
15+
/** @var array|null */
16+
protected $validFunctions;
1417

1518
public function __construct(?array $validFunctions = null, ?Parser $parser = null)
1619
{
1720
$this->validFunctions = $validFunctions;
18-
$this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion();
21+
$this->parser = $parser ?: static::createParser();
22+
}
23+
24+
protected static function createParser(): Parser
25+
{
26+
$factory = new ParserFactory();
27+
28+
// nikic/php-parser 5
29+
if (method_exists($factory, 'createForNewestSupportedVersion')) {
30+
return $factory->createForNewestSupportedVersion();
31+
}
32+
33+
// nikic/php-parser 4
34+
return $factory->create(ParserFactory::PREFER_PHP7);
1935
}
2036

2137
public function scan(string $code, string $filename): array
@@ -28,7 +44,8 @@ public function scan(string $code, string $filename): array
2844

2945
$visitor = $this->createNodeVisitor($filename);
3046

31-
$traverser = new NodeTraverser($visitor);
47+
$traverser = new NodeTraverser();
48+
$traverser->addVisitor($visitor);
3249
$traverser->traverse($ast);
3350

3451
return $visitor->getFunctions();

‎src/PhpNodeVisitor.php‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
class PhpNodeVisitor implements NodeVisitor
1616
{
17-
protected ?array $validFunctions;
18-
protected string $filename;
19-
protected array $functions = [];
17+
/** @var array|null */
18+
protected $validFunctions;
2019

21-
/** @var Comment[] */
22-
protected array $bufferComments = [];
20+
/** @var string */
21+
protected $filename;
22+
23+
/** @var ParsedFunction[] */
24+
protected $functions = [];
25+
26+
/** @var Node[] */
27+
protected $bufferComments = [];
2328

2429
public function __construct(string $filename, ?array $validFunctions = null)
2530
{
@@ -86,28 +91,33 @@ protected function createFunction(Expr $node): ParsedFunction
8691
$node->getEndLine()
8792
);
8893

89-
foreach ($node->getComments() as $comment) {
90-
$function->addComment(static::getComment($comment));
91-
}
94+
// nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one.
95+
$seen = [];
96+
$addComments = static function (Node $commentedNode) use ($function, &$seen): void {
97+
foreach ($commentedNode->getComments() as $comment) {
98+
$position = $comment->getStartFilePos();
9299

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-
}
100+
if (!isset($seen[$position])) {
101+
$seen[$position] = true;
102+
$function->addComment(static::getComment($comment));
99103
}
100104
}
105+
};
106+
107+
$addComments($node);
108+
109+
foreach ($this->bufferComments as $bufferComment) {
110+
if ($bufferComment->getStartLine() === $node->getStartLine()) {
111+
$addComments($bufferComment);
112+
}
101113
}
102114

103115
$this->bufferComments = [];
104116

105117
foreach ($node->args as $argument) {
106118
$value = $argument->value;
107119

108-
foreach ($argument->getComments() as $comment) {
109-
$function->addComment(static::getComment($comment));
110-
}
120+
$addComments($argument);
111121

112122
$function->addArgument(static::getValue($value));
113123
}
@@ -151,6 +161,9 @@ protected static function getValue(Expr $value)
151161
case 'Scalar_String':
152162
case 'Scalar_Int':
153163
case 'Scalar_Float':
164+
// nikic/php-parser 4
165+
case 'Scalar_LNumber':
166+
case 'Scalar_DNumber':
154167
return $value->value;
155168
case 'Expr_BinaryOp_Concat':
156169
$values = [];

0 commit comments

Comments
 (0)