Skip to content

Commit

Permalink
Merge pull request #1124 from veewee/phpparser-v5
Browse files Browse the repository at this point in the history
Upgrade to php-parser v5
  • Loading branch information
veewee committed Feb 9, 2024
2 parents c057cbc + 4f54a9d commit 970d082
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 157 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"require-dev": {
"brianium/paratest": "^6.4",
"composer/composer": "^2.2.6",
"nikic/php-parser": "^4.13",
"nikic/php-parser": "^5.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpspec/phpspec": "^7.2",
"phpspec/prophecy-phpunit": "^2.0",
Expand Down
266 changes: 134 additions & 132 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions doc/tasks/phpparser.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ grumphp:
tasks:
phpparser:
ignore_patterns: []
kind: php7
php_version: null
visitors: {}
triggered_by: [php]
```
Expand All @@ -31,13 +31,13 @@ grumphp:
This is a list of patterns that will be ignored by the PHP Parser.
With this option you can skip files like tests. Leave this option blank to run analysis for every php file.

**kind**
**php_version**

*Default: php7*
*Default: null*

Can be one of: php5, php7.
This option determines which Lexer the PHP_Parser uses to tokenize the PHP code.
By default, the PREFER_PHP7 is loaded.
Can be any PHP version specified as `major.minor`. For example: `8.2`.
There is a special placeholder `latest`, which will use the latest supported PHP version from php-parser.
If left empty, the system's PHP version will be picked. This is the default behaviour.

**visitors**

Expand Down
26 changes: 25 additions & 1 deletion spec/Parser/Php/Factory/ParserFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@ function it_is_initializable()

function it_can_create_a_parser_from_task_options()
{
$options = ['kind' => PhpParser::KIND_PHP7];
$options = ['php_version' => null];
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
}

function it_can_create_a_parser_from_empty_options()
{
$options = [];
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
}

function it_can_create_a_parser_from_latest_php_version()
{
$options = ['php_version' => 'latest'];
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
}

function it_can_create_a_parser_from_specific_latest_php_version()
{
$options = ['php_version' => '8.0'];
$this->createFromOptions($options)->shouldBeAnInstanceOf(Parser::class);
}

function it_fails_on_invalid_version()
{
$options = ['php_version' => 'invalid'];
$this->shouldThrow(\LogicException::class)->duringCreateFromOptions($options);
}
}
13 changes: 9 additions & 4 deletions src/Parser/Php/Factory/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace GrumPHP\Parser\Php\Factory;

use GrumPHP\Task\PhpParser;
use PhpParser\ParserFactory as PhpParserFactory;
use PhpParser\PhpVersion;

class ParserFactory
{
public function createFromOptions(array $options): \PhpParser\Parser
{
$kind = (PhpParser::KIND_PHP5 === $options['kind'])
? PhpParserFactory::PREFER_PHP5 : PhpParserFactory::PREFER_PHP7;
$version = $options['php_version'] ?? null;

return (new PhpParserFactory())->create($kind);
return (new PhpParserFactory())->createForVersion(
match ($version) {
null => PhpVersion::getHostVersion(),
'latest' => PhpVersion::getNewestSupported(),
default => PhpVersion::fromString($version)
}
);
}
}
7 changes: 1 addition & 6 deletions src/Parser/Php/Visitor/ForbiddenStaticMethodCallsVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public function leaveNode(Node $node): void
return;
}

/**
* https://github.com/nikic/PHP-Parser/releases/tag/v4.16.0
* @psalm-suppress DeprecatedProperty
*/
$class = implode('\\', $node->class->parts);

$class = implode('\\', $node->class->getParts());
$method = $node->name;
$normalized = sprintf('%s::%s', $class, $method);

Expand Down
15 changes: 10 additions & 5 deletions src/Task/PhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractParserTask<PhpParser>
*/
class PhpParser extends AbstractParserTask
{
const KIND_PHP5 = 'php5';
const KIND_PHP7 = 'php7';

/**
* @var \GrumPHP\Parser\Php\PhpParser
*/
Expand All @@ -30,10 +26,19 @@ public static function getConfigurableOptions(): ConfigOptionsResolver
$resolver = self::sharedOptionsResolver();
$resolver->setDefaults([
'triggered_by' => ['php'],
'kind' => self::KIND_PHP7,
'php_version' => null,
'kind' => null,
'visitors' => [],
]);

$resolver->setAllowedTypes('php_version', ['string', 'null']);
$resolver->setDeprecated(
'kind',
'phpro/grumphp',
'2.5',
'The option "%name%" is deprecated and replaced by the php_version option.'
);

return ConfigOptionsResolver::fromOptionsResolver($resolver);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Unit/Parser/Php/Visitor/AbstractVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function visit($code): ParseErrorsCollection
$visitor = $this->getVisitor();
$visitor->setContext($context);

$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory())->createForHostVersion();
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor($visitor);
Expand Down
3 changes: 2 additions & 1 deletion test/Unit/Task/PhpParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public function provideConfigurableOptions(): iterable
yield 'defaults' => [
[],
[
'kind' => PhpParser::KIND_PHP7,
'visitors' => [],
'triggered_by' => ['php'],
'ignore_patterns' => [],
'php_version' => null,
'kind' => null,
]
];
}
Expand Down

0 comments on commit 970d082

Please sign in to comment.