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 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Innmind\Reflection; |
||
| 5 | |||
| 6 | use Innmind\Immutable\Collection; |
||
| 7 | use Innmind\Immutable\CollectionInterface; |
||
| 8 | use Innmind\Reflection\Exception\InvalidArgumentException; |
||
| 9 | use Innmind\Reflection\ExtractionStrategy\DefaultExtractionStrategies; |
||
| 10 | use Innmind\Reflection\ExtractionStrategy\ExtractionStrategies; |
||
| 11 | use Innmind\Reflection\InjectionStrategy\DefaultInjectionStrategies; |
||
| 12 | use Innmind\Reflection\InjectionStrategy\InjectionStrategies; |
||
| 13 | |||
| 14 | class ReflectionObject |
||
| 15 | { |
||
| 16 | private $object; |
||
| 17 | private $properties; |
||
| 18 | private $injectionStrategies; |
||
| 19 | private $extractionStrategies; |
||
| 20 | |||
| 21 | 14 | public function __construct( |
|
| 22 | $object, |
||
| 23 | CollectionInterface $properties = null, |
||
| 24 | InjectionStrategies $injectionStrategies = null, |
||
| 25 | ExtractionStrategies $extractionStrategies = null |
||
| 26 | ) { |
||
| 27 | 14 | if (!is_object($object)) { |
|
| 28 | 1 | throw new InvalidArgumentException; |
|
| 29 | } |
||
| 30 | |||
| 31 | 13 | $this->injectionStrategies = $injectionStrategies ?? new DefaultInjectionStrategies(); |
|
| 32 | 13 | $this->extractionStrategies = $extractionStrategies ?? new DefaultExtractionStrategies(); |
|
| 33 | |||
| 34 | 13 | $this->object = $object; |
|
| 35 | |||
| 36 | 13 | $this->properties = $properties ?? new Collection([]); |
|
| 37 | 13 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Add a property that will be injected |
||
| 41 | * |
||
| 42 | * @param string $name |
||
| 43 | * @param mixed $value |
||
| 44 | * |
||
| 45 | * @return self |
||
| 46 | */ |
||
| 47 | 4 | public function withProperty(string $name, $value) |
|
| 48 | { |
||
| 49 | 4 | return new self( |
|
| 50 | 4 | $this->object, |
|
| 51 | 4 | $this->properties->set($name, $value), |
|
| 52 | 4 | $this->injectionStrategies, |
|
| 53 | 4 | $this->extractionStrategies |
|
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Add a set of properties that need to be injected |
||
| 59 | * |
||
| 60 | * @param array $properties |
||
| 61 | * |
||
| 62 | * @return self |
||
| 63 | */ |
||
| 64 | 1 | public function withProperties(array $properties): self |
|
| 65 | { |
||
| 66 | 1 | return new self( |
|
| 67 | 1 | $this->object, |
|
| 68 | 1 | $this->properties->merge(new Collection($properties)), |
|
|
0 ignored issues
–
show
|
|||
| 69 | 1 | $this->injectionStrategies, |
|
| 70 | 1 | $this->extractionStrategies |
|
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return the collection of properties that will be injected in the object |
||
| 76 | * |
||
| 77 | * @return CollectionInterface |
||
| 78 | */ |
||
| 79 | 2 | public function getProperties(): CollectionInterface |
|
| 80 | { |
||
| 81 | 2 | return $this->properties; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return the list of injection strategies used |
||
| 86 | * |
||
| 87 | * @return InjectionStrategies |
||
| 88 | */ |
||
| 89 | 1 | public function getInjectionStrategies(): InjectionStrategies |
|
| 90 | { |
||
| 91 | 1 | return $this->injectionStrategies; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Return the list of extraction strategies used |
||
| 96 | * |
||
| 97 | * @return ExtractionStrategies |
||
| 98 | */ |
||
| 99 | 2 | public function getExtractionStrategies(): ExtractionStrategies |
|
| 100 | { |
||
| 101 | 2 | return $this->extractionStrategies; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return the object with the list of properties set on it |
||
| 106 | * |
||
| 107 | * @return object |
||
| 108 | */ |
||
| 109 | 7 | public function buildObject() |
|
| 110 | { |
||
| 111 | 7 | foreach ($this->properties as $key => $value) { |
|
| 112 | 5 | $this->inject($key, $value); |
|
| 113 | } |
||
| 114 | |||
| 115 | 5 | return $this->object; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Extract the given list of properties |
||
| 120 | * |
||
| 121 | * @param array $properties |
||
| 122 | * |
||
| 123 | * @return CollectionInterface |
||
| 124 | */ |
||
| 125 | 3 | public function extract(array $properties): CollectionInterface |
|
| 126 | { |
||
| 127 | 3 | $values = []; |
|
| 128 | |||
| 129 | 3 | foreach ($properties as $property) { |
|
| 130 | 3 | $values[$property] = $this->extractProperty($property); |
|
| 131 | } |
||
| 132 | |||
| 133 | 2 | return new Collection($values); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Inject the given key/value pair into the object |
||
| 138 | * |
||
| 139 | * @param string $key |
||
| 140 | * @param mixed $value |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | 5 | private function inject(string $key, $value) |
|
| 145 | { |
||
| 146 | $this |
||
| 147 | 5 | ->injectionStrategies |
|
| 148 | 5 | ->get($this->object, $key, $value) |
|
| 149 | 3 | ->inject($this->object, $key, $value); |
|
| 150 | 3 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Extract the given property out of the object |
||
| 154 | * |
||
| 155 | * @param string $property |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | 3 | private function extractProperty(string $property) |
|
| 160 | { |
||
| 161 | return $this |
||
| 162 | 3 | ->extractionStrategies |
|
| 163 | 3 | ->get($this->object, $property) |
|
| 164 | 2 | ->extract($this->object, $property); |
|
| 165 | } |
||
| 166 | } |
||
| 167 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: