Skip to content

Commit

Permalink
Be more resilient against non-existent traits and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 26, 2022
1 parent e594f06 commit 72bb305
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,10 +1197,17 @@ public function getTraits(): array
if ($this->cachedTraits !== null) {
return $this->cachedTraits;
}
return $this->cachedTraits = array_values(array_map(
fn (string $importedTrait): ReflectionClass => $this->reflector->reflectClass($importedTrait),
$this->traitNames,
));

$traits = [];
foreach ($this->traitNames as $traitName) {
try {
$traits[] = $this->reflector->reflectClass($traitName);
} catch (IdentifierNotFound) {
// pass
}
}

return $this->cachedTraits = $traits;
}

/**
Expand Down Expand Up @@ -1463,12 +1470,12 @@ private function getCurrentClassImplementedInterfacesIndexedByName(): array
foreach ($this->implementsNames as $name) {
try {
$interface = $this->reflector->reflectClass($name);
foreach ($interface->getInterfacesHierarchy() as $n => $i) {
$interfaces[$n] = $i;
}
} catch (IdentifierNotFound $e) {
continue;
}
foreach ($interface->getInterfacesHierarchy() as $n => $i) {
$interfaces[$n] = $i;
}
}

return $interfaces;
Expand Down Expand Up @@ -1510,16 +1517,19 @@ private function getInterfacesHierarchy(): array
throw NotAnInterfaceReflection::fromReflectionClass($this);
}

/** @var array<class-string, self> */
return array_merge(
[$this->getName() => $this],
...array_map(
fn (string $interfaceName): array => $this
->reflector->reflectClass($interfaceName)
->getInterfacesHierarchy(),
$this->interfaceExtendsNames,
),
);
$interfaces = [$this->getName() => $this];
foreach ($this->interfaceExtendsNames as $interfaceName) {
try {
$interface = $this->reflector->reflectClass($interfaceName);
foreach ($interface->getInterfacesHierarchy() as $hName => $hInterface) {
$interfaces[$hName] = $hInterface;
}
} catch (IdentifierNotFound) {
continue;
}
}

return $interfaces;
}

/**
Expand Down

0 comments on commit 72bb305

Please sign in to comment.