diff --git a/application/Espo/Core/InjectableFactory.php b/application/Espo/Core/InjectableFactory.php index a0694f891c..5c9daecb55 100644 --- a/application/Espo/Core/InjectableFactory.php +++ b/application/Espo/Core/InjectableFactory.php @@ -40,6 +40,7 @@ use ReflectionParameter; use ReflectionFunction; use ReflectionNamedType; use Throwable; +use Closure; /** * Creates an instance by a class name. Uses constructor param names and type hinting to detect which @@ -179,7 +180,10 @@ class InjectableFactory !$type->isBuiltin() ) { try { - $dependencyClass = new ReflectionClass($type->getName()); + /** @var class-string */ + $dependencyClassName = $type->getName(); + + $dependencyClass = new ReflectionClass($dependencyClassName); } catch (Throwable $e) { $badClassName = $type->getName(); @@ -239,6 +243,10 @@ class InjectableFactory { $injectionList = []; + if (!$callback instanceof Closure) { + $callback = Closure::fromCallable($callback); + } + $function = new ReflectionFunction($callback); foreach ($function->getParameters() as $param) { @@ -362,7 +370,10 @@ class InjectableFactory $type instanceof ReflectionNamedType && !$type->isBuiltin() ) { - $paramClass = new ReflectionClass($type->getName()); + /** @var class-string */ + $dependencyClassName = $type->getName(); + + $paramClass = new ReflectionClass($dependencyClassName); } if ($paramClass && $paramClass->isInstance($injection)) { @@ -374,6 +385,10 @@ class InjectableFactory /** * @deprecated Use create or createWith methods instead. + * + * @template T of object + * @phpstan-param class-string $className + * @phpstan-return T */ public function createByClassName(string $className, ?array $with = null): object { @@ -388,6 +403,8 @@ class InjectableFactory { $setList = []; + assert($obj instanceof Injectable); + $dependencyList = $obj->getDependencyList(); foreach ($dependencyList as $name) {