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