| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Diezz\YamlToObjectMapper\Resolver; |
||
| 4 | |||
| 5 | use phpDocumentor\Reflection\Types\ClassString; |
||
| 6 | use RuntimeException; |
||
| 7 | |||
| 8 | class ArgumentResolverFactory |
||
| 9 | { |
||
| 10 | private array $registeredResolvers = [ |
||
| 11 | 'format' => FormatArgumentResolver::class, |
||
| 12 | 'substring' => SubstringArgumentResolver::class, |
||
| 13 | 'now' => NowArgumentResolver::class, |
||
| 14 | 'self' => SelfArgumentResolver::class, |
||
| 15 | 'env' => EnvironmentArgumentResolver::class, |
||
| 16 | ]; |
||
| 17 | |||
| 18 | 1 | public function addResolver(string $resolverAlias, string $resolverClassName): void |
|
| 19 | { |
||
| 20 | 1 | $interfaces = class_implements($resolverClassName); |
|
| 21 | 1 | if ($interfaces && in_array(CustomArgumentResolver::class, $interfaces, true)) { |
|
|
0 ignored issues
–
show
|
|||
| 22 | throw new RuntimeException('Resolver must be an instance of ArgumentResolver class'); |
||
| 23 | } |
||
| 24 | 1 | if (array_key_exists($resolverAlias, $this->registeredResolvers)) { |
|
| 25 | throw new RuntimeException("Resolver with name $resolverAlias has already registered"); |
||
| 26 | } |
||
| 27 | 1 | $this->registeredResolvers[$resolverAlias] = $resolverClassName; |
|
| 28 | 1 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @throws ArgumentResolverException |
||
| 32 | */ |
||
| 33 | 1 | public function create(string $provider, array $arguments): ArgumentResolver |
|
| 34 | { |
||
| 35 | 1 | $argumentResolverClassName = $this->findArgumentResolverClassName($provider); |
|
| 36 | 1 | if (empty($arguments)) { |
|
| 37 | $arguments[] = null; |
||
| 38 | } |
||
| 39 | |||
| 40 | 1 | return new $argumentResolverClassName(...$arguments); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @throws ArgumentResolverException |
||
| 45 | * |
||
| 46 | * @return ClassString |
||
| 47 | */ |
||
| 48 | 1 | private function findArgumentResolverClassName(string $argumentResolverName): string |
|
| 49 | { |
||
| 50 | 1 | if (!array_key_exists($argumentResolverName, $this->registeredResolvers)) { |
|
| 51 | throw new ArgumentResolverException("Couldn't find suitable argument resolver for name $argumentResolverName"); |
||
| 52 | } |
||
| 53 | |||
| 54 | 1 | $argumentResolverClassName = $this->registeredResolvers[$argumentResolverName]; |
|
| 55 | |||
| 56 | 1 | if (!class_exists($argumentResolverClassName)) { |
|
| 57 | throw new ArgumentResolverException("Unable to find class $argumentResolverName"); |
||
| 58 | } |
||
| 59 | 1 | if (!is_subclass_of($argumentResolverClassName, ArgumentResolver::class)) { |
|
| 60 | throw new ArgumentResolverException("The argument resolver $argumentResolverClassName must extends ArgumentResolver abstract class"); |
||
| 61 | } |
||
| 62 | |||
| 63 | 1 | return $argumentResolverClassName; |
|
| 64 | } |
||
| 65 | } |
||
| 66 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.