Skip to content

Commit 1ecb07b

Browse files
committed
small fixes
1 parent 186a3f5 commit 1ecb07b

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function extractMethodBodies(string $className): array
7070

7171
$res = [];
7272
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\ClassMethod::class) as $methodNode) {
73-
assert($methodNode instanceof Node\Stmt\ClassMethod);
7473
if ($methodNode->stmts) {
7574
$res[$methodNode->name->toString()] = $this->getReformattedContents($methodNode->stmts, 2);
7675
}
@@ -110,7 +109,7 @@ public function extractPropertyHookBodies(string $className): array
110109
}
111110

112111

113-
public function extractFunctionBody(string $name): ?string
112+
public function extractFunctionBody(string $name): string
114113
{
115114
$functionNode = (new NodeFinder)->findFirst(
116115
$this->statements,
@@ -250,7 +249,6 @@ public function extractAll(): PhpFile
250249
$namespaces = ['' => $this->statements];
251250
foreach ($this->statements as $node) {
252251
if ($node instanceof Node\Stmt\Declare_
253-
&& $node->declares[0] instanceof Node\Stmt\DeclareDeclare
254252
&& $node->declares[0]->key->name === 'strict_types'
255253
&& $node->declares[0]->value instanceof Node\Scalar\LNumber
256254
) {
@@ -344,6 +342,7 @@ private function addTraitToClass(ClassLike $class, Node\Stmt\TraitUse $node): vo
344342
foreach ($node->traits as $item) {
345343
$trait = $class->addTrait($item->toString());
346344
}
345+
assert($trait instanceof TraitUse);
347346

348347
foreach ($node->adaptations as $item) {
349348
$trait->addResolution(rtrim($this->getReformattedContents([$item], 0), ';'));
@@ -528,10 +527,7 @@ private function toValue(Node\Expr $node): mixed
528527
return new Literal($this->getReformattedContents([$node], 0));
529528

530529
} elseif ($item->key) {
531-
$key = $item->key instanceof Node\Identifier
532-
? $item->key->name
533-
: $this->toValue($item->key);
534-
530+
$key = $this->toValue($item->key);
535531
if ($key instanceof Literal) {
536532
return new Literal($this->getReformattedContents([$node], 0));
537533
}

src/PhpGenerator/Factory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ public function fromClassReflection(
128128
$class->setMethods($methods);
129129

130130
foreach ($from->getTraitNames() as $trait) {
131-
$class->addTrait($trait, $resolutions);
131+
$trait = $class->addTrait($trait);
132+
foreach ($resolutions as $resolution) {
133+
$trait->addResolution($resolution);
134+
}
132135
$resolutions = [];
133136
}
134137

src/PhpGenerator/PhpNamespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function getUses(string $of = self::NameNormal): array
167167
uasort($this->aliases[$of], fn(string $a, string $b): int => strtr($a, '\\', ' ') <=> strtr($b, '\\', ' '));
168168
return array_filter(
169169
$this->aliases[$of],
170-
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
170+
fn($name, $alias) => (bool) strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
171171
ARRAY_FILTER_USE_BOTH,
172172
);
173173
}

src/PhpGenerator/Printer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ private function printHooks(Property|PromotedParameter $property, bool $isInterf
480480
}
481481

482482
$simple = true;
483-
foreach ($property->getHooks() as $type => $hook) {
483+
foreach ($hooks as $type => $hook) {
484484
$simple = $simple && ($hook->isAbstract() || $isInterface);
485485
$hooks[$type] = $this->printDocComment($hook)
486486
. $this->printAttributes($hook->getAttributes())

tests/PhpGenerator/Extractor.extractPropertyHookBodies.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use Tester\Assert;
77

88
require __DIR__ . '/../bootstrap.php';
99

10+
if (!class_exists(PhpParser\Node\PropertyHook::class)) {
11+
Tester\Environment::skip('Requires newer PhpParser.');
12+
}
1013

1114
$extractor = new Extractor(<<<'XX'
1215
<?php

0 commit comments

Comments
 (0)