Innmind /
Reflection
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Innmind\Reflection\InjectionStrategy; |
||
| 4 | |||
| 5 | use Innmind\Immutable\TypedCollection; |
||
| 6 | use Innmind\Immutable\TypedCollectionInterface; |
||
| 7 | use Innmind\Reflection\Cache\StrategyCachingCapabilities; |
||
| 8 | use Innmind\Reflection\Exception\LogicException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * DefaultInjectionStrategies |
||
| 12 | * |
||
| 13 | * @author Hugues Maignol <[email protected]> |
||
| 14 | */ |
||
| 15 | final class DefaultInjectionStrategies implements InjectionStrategies |
||
| 16 | { |
||
| 17 | use StrategyCachingCapabilities; |
||
| 18 | |||
| 19 | private $strategies; |
||
| 20 | |||
| 21 | 8 | public function all(): TypedCollectionInterface |
|
| 22 | { |
||
| 23 | 8 | if ($this->strategies == null) { |
|
| 24 | 8 | return $this->strategies = new TypedCollection( |
|
| 25 | 8 | InjectionStrategyInterface::class, |
|
| 26 | [ |
||
| 27 | 8 | new SetterStrategy, |
|
| 28 | 8 | new NamedMethodStrategy, |
|
| 29 | 8 | new ReflectionStrategy, |
|
| 30 | ] |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | |||
| 34 | 2 | return $this->strategies; |
|
| 35 | } |
||
| 36 | |||
| 37 | 5 | View Code Duplication | public function get($object, string $key, $value): InjectionStrategyInterface |
|
0 ignored issues
–
show
|
|||
| 38 | { |
||
| 39 | 5 | $strategy = $this->getCachedStrategy(get_class($object), $key); |
|
| 40 | 5 | if (null !== $strategy) { |
|
| 41 | return $strategy; |
||
| 42 | } |
||
| 43 | |||
| 44 | 5 | foreach ($this->all() as $strategy) { |
|
| 45 | 5 | if ($strategy->supports($object, $key, $value)) { |
|
| 46 | |||
| 47 | 3 | $this->setCachedStrategy(get_class($object), $key, $strategy); |
|
| 48 | |||
| 49 | 5 | return $strategy; |
|
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | 2 | throw new LogicException( |
|
| 54 | sprintf( |
||
| 55 | 2 | 'Property "%s" cannot be injected', |
|
| 56 | $key |
||
| 57 | ) |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | } |
||
| 61 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.