From e9ae00d487d3db00da3cee55bdab06ea14156596 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Wed, 11 Feb 2026 16:19:03 -0600 Subject: [PATCH] WIP: Update to PHPParser ^5.0 --- composer.json | 2 +- src/phpDocumentor/Reflection/BaseReflector.php | 13 ++++--------- src/phpDocumentor/Reflection/FileReflector.php | 6 +++--- .../FunctionReflector/ArgumentReflector.php | 4 +++- src/phpDocumentor/Reflection/PrettyPrinter.php | 4 ++-- src/phpDocumentor/Reflection/Traverser.php | 4 +++- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index b4445922..5da2946a 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "require": { "php": ">=5.3.3", "psr/log": "~1.0", - "nikic/php-parser": "^1.0", + "nikic/php-parser": "^5.0", "phpdocumentor/reflection-docblock": "~2.0" }, "suggests": { diff --git a/src/phpDocumentor/Reflection/BaseReflector.php b/src/phpDocumentor/Reflection/BaseReflector.php index 543da1cd..5e4755d5 100644 --- a/src/phpDocumentor/Reflection/BaseReflector.php +++ b/src/phpDocumentor/Reflection/BaseReflector.php @@ -128,7 +128,7 @@ protected function extractDocBlock($node) $doc_block = new DocBlock( (string) $comment, $this->context, - new Location($comment->getLine()) + new Location($comment->getStartLine()) ); } catch (Exception $e) { $this->log($e->getMessage(), LogLevel::CRITICAL); @@ -169,7 +169,7 @@ public function getShortName() { return isset($this->node->name) ? $this->node->name - : (string) $this->node; + : ''; } /** @@ -202,12 +202,7 @@ public function getNamespace() return $this->context->getNamespace(); } - $parts = $this->node->namespacedName->parts; - array_pop($parts); - - $namespace = implode('\\', $parts); - - return $namespace ? $namespace : 'global'; + return $this->node->namespacedName ?? 'global'; } /** @@ -256,7 +251,7 @@ public function setNamespaceAlias($alias, $fqnn) */ public function getLinenumber() { - return $this->node->getLine(); + return $this->node->getStartLine(); } /** diff --git a/src/phpDocumentor/Reflection/FileReflector.php b/src/phpDocumentor/Reflection/FileReflector.php index 91ff73af..1d6d730e 100644 --- a/src/phpDocumentor/Reflection/FileReflector.php +++ b/src/phpDocumentor/Reflection/FileReflector.php @@ -243,7 +243,7 @@ function ($comment) { $docblock = new DocBlock( (string) $comments[0], null, - new Location($comments[0]->getLine()) + new Location($comments[0]->getStartLine()) ); // the first DocBlock in a file documents the file if @@ -489,13 +489,13 @@ public function leaveNode(Node $node) foreach ($node->uses as $use) { $this->context->setNamespaceAlias( $use->alias, - implode('\\', $use->name->parts) + $use->name ); } break; case 'PhpParser\Node\Stmt\Namespace_': $this->context->setNamespace( - isset($node->name) && ($node->name) ? implode('\\', $node->name->parts) : '' + $node->name ?: '' ); break; case 'PhpParser\Node\Stmt\Class_': diff --git a/src/phpDocumentor/Reflection/FunctionReflector/ArgumentReflector.php b/src/phpDocumentor/Reflection/FunctionReflector/ArgumentReflector.php index 8b338711..302cd6d3 100644 --- a/src/phpDocumentor/Reflection/FunctionReflector/ArgumentReflector.php +++ b/src/phpDocumentor/Reflection/FunctionReflector/ArgumentReflector.php @@ -52,7 +52,9 @@ public function getDefault() */ public function getType() { - $type = (string) $this->node->type; + $type = $this->node->type instanceof \PHPParser\Node\NullableType + ? "?{$this->node->type->type}" + : (string) $this->node->type; // in case of the callable of array keyword; do not prefix with a \ if ($type == 'callable' || $type == 'array' diff --git a/src/phpDocumentor/Reflection/PrettyPrinter.php b/src/phpDocumentor/Reflection/PrettyPrinter.php index 0d9d0704..077ad0f6 100644 --- a/src/phpDocumentor/Reflection/PrettyPrinter.php +++ b/src/phpDocumentor/Reflection/PrettyPrinter.php @@ -46,13 +46,13 @@ class PrettyPrinter extends Standard * * @return string */ - public function pScalar_String(String_ $node) + public function pScalar_String(String_ $node): string { if (method_exists($this, 'pSafe')) { return $this->pSafe($node->getAttribute('originalValue')); } - return $this->pNoIndent($node->getAttribute('originalValue')); + return $node->getAttribute('originalValue') ?? '(unknown)'; } } diff --git a/src/phpDocumentor/Reflection/Traverser.php b/src/phpDocumentor/Reflection/Traverser.php index 31c3890b..2cc4dfa1 100644 --- a/src/phpDocumentor/Reflection/Traverser.php +++ b/src/phpDocumentor/Reflection/Traverser.php @@ -15,6 +15,8 @@ use PhpParser\Error; use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; +use PhpParser\ParserFactory; +use PhpParser\PhpVersion; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; @@ -78,7 +80,7 @@ public function addVisitor(\PhpParser\NodeVisitor $visitor) */ protected function createParser() { - return new Parser(new Lexer()); + return (new ParserFactory)->createForVersion( PhpVersion::fromString( '7.4' ) ); } /**