| Conditions | 6 |
| Paths | 7 |
| Total Lines | 25 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 6.0163 |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 22 | 115 | public function resolve(string $className): RuleHandlerInterface |
|
| 23 | { |
||
| 24 | 115 | if (!class_exists($className)) { |
|
| 25 | 1 | throw new RuleHandlerNotFoundException($className); |
|
| 26 | } |
||
| 27 | |||
| 28 | 114 | if (array_key_exists($className, $this->instances)) { |
|
| 29 | 35 | return $this->instances[$className]; |
|
| 30 | } |
||
| 31 | |||
| 32 | try { |
||
| 33 | 101 | $classInstance = new $className(translator: $this->translator); |
|
| 34 | 3 | } catch (Error $e) { |
|
| 35 | 3 | if ($e->getMessage() !== 'Unknown named parameter $translator') { |
|
| 36 | throw $e; |
||
| 37 | } |
||
| 38 | |||
| 39 | 3 | $classInstance = new $className(); |
|
| 40 | } |
||
| 41 | |||
| 42 | 101 | if (!$classInstance instanceof RuleHandlerInterface) { |
|
| 43 | 1 | throw new RuleHandlerInterfaceNotImplementedException($className); |
|
| 44 | } |
||
| 45 | |||
| 46 | 100 | return $this->instances[$className] = $classInstance; |
|
| 47 | } |
||
| 49 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: