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