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 | $injectionStrategies = $injectionStrategies ?? InjectionStrategies::defaults(); |
|
29 | |||
30 | 5 | if ($injectionStrategies->getType() !== InjectionStrategyInterface::class) { |
|
31 | throw new InvalidArgumentException; |
||
32 | } |
||
33 | |||
34 | 5 | $this->class = $class; |
|
35 | 5 | $this->properties = $properties ?? new Collection([]); |
|
36 | 5 | $this->injectionStrategies = $injectionStrategies; |
|
37 | 5 | $this->instanciator = $instanciator ?? new ReflectionInstanciator; |
|
38 | 5 | } |
|
39 | |||
40 | /** |
||
41 | * Add a property to be injected in the new object |
||
42 | * |
||
43 | * @param string $property |
||
44 | * @param mixed $value |
||
45 | * |
||
46 | * @return self |
||
47 | */ |
||
48 | 2 | public function withProperty(string $property, $value): self |
|
49 | { |
||
50 | 2 | return new self( |
|
51 | 2 | $this->class, |
|
52 | 2 | $this->properties->set($property, $value), |
|
53 | 2 | $this->injectionStrategies, |
|
54 | 2 | $this->instanciator |
|
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Add a set of properties that need to be injected |
||
60 | * |
||
61 | * @param array $properties |
||
62 | * |
||
63 | * @return self |
||
64 | */ |
||
65 | 1 | public function withProperties(array $properties): self |
|
66 | { |
||
67 | 1 | return new self( |
|
68 | 1 | $this->class, |
|
69 | 1 | $this->properties->merge(new Collection($properties)), |
|
70 | 1 | $this->injectionStrategies, |
|
71 | 1 | $this->instanciator |
|
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return the collection of properties that will be injected in the object |
||
77 | * |
||
78 | * @return CollectionInterface |
||
79 | */ |
||
80 | 1 | public function getProperties(): CollectionInterface |
|
81 | { |
||
82 | 1 | return $this->properties; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Return the list of injection strategies used |
||
87 | * |
||
88 | * @return TypedCollectionInterface |
||
89 | */ |
||
90 | 1 | public function getInjectionStrategies(): TypedCollectionInterface |
|
91 | { |
||
92 | 1 | return $this->injectionStrategies; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return the object instanciator |
||
97 | * |
||
98 | * @return InstanciatorInterface |
||
99 | */ |
||
100 | 1 | public function getInstanciator(): InstanciatorInterface |
|
101 | { |
||
102 | 1 | return $this->instanciator; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return a new instance of the class |
||
107 | * |
||
108 | * @return object |
||
109 | */ |
||
110 | 2 | public function buildObject() |
|
111 | { |
||
112 | 2 | $object = $this->instanciator->build($this->class, $this->properties); |
|
113 | 2 | $parameters = $this->instanciator->getParameters($this->class); |
|
114 | |||
115 | //avoid injecting the properties already used in the constructor |
||
116 | $properties = $this |
||
117 | 2 | ->properties |
|
118 | 2 | ->filter(function($value, $property) use ($parameters) { |
|
119 | 1 | return !$parameters->contains($property); |
|
0 ignored issues
–
show
|
|||
120 | 2 | }); |
|
121 | 2 | $refl = new ReflectionObject( |
|
122 | $object, |
||
123 | $properties, |
||
124 | 2 | $this->injectionStrategies |
|
125 | ); |
||
126 | |||
127 | 2 | return $refl->buildObject(); |
|
128 | } |
||
129 | } |
||
130 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.