These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Reflection; |
||
5 | |||
6 | use Innmind\Reflection\InjectionStrategy\SetterStrategy; |
||
7 | use Innmind\Reflection\InjectionStrategy\NamedMethodStrategy; |
||
8 | use Innmind\Reflection\InjectionStrategy\ReflectionStrategy; |
||
9 | use Innmind\Reflection\Instanciator\ReflectionInstanciator; |
||
10 | use Innmind\Immutable\Collection; |
||
11 | use Innmind\Immutable\CollectionInterface; |
||
12 | use Innmind\Immutable\TypedCollection; |
||
13 | use Innmind\Immutable\TypedCollectionInterface; |
||
14 | |||
15 | class ReflectionClass |
||
16 | { |
||
17 | private $class; |
||
18 | private $properties; |
||
19 | private $injectionStrategies; |
||
20 | private $instanciator; |
||
21 | |||
22 | 5 | public function __construct( |
|
23 | string $class, |
||
24 | CollectionInterface $properties = null, |
||
25 | TypedCollectionInterface $injectionStrategies = null, |
||
26 | InstanciatorInterface $instanciator = null |
||
27 | ) { |
||
28 | 5 | $this->class = $class; |
|
29 | 5 | $this->properties = $properties ?? new Collection([]); |
|
30 | 5 | $this->initInjectionStrategies($injectionStrategies); |
|
31 | 5 | $this->instanciator = $instanciator ?? new ReflectionInstanciator; |
|
32 | 5 | } |
|
33 | |||
34 | /** |
||
35 | * Add a property to be injected in the new object |
||
36 | * |
||
37 | * @param string $property |
||
38 | * @param mixed $value |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | 2 | public function withProperty(string $property, $value): self |
|
43 | { |
||
44 | 2 | return new self( |
|
45 | 2 | $this->class, |
|
46 | 2 | $this->properties->set($property, $value), |
|
47 | 2 | $this->injectionStrategies, |
|
48 | 2 | $this->instanciator |
|
49 | ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Add a set of properties that need to be injected |
||
54 | * |
||
55 | * @param array $properties |
||
56 | * |
||
57 | * @return self |
||
58 | */ |
||
59 | 1 | public function withProperties(array $properties): self |
|
60 | { |
||
61 | 1 | return new self( |
|
62 | 1 | $this->class, |
|
63 | 1 | $this->properties->merge(new Collection($properties)), |
|
64 | 1 | $this->injectionStrategies, |
|
65 | 1 | $this->instanciator |
|
66 | ); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Return the collection of properties that will be injected in the object |
||
71 | * |
||
72 | * @return CollectionInterface |
||
73 | */ |
||
74 | 1 | public function getProperties(): CollectionInterface |
|
75 | { |
||
76 | 1 | return $this->properties; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Return the list of injection strategies used |
||
81 | * |
||
82 | * @return TypedCollectionInterface |
||
83 | */ |
||
84 | 1 | public function getInjectionStrategies(): TypedCollectionInterface |
|
85 | { |
||
86 | 1 | return $this->injectionStrategies; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Return the object instanciator |
||
91 | * |
||
92 | * @return InstanciatorInterface |
||
93 | */ |
||
94 | 1 | public function getInstanciator(): InstanciatorInterface |
|
95 | { |
||
96 | 1 | return $this->instanciator; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Return a new instance of the class |
||
101 | * |
||
102 | * @return object |
||
103 | */ |
||
104 | 2 | public function buildObject() |
|
105 | { |
||
106 | 2 | $object = $this->instanciator->build($this->class, $this->properties); |
|
107 | 2 | $parameters = $this->instanciator->getParameters($this->class); |
|
108 | |||
109 | //avoid injecting the properties already used in the constructor |
||
110 | $properties = $this |
||
111 | 2 | ->properties |
|
112 | 2 | ->filter(function ($value, $property) use ($parameters) { |
|
113 | 1 | return !$parameters->contains($property); |
|
114 | 2 | }); |
|
115 | 2 | $refl = new ReflectionObject( |
|
116 | $object, |
||
117 | $properties, |
||
118 | 2 | $this->injectionStrategies |
|
119 | ); |
||
120 | |||
121 | 2 | return $refl->buildObject(); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param TypedCollectionInterface $strategies |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 5 | View Code Duplication | private function initInjectionStrategies(TypedCollectionInterface $strategies = null) |
0 ignored issues
–
show
|
|||
130 | { |
||
131 | 5 | $strategies = $strategies ?? new TypedCollection( |
|
132 | 5 | InjectionStrategyInterface::class, |
|
133 | [ |
||
134 | 5 | new SetterStrategy, |
|
135 | 5 | new NamedMethodStrategy, |
|
136 | 5 | new ReflectionStrategy, |
|
137 | ] |
||
138 | ); |
||
139 | |||
140 | 5 | if ($strategies->getType() !== InjectionStrategyInterface::class) { |
|
141 | throw new InvalidArgumentException; |
||
142 | } |
||
143 | |||
144 | 5 | $this->injectionStrategies = $strategies; |
|
145 | 5 | } |
|
146 | } |
||
147 |
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.