Skip to content

Add support for PhpParser v5 for PHP 7.4+ #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
block_comment_start = /*
block_comment = *
block_comment_end = */
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
/vendor/

# Cache files
.php_cs.cache
.phpunit.result.cache
*.cache
13 changes: 6 additions & 7 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
// http://cs.sensiolabs.org/#usage
$finder = PhpCsFixer\Finder::create()
// https://cs.symfony.com/doc/usage.html
$finder = (new PhpCsFixer\Finder())
->in(__DIR__ . '/src')
->in(__DIR__ . '/test')
->in(__DIR__ . '/bin')
;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
// If you're curious what these do, use `php-cs-fixer describe <key>`
'@PSR1' => true,
Expand All @@ -19,9 +19,9 @@
'function_typehint_space' => true,
'include' => true,
'linebreak_after_opening_tag' => true,
// 'list_syntax' => ['syntax' => 'short'], Requires PHP 7.1
'list_syntax' => ['syntax' => 'short'],
'magic_constant_casing' => true,
'method_separation' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'native_function_casing' => true,
'new_with_braces' => true,
'no_blank_lines_after_class_opening' => true,
Expand All @@ -34,7 +34,7 @@
'no_unused_imports' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => false,
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'phpdoc_indent' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
Expand All @@ -49,5 +49,4 @@
'whitespace_after_comma_in_array' => true,
])
->setFinder($finder)
->setCacheFile(__DIR__ . '/.php_cs.cache')
;
22 changes: 14 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@
],
"require": {
"php": "^7.3 || ^8.0",
"nikic/php-parser": "^4.10",
"symfony/console": "^5.1 || ^6.0",
"symfony/filesystem": "^5.0 || ^6.0",
"symfony/finder": "^5.0 || ^6.0"
"nikic/php-parser": "^4.10 || ^5.1",
"symfony/console": "^5.1 || ^6.0 || ^7.0",
"symfony/filesystem": "^5.0 || ^6.0 || ^7.0",
"symfony/finder": "^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"vimeo/psalm": "^4.1",
"phpunit/phpunit": "^9.4",
"friendsofphp/php-cs-fixer": "^2.16 || ^3.12"
"friendsofphp/php-cs-fixer": "3.4.0 || ^3.12",
"phpstan/phpstan": "^1.0 || ^2.0",
"phpunit/phpunit": "^9.4"
},
"scripts": {
"test": "phpunit --verbose"
"test": "phpunit --verbose",
"cs:check": "php-cs-fixer check",
"cs:fix": "php-cs-fixer fix",
"phpstan": "phpstan analyse"
},
"config": {
"sort-packages": true
}
}
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: max
paths:
- src
22 changes: 11 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!--bootstrap="tests/bootstrap.php" -->
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
backupGlobals="false"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
backupGlobals="false"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="Stub Generator Tests">
Expand Down
6 changes: 3 additions & 3 deletions src/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function enterNode(Node $node)
$node instanceof Expression &&
$node->expr instanceof FuncCall &&
$node->expr->name instanceof Name &&
$node->expr->name->parts[0] === 'define'
$node->expr->name->getFirst() === 'define'
) {
$this->isInDeclaration = true;
} elseif ($node instanceof If_) {
Expand Down Expand Up @@ -207,7 +207,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
$node instanceof Expression &&
$node->expr instanceof FuncCall &&
$node->expr->name instanceof Name &&
$node->expr->name->parts[0] === 'define'
$node->expr->name->getFirst() === 'define'
)
) {
// We're leaving one of these.
Expand Down Expand Up @@ -388,7 +388,7 @@ function (\PhpParser\Node\Const_ $const) {
$node instanceof Expression &&
$node->expr instanceof FuncCall &&
$node->expr->name instanceof Name &&
$node->expr->name->parts[0] === 'define'
$node->expr->name->getFirst() === 'define'
) {
$fullyQualifiedName = $node->expr->args[0]->value->value;

Expand Down
4 changes: 2 additions & 2 deletions src/StubsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ public function __construct(int $symbols = self::DEFAULT, array $config = [])
*/
public function generate(Finder $finder, NodeVisitor $visitor = null): Result
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory())->createForNewestSupportedVersion();

if (!($visitor instanceof NodeVisitor)) {
$visitor = new NodeVisitor;
$visitor = new NodeVisitor();
}

$visitor->init($this->symbols, $this->config);
Expand Down
2 changes: 1 addition & 1 deletion test/NodeVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NodeVisitorTest extends TestCase
{
private function parse(string $php, int $symbols, array $config): NodeVisitor
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory())->createForNewestSupportedVersion();

$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
Expand Down