Skip to content
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
7 changes: 6 additions & 1 deletion src/NodeVisitor/GeneratorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class GeneratorDetector extends NodeVisitorAbstract
private bool $isGenerator = false;

/**
* {@inheritDoc}
* Detects yield expressions, without looking into closures
*
* The return type is declared in the docblock only, like in PhpParser\NodeVisitor: an explicit @return keeps
* Symfony's DebugClassLoader from reporting the tentative return type of the parent method.
*
* @return int|null self::DONT_TRAVERSE_CHILDREN for closures, null otherwise
*/
public function enterNode(Node $node)
{
Expand Down
9 changes: 8 additions & 1 deletion src/NodeVisitor/RootNamespaceNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
class RootNamespaceNormalizer extends NodeVisitorAbstract
{
/**
* {@inheritdoc}
* Wraps the statements of a file without a namespace into a global namespace node
*
* The return type is declared in the docblock only, like in PhpParser\NodeVisitor: an explicit @return keeps
* Symfony's DebugClassLoader from reporting the tentative return type of the parent method.
*
* @param \PhpParser\Node[] $nodes
*
* @return \PhpParser\Node[]|null Null when the file declares namespaces itself
*/
public function beforeTraverse(array $nodes)
{
Expand Down
7 changes: 6 additions & 1 deletion src/NodeVisitor/StaticVariablesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public function __construct(\ReflectionClass|\ReflectionFunction|\ReflectionMeth
}

/**
* {@inheritDoc}
* Collects static variables with their default values, without looking into closures
*
* The return type is declared in the docblock only, like in PhpParser\NodeVisitor: an explicit @return keeps
* Symfony's DebugClassLoader from reporting the tentative return type of the parent method.
*
* @return int|null self::DONT_TRAVERSE_CHILDREN for closures, null otherwise
*/
public function enterNode(Node $node)
{
Expand Down
64 changes: 64 additions & 0 deletions tests/NodeVisitor/NodeVisitorReturnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);
/**
* Parser Reflection API
*
* @copyright Copyright 2026, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\ParserReflection\NodeVisitor;

use PhpParser\NodeVisitor;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

/**
* PhpParser\NodeVisitor declares the return types of its methods in docblocks only. An implementation without a
* native return type and without its own @return makes Symfony's DebugClassLoader report that the parent "might
* add" a native return type, so every overridden visitor method documents its return type explicitly.
*/
final class NodeVisitorReturnTypeTest extends TestCase
{
/**
* @param class-string<NodeVisitor> $visitorClass
*/
#[DataProvider('visitorMethods')]
public function testOverriddenVisitorMethodsDocumentTheirReturnType(string $visitorClass, string $methodName): void
{
$method = new ReflectionClass($visitorClass)->getMethod($methodName);
$this->assertFalse($method->hasReturnType(), 'The return type is documented, not declared: PhpParser\NodeVisitor owns it');

$docComment = $method->getDocComment();
$this->assertIsString($docComment);
$this->assertMatchesRegularExpression('/@return\s+\S+/', $docComment);
}

/**
* @return iterable<string, array{class-string<NodeVisitor>, string}>
*/
public static function visitorMethods(): iterable
{
$visitorMethods = array_map(
static fn (\ReflectionMethod $method): string => $method->getName(),
new ReflectionClass(NodeVisitor::class)->getMethods()
);
foreach (glob(dirname(__DIR__, 2) . '/src/NodeVisitor/*.php') ?: [] as $file) {
/** @var class-string $visitorClass */
$visitorClass = __NAMESPACE__ . '\\' . basename($file, '.php');
$visitor = new ReflectionClass($visitorClass);
if (!$visitor->implementsInterface(NodeVisitor::class)) {
continue;
}
foreach ($visitorMethods as $methodName) {
if ($visitor->getMethod($methodName)->getDeclaringClass()->getName() === $visitorClass) {
yield $visitor->getShortName() . '::' . $methodName => [$visitorClass, $methodName];
}
}
}
}
}
Loading