Skip to content

Commit

Permalink
Update ClosureExporter.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelamkoff authored May 25, 2024
1 parent c52a2ce commit 23afecf
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/ClosureExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\PrettyPrinterAbstract;
use PhpParser\Node\Expr\ArrowFunction;
Expand Down Expand Up @@ -78,7 +79,7 @@ private static function getFinder(): NodeFinder

private function createParser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
return (new ParserFactory)->createForVersion(PhpVersion::getHostVersion());
}

private function getReflector(\Closure $closure): \ReflectionFunction
Expand Down Expand Up @@ -126,7 +127,7 @@ public function enterNode(Node $node)
return parent::enterNode($node);
}

public function beforeTraverse(array $nodes)
public function beforeTraverse(array $nodes):? array
{
$this->stack = [];
return parent::beforeTraverse($nodes);
Expand Down Expand Up @@ -201,20 +202,20 @@ public function enterNode(Node $node)
}

foreach ($use->uses as $useUse) {
if (end($useUse->name->parts) == end($node->parts)) {
$parts = array_merge($parts, $useUse->name->parts);
if ($useUse->name->getLast() == $node->getLast()) {
$parts = array_merge($parts, $useUse->name->getParts());
return new Name\FullyQualified($parts, $node->getAttributes());
}
}
}

if ($node->getAttribute('parent') instanceof Node\Expr\ConstFetch) {
if (in_array(strtolower($node->parts[0]), ['null', 'false', 'true'])) {
if (in_array(strtolower($node->getFirst()), ['null', 'false', 'true'])) {
return $node;
}
foreach (get_defined_constants() as $name => $v) {
if ($node->parts[0] == $name) {
return new Name\FullyQualified($node->parts[0], $node->getAttributes());
return new Name\FullyQualified($node->getFirst(), $node->getAttributes());
}
}
}
Expand Down Expand Up @@ -244,19 +245,19 @@ public function enterNode(Node $node)

if ($this->namespace) {
return new Name\FullyQualified(
[...$this->namespace->name->parts, ...$node->parts],
[...$this->namespace->name->getParts(), ...$node->getParts()],
$node->getAttributes()
);
} else {
return new Name\FullyQualified($node->parts, $node->getAttributes());
return new Name\FullyQualified($node->getParts(), $node->getAttributes());
}
}

if ($node instanceof Node\Scalar\MagicConst\Class_) {
if (!$cls) $cls = ($this->clsNameFinder)($node);
if ($cls) {
$part = $cls->name->name;
return new Node\Expr\ClassConstFetch(new Name\FullyQualified([...$this->namespace->name->parts, $part]), 'class',
return new Node\Expr\ClassConstFetch(new Name\FullyQualified([...$this->namespace->name->getParts(), $part]), 'class',
$node->getAttributes()
);
}
Expand Down

0 comments on commit 23afecf

Please sign in to comment.