Skip to content

Commit 970d082

Browse files
authored
Merge pull request #1124 from veewee/phpparser-v5
Upgrade to php-parser v5
2 parents c057cbc + 4f54a9d commit 970d082

File tree

9 files changed

+189
-157
lines changed

9 files changed

+189
-157
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"require-dev": {
4141
"brianium/paratest": "^6.4",
4242
"composer/composer": "^2.2.6",
43-
"nikic/php-parser": "^4.13",
43+
"nikic/php-parser": "^5.0",
4444
"php-parallel-lint/php-parallel-lint": "^1.3",
4545
"phpspec/phpspec": "^7.2",
4646
"phpspec/prophecy-phpunit": "^2.0",

composer.lock

Lines changed: 134 additions & 132 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/tasks/phpparser.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ grumphp:
1919
tasks:
2020
phpparser:
2121
ignore_patterns: []
22-
kind: php7
22+
php_version: null
2323
visitors: {}
2424
triggered_by: [php]
2525
```
@@ -31,13 +31,13 @@ grumphp:
3131
This is a list of patterns that will be ignored by the PHP Parser.
3232
With this option you can skip files like tests. Leave this option blank to run analysis for every php file.
3333
34-
**kind**
34+
**php_version**
3535
36-
*Default: php7*
36+
*Default: null*
3737
38-
Can be one of: php5, php7.
39-
This option determines which Lexer the PHP_Parser uses to tokenize the PHP code.
40-
By default, the PREFER_PHP7 is loaded.
38+
Can be any PHP version specified as `major.minor`. For example: `8.2`.
39+
There is a special placeholder `latest`, which will use the latest supported PHP version from php-parser.
40+
If left empty, the system's PHP version will be picked. This is the default behaviour.
4141

4242
**visitors**
4343

spec/Parser/Php/Factory/ParserFactorySpec.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,31 @@ function it_is_initializable()
1616

1717
function it_can_create_a_parser_from_task_options()
1818
{
19-
$options = ['kind' => PhpParser::KIND_PHP7];
19+
$options = ['php_version' => null];
2020
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
2121
}
22+
23+
function it_can_create_a_parser_from_empty_options()
24+
{
25+
$options = [];
26+
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
27+
}
28+
29+
function it_can_create_a_parser_from_latest_php_version()
30+
{
31+
$options = ['php_version' => 'latest'];
32+
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
33+
}
34+
35+
function it_can_create_a_parser_from_specific_latest_php_version()
36+
{
37+
$options = ['php_version' => '8.0'];
38+
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
39+
}
40+
41+
function it_fails_on_invalid_version()
42+
{
43+
$options = ['php_version' => 'invalid'];
44+
$this->shouldThrow(\LogicException::class)->duringCreateFromOptions($options);
45+
}
2246
}

src/Parser/Php/Factory/ParserFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44

55
namespace GrumPHP\Parser\Php\Factory;
66

7-
use GrumPHP\Task\PhpParser;
87
use PhpParser\ParserFactory as PhpParserFactory;
8+
use PhpParser\PhpVersion;
99

1010
class ParserFactory
1111
{
1212
public function createFromOptions(array $options): \PhpParser\Parser
1313
{
14-
$kind = (PhpParser::KIND_PHP5 === $options['kind'])
15-
? PhpParserFactory::PREFER_PHP5 : PhpParserFactory::PREFER_PHP7;
14+
$version = $options['php_version'] ?? null;
1615

17-
return (new PhpParserFactory())->create($kind);
16+
return (new PhpParserFactory())->createForVersion(
17+
match ($version) {
18+
null => PhpVersion::getHostVersion(),
19+
'latest' => PhpVersion::getNewestSupported(),
20+
default => PhpVersion::fromString($version)
21+
}
22+
);
1823
}
1924
}

src/Parser/Php/Visitor/ForbiddenStaticMethodCallsVisitor.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ public function leaveNode(Node $node): void
4141
return;
4242
}
4343

44-
/**
45-
* https://github.com/nikic/PHP-Parser/releases/tag/v4.16.0
46-
* @psalm-suppress DeprecatedProperty
47-
*/
48-
$class = implode('\\', $node->class->parts);
49-
44+
$class = implode('\\', $node->class->getParts());
5045
$method = $node->name;
5146
$normalized = sprintf('%s::%s', $class, $method);
5247

src/Task/PhpParser.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
use GrumPHP\Task\Context\RunContext;
1111
use GrumPHP\Runner\TaskResult;
1212
use GrumPHP\Runner\TaskResultInterface;
13-
use Symfony\Component\OptionsResolver\OptionsResolver;
1413

1514
/**
1615
* @extends AbstractParserTask<PhpParser>
1716
*/
1817
class PhpParser extends AbstractParserTask
1918
{
20-
const KIND_PHP5 = 'php5';
21-
const KIND_PHP7 = 'php7';
22-
2319
/**
2420
* @var \GrumPHP\Parser\Php\PhpParser
2521
*/
@@ -30,10 +26,19 @@ public static function getConfigurableOptions(): ConfigOptionsResolver
3026
$resolver = self::sharedOptionsResolver();
3127
$resolver->setDefaults([
3228
'triggered_by' => ['php'],
33-
'kind' => self::KIND_PHP7,
29+
'php_version' => null,
30+
'kind' => null,
3431
'visitors' => [],
3532
]);
3633

34+
$resolver->setAllowedTypes('php_version', ['string', 'null']);
35+
$resolver->setDeprecated(
36+
'kind',
37+
'phpro/grumphp',
38+
'2.5',
39+
'The option "%name%" is deprecated and replaced by the php_version option.'
40+
);
41+
3742
return ConfigOptionsResolver::fromOptionsResolver($resolver);
3843
}
3944

test/Unit/Parser/Php/Visitor/AbstractVisitorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function visit($code): ParseErrorsCollection
4646
$visitor = $this->getVisitor();
4747
$visitor->setContext($context);
4848

49-
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
49+
$parser = (new ParserFactory())->createForHostVersion();
5050
$traverser = new NodeTraverser();
5151
$traverser->addVisitor(new NameResolver());
5252
$traverser->addVisitor($visitor);

test/Unit/Task/PhpParserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ public function provideConfigurableOptions(): iterable
3636
yield 'defaults' => [
3737
[],
3838
[
39-
'kind' => PhpParser::KIND_PHP7,
4039
'visitors' => [],
4140
'triggered_by' => ['php'],
4241
'ignore_patterns' => [],
42+
'php_version' => null,
43+
'kind' => null,
4344
]
4445
];
4546
}

0 commit comments

Comments
 (0)