-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reflect anonymous classes with their actual names, but don't cache th…
…eir reflections Resolves #22
- Loading branch information
Showing
10 changed files
with
145 additions
and
170 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Typhoon\Reflection\PhpParserReflector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
use Typhoon\Reflection\NameContext\AnonymousClassName; | ||
use Typhoon\Reflection\ReflectionException; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Typhoon\Reflection\PhpParserReflector | ||
*/ | ||
final class FindAnonymousClassVisitor extends NodeVisitorAbstract | ||
{ | ||
private ?Class_ $node = null; | ||
|
||
public function __construct( | ||
private readonly AnonymousClassName $name, | ||
) {} | ||
|
||
public function node(): Class_ | ||
{ | ||
return $this->node ?? throw new ReflectionException(); | ||
} | ||
|
||
public function enterNode(Node $node): ?int | ||
{ | ||
if ($node->getLine() < $this->name->line) { | ||
return null; | ||
} | ||
|
||
if ($node->getLine() > $this->name->line) { | ||
return NodeTraverser::STOP_TRAVERSAL; | ||
} | ||
|
||
if (!$node instanceof Class_ || $node->name !== null) { | ||
return null; | ||
} | ||
|
||
if ($this->node !== null) { | ||
throw new ReflectionException(sprintf('More than 1 anonymous class at %s:%d.', $this->name->file, $this->name->line)); | ||
} | ||
|
||
$this->node = $node; | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.