Skip to content

EspoCRM – Open Source CRM Application

Ref

Yuriibe3aeb275f1d
diff --git a/application/Espo/Core/Binding/Binder.php b/application/Espo/Core/Binding/Binder.php
index 34d2dd27c5..a5d41469c9 100644
--- a/application/Espo/Core/Binding/Binder.php
+++ b/application/Espo/Core/Binding/Binder.php
@@ -50,10 +50,7 @@ class Binder
$key = self::keyToString($key);
$this->validateBindingKey($key);
- $this->data->addGlobal(
- $key,
- Binding::createFromImplementationClassName($implementationClassName)
- );
+ $this->data->addGlobal($key, Binding::createFromImplementationClassName($implementationClassName));
return $this;
}
@@ -69,10 +66,7 @@ class Binder
$key = self::keyToString($key);
$this->validateBindingKey($key);
- $this->data->addGlobal(
- $key,
- Binding::createFromServiceName($serviceName)
- );
+ $this->data->addGlobal($key, Binding::createFromServiceName($serviceName));
return $this;
}
@@ -90,10 +84,7 @@ class Binder
$key = self::keyToString($key);
$this->validateBindingKey($key);
- $this->data->addGlobal(
- $key,
- Binding::createFromCallback($callback)
- );
+ $this->data->addGlobal($key, Binding::createFromCallback($callback));
return $this;
}
@@ -104,17 +95,13 @@ class Binder
* @template T of object
* @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
* @param T $instance An instance.
- * @noinspection PhpDocSignatureInspection
*/
public function bindInstance(string|NamedClassKey $key, object $instance): self
{
$key = self::keyToString($key);
$this->validateBindingKey($key);
- $this->data->addGlobal(
- $key,
- Binding::createFromValue($instance)
- );
+ $this->data->addGlobal($key, Binding::createFromValue($instance));
return $this;
}
@@ -131,10 +118,7 @@ class Binder
$key = self::keyToString($key);
$this->validateBindingKey($key);
- $this->data->addGlobal(
- $key,
- Binding::createFromFactoryClassName($factoryClassName)
- );
+ $this->data->addGlobal($key, Binding::createFromFactoryClassName($factoryClassName));
return $this;
}
diff --git a/application/Espo/Core/Binding/Binding.php b/application/Espo/Core/Binding/Binding.php
index b6f4dfdc53..7e8175fc3f 100644
--- a/application/Espo/Core/Binding/Binding.php
+++ b/application/Espo/Core/Binding/Binding.php
@@ -33,20 +33,17 @@ use LogicException;
class Binding
{
- public const IMPLEMENTATION_CLASS_NAME = 1;
- public const CONTAINER_SERVICE = 2;
- public const VALUE = 3;
- public const CALLBACK = 4;
- public const FACTORY_CLASS_NAME = 5;
+ public const int IMPLEMENTATION_CLASS_NAME = 1;
+ public const int CONTAINER_SERVICE = 2;
+ public const int VALUE = 3;
+ public const int CALLBACK = 4;
+ public const int FACTORY_CLASS_NAME = 5;
private int $type;
- /** @var mixed */
- private $value;
- /**
- * @param mixed $value
- */
- private function __construct(int $type, $value)
+ private mixed $value;
+
+ private function __construct(int $type, mixed $value)
{
$this->type = $type;
$this->value = $value;
@@ -57,10 +54,7 @@ class Binding
return $this->type;
}
- /**
- * @return mixed
- */
- public function getValue()
+ public function getValue(): mixed
{
return $this->value;
}
diff --git a/application/Espo/Core/Binding/BindingContainer.php b/application/Espo/Core/Binding/BindingContainer.php
index 7ec045f2b9..96af818b2c 100644
--- a/application/Espo/Core/Binding/BindingContainer.php
+++ b/application/Espo/Core/Binding/BindingContainer.php
@@ -67,8 +67,7 @@ class BindingContainer
throw new LogicException("Cannot get not existing binding.");
}
- /** @var Binding */
- return $this->getInternal($class, $param);
+ return $this->getInternal($class, $param) ?? throw new LogicException();
}
/**
@@ -104,74 +103,94 @@ class BindingContainer
*/
private function getInternal(?ReflectionClass $class, ReflectionParameter $param): ?Binding
{
- $className = null;
-
- $key = null;
-
if ($class) {
- $className = $class->getName();
+ $binding = $this->getInternalContextualNamed($class, $param);
- $key = '$' . $param->getName();
+ if ($binding) {
+ return $binding;
+ }
}
- $type = $param->getType();
+ $paramClassName = $this->getClassNameFromParameterType($param->getType());
- if (
- $className &&
- $key &&
- $this->data->hasContext($className, $key)
- ) {
- $binding = $this->data->getContext($className, $key);
+ if ($paramClassName === null) {
+ return null;
+ }
- $notMatching =
- $type instanceof ReflectionNamedType &&
- !$type->isBuiltin() &&
- $binding->getType() === Binding::VALUE &&
- is_scalar($binding->getValue());
+ $keyWithName = $paramClassName . ' $' . $param->getName();
- if (!$notMatching) {
- return $binding;
- }
+ $binding = $this->getInternalByClassNameKey($class?->getName(), $keyWithName);
+
+ if ($binding) {
+ return $binding;
}
- $dependencyClassName = null;
+ $key = $paramClassName;
- if (
- $type instanceof ReflectionNamedType &&
- !$type->isBuiltin()
- ) {
- $dependencyClassName = $type->getName();
+ $binding = $this->getInternalByClassNameKey($class?->getName(), $key);
+
+ if ($binding) {
+ return $binding;
}
- $key = null;
- $keyWithParamName = null;
+ return null;
+ }
- if ($dependencyClassName) {
- $key = $dependencyClassName;
+ /**
+ * @param ReflectionClass<object> $class
+ */
+ private function getInternalContextualNamed(ReflectionClass $class, ReflectionParameter $param): ?Binding
+ {
+ $key = '$' . $param->getName();
- $keyWithParamName = $key . ' $' . $param->getName();
+ if (!$this->data->hasContext($class->getName(), $key)) {
+ return null;
}
- if ($keyWithParamName) {
- if ($className && $this->data->hasContext($className, $keyWithParamName)) {
- return $this->data->getContext($className, $keyWithParamName);
- }
+ $type = $param->getType();
- if ($this->data->hasGlobal($keyWithParamName)) {
- return $this->data->getGlobal($keyWithParamName);
- }
+ $binding = $this->data->getContext($class->getName(), $key);
+
+ $notMatching =
+ $type instanceof ReflectionNamedType &&
+ !$type->isBuiltin() &&
+ $binding->getType() === Binding::VALUE &&
+ is_scalar($binding->getValue());
+
+ if ($notMatching) {
+ return null;
}
- if ($key) {
- if ($className && $this->data->hasContext($className, $key)) {
- return $this->data->getContext($className, $key);
- }
+ return $binding;
+ }
- if ($this->data->hasGlobal($key)) {
- return $this->data->getGlobal($key);
- }
+ /**
+ * @param ?class-string<object> $className
+ */
+ private function getInternalByClassNameKey(?string $className, string $key): ?Binding
+ {
+ if ($className && $this->data->hasContext($className, $key)) {
+ return $this->data->getContext($className, $key);
+ }
+
+ if ($this->data->hasGlobal($key)) {
+ return $this->data->getGlobal($key);
}
return null;
}
+
+ private function getClassNameFromParameterType(mixed $type): ?string
+ {
+ $dependencyClassName = null;
+
+ if (
+ $type instanceof ReflectionNamedType &&
+ !$type->isBuiltin()
+ ) {
+ $dependencyClassName = $type->getName();
+ }
+
+ return $dependencyClassName;
+ }
}
diff --git a/application/Espo/Core/Binding/ContextualBinder.php b/application/Espo/Core/Binding/ContextualBinder.php
index 335edf8ce7..de46cb341d 100644
--- a/application/Espo/Core/Binding/ContextualBinder.php
+++ b/application/Espo/Core/Binding/ContextualBinder.php
@@ -37,6 +37,7 @@ use LogicException;
class ContextualBinder
{
private BindingData $data;
+
/** @var class-string<object> */
private string $className;
@@ -61,11 +62,9 @@ class ContextualBinder
$key = self::keyToString($key);
$this->validateBindingKeyNoParameterName($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromImplementationClassName($implementationClassName)
- );
+ $binding = Binding::createFromImplementationClassName($implementationClassName);
+
+ $this->data->addContext($this->className, $key, $binding);
return $this;
}
@@ -82,11 +81,7 @@ class ContextualBinder
$key = self::keyToString($key);
$this->validateBindingKeyNoParameterName($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromServiceName($serviceName)
- );
+ $this->data->addContext($this->className, $key, Binding::createFromServiceName($serviceName));
return $this;
}
@@ -102,11 +97,7 @@ class ContextualBinder
$key = self::keyToString($key);
$this->validateBindingKeyParameterName($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromValue($value)
- );
+ $this->data->addContext($this->className, $key, Binding::createFromValue($value));
return $this;
}
@@ -117,18 +108,13 @@ class ContextualBinder
* @template T of object
* @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
* @param T $instance An instance.
- * @noinspection PhpDocSignatureInspection
*/
public function bindInstance(string|NamedClassKey $key, object $instance): self
{
$key = self::keyToString($key);
$this->validateBindingKeyNoParameterName($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromValue($instance)
- );
+ $this->data->addContext($this->className, $key, Binding::createFromValue($instance));
return $this;
}
@@ -145,11 +131,7 @@ class ContextualBinder
$key = self::keyToString($key);
$this->validateBinding($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromCallback($callback)
- );
+ $this->data->addContext($this->className, $key, Binding::createFromCallback($callback));
return $this;
}
@@ -166,11 +148,7 @@ class ContextualBinder
$key = self::keyToString($key);
$this->validateBindingKeyNoParameterName($key);
- $this->data->addContext(
- $this->className,
- $key,
- Binding::createFromFactoryClassName($factoryClassName)
- );
+ $this->data->addContext($this->className, $key, Binding::createFromFactoryClassName($factoryClassName));
return $this;
}
diff --git a/application/Espo/Core/InjectableFactory.php b/application/Espo/Core/InjectableFactory.php
index 6cc09787a1..29050f9e92 100644
--- a/application/Espo/Core/InjectableFactory.php
+++ b/application/Espo/Core/InjectableFactory.php
@@ -34,8 +34,8 @@ use Psr\Container\NotFoundExceptionInterface;
use Espo\Core\Binding\BindingContainer;
use Espo\Core\Binding\Binding;
use Espo\Core\Binding\Factory;
-
use ReflectionClass;
+use ReflectionException;
use ReflectionParameter;
use ReflectionFunction;
use ReflectionNamedType;
@@ -114,6 +114,7 @@ class InjectableFactory
null;
if (!$binding) {
+ /** @noinspection PhpUnhandledExceptionInspection */
$class = new ReflectionClass($interfaceName);
if ($class->isInterface()) {
@@ -145,6 +146,7 @@ class InjectableFactory
throw new RuntimeException("Class `$interfaceName` resolved to another type.");
}
+ /** @noinspection PhpIncompatibleReturnTypeInspection */
return $obj;
}
@@ -157,18 +159,22 @@ class InjectableFactory
private function createInternal(
string $className,
?array $with = null,
- ?BindingContainer $bindingContainer = null
+ ?BindingContainer $bindingContainer = null,
): object {
if (!class_exists($className)) {
- throw new RuntimeException("InjectableFactory: Class '$className' does not exist.");
+ throw new RuntimeException("Class '$className' does not exist.");
}
$class = new ReflectionClass($className);
$injectionList = $this->getConstructorInjectionList($class, $with, $bindingContainer);
- $obj = $class->newInstanceArgs($injectionList);
+ try {
+ $obj = $class->newInstanceArgs($injectionList);
+ } catch (ReflectionException $e) {
+ throw new RuntimeException("Reflection error.", previous: $e);
+ }
$this->applyAwareInjections($class, $obj);
@@ -178,7 +184,7 @@ class InjectableFactory
/**
* @param ReflectionClass<object> $class
* @param ?array<string, mixed> $with
- * @return mixed[]
+ * @return array<int, mixed>[]
*/
private function getConstructorInjectionList(
ReflectionClass $class,
@@ -225,6 +231,7 @@ class InjectableFactory
$type = $param->getType();
+ /** @noinspection PhpConditionCheckedByNextConditionInspection */
if (
$type &&
$type instanceof ReflectionNamedType &&
@@ -290,17 +297,21 @@ class InjectableFactory
}
/**
- * @return mixed[]
+ * @return array<int, mixed>[]
*/
private function getCallbackInjectionList(callable $callback): array
{
$injectionList = [];
if (!$callback instanceof Closure) {
- $callback = Closure::fromCallable($callback);
+ $callback = $callback(...);
}
- $function = new ReflectionFunction($callback);
+ try {
+ $function = new ReflectionFunction($callback);
+ } catch (ReflectionException $e) {
+ throw new RuntimeException("Reflection error.", previous: $e);
+ }
foreach ($function->getParameters() as $param) {
$injectionList[] = $this->getMethodParamInjection(null, $param);
@@ -372,9 +383,8 @@ class InjectableFactory
/**
* @param ReflectionClass<object> $class
- * @param string[] $ignoreList
*/
- private function applyAwareInjections(ReflectionClass $class, object $obj, array $ignoreList = []): void
+ private function applyAwareInjections(ReflectionClass $class, object $obj): void
{
foreach ($class->getInterfaces() as $interface) {
$interfaceName = $interface->getShortName();
@@ -385,10 +395,6 @@ class InjectableFactory
$name = lcfirst(substr($interfaceName, 0, -5));
- if (in_array($name, $ignoreList)) {
- continue;
- }
-
if (!$this->classHasDependencySetter($class, $name, true)) {
continue;
}
@@ -432,6 +438,7 @@ class InjectableFactory
$type = $params[0]->getType();
+ /** @noinspection PhpConditionCheckedByNextConditionInspection */
if (
$type &&
$type instanceof ReflectionNamedType &&