Skip to content

Commit 23afecf

Browse files
authored
Update ClosureExporter.php
1 parent c52a2ce commit 23afecf

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/ClosureExporter.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\NodeVisitorAbstract;
1212
use PhpParser\Parser;
1313
use PhpParser\ParserFactory;
14+
use PhpParser\PhpVersion;
1415
use PhpParser\PrettyPrinter\Standard;
1516
use PhpParser\PrettyPrinterAbstract;
1617
use PhpParser\Node\Expr\ArrowFunction;
@@ -78,7 +79,7 @@ private static function getFinder(): NodeFinder
7879

7980
private function createParser(): Parser
8081
{
81-
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
82+
return (new ParserFactory)->createForVersion(PhpVersion::getHostVersion());
8283
}
8384

8485
private function getReflector(\Closure $closure): \ReflectionFunction
@@ -126,7 +127,7 @@ public function enterNode(Node $node)
126127
return parent::enterNode($node);
127128
}
128129

129-
public function beforeTraverse(array $nodes)
130+
public function beforeTraverse(array $nodes):? array
130131
{
131132
$this->stack = [];
132133
return parent::beforeTraverse($nodes);
@@ -201,20 +202,20 @@ public function enterNode(Node $node)
201202
}
202203

203204
foreach ($use->uses as $useUse) {
204-
if (end($useUse->name->parts) == end($node->parts)) {
205-
$parts = array_merge($parts, $useUse->name->parts);
205+
if ($useUse->name->getLast() == $node->getLast()) {
206+
$parts = array_merge($parts, $useUse->name->getParts());
206207
return new Name\FullyQualified($parts, $node->getAttributes());
207208
}
208209
}
209210
}
210211

211212
if ($node->getAttribute('parent') instanceof Node\Expr\ConstFetch) {
212-
if (in_array(strtolower($node->parts[0]), ['null', 'false', 'true'])) {
213+
if (in_array(strtolower($node->getFirst()), ['null', 'false', 'true'])) {
213214
return $node;
214215
}
215216
foreach (get_defined_constants() as $name => $v) {
216217
if ($node->parts[0] == $name) {
217-
return new Name\FullyQualified($node->parts[0], $node->getAttributes());
218+
return new Name\FullyQualified($node->getFirst(), $node->getAttributes());
218219
}
219220
}
220221
}
@@ -244,19 +245,19 @@ public function enterNode(Node $node)
244245

245246
if ($this->namespace) {
246247
return new Name\FullyQualified(
247-
[...$this->namespace->name->parts, ...$node->parts],
248+
[...$this->namespace->name->getParts(), ...$node->getParts()],
248249
$node->getAttributes()
249250
);
250251
} else {
251-
return new Name\FullyQualified($node->parts, $node->getAttributes());
252+
return new Name\FullyQualified($node->getParts(), $node->getAttributes());
252253
}
253254
}
254255

255256
if ($node instanceof Node\Scalar\MagicConst\Class_) {
256257
if (!$cls) $cls = ($this->clsNameFinder)($node);
257258
if ($cls) {
258259
$part = $cls->name->name;
259-
return new Node\Expr\ClassConstFetch(new Name\FullyQualified([...$this->namespace->name->parts, $part]), 'class',
260+
return new Node\Expr\ClassConstFetch(new Name\FullyQualified([...$this->namespace->name->getParts(), $part]), 'class',
260261
$node->getAttributes()
261262
);
262263
}

0 commit comments

Comments
 (0)