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\InjectionStrategy; |
||
| 5 | |||
| 6 | use Innmind\Reflection\InjectionStrategyInterface; |
||
| 7 | use Innmind\Reflection\Exception\LogicException; |
||
| 8 | use Innmind\Immutable\StringPrimitive; |
||
| 9 | |||
| 10 | class SetterStrategy implements InjectionStrategyInterface |
||
| 11 | { |
||
| 12 | private $setter; |
||
| 13 | |||
| 14 | 4 | public function __construct() |
|
| 15 | { |
||
| 16 | 4 | $this->setter = new StringPrimitive('set%s'); |
|
| 17 | 4 | } |
|
| 18 | |||
| 19 | /** |
||
| 20 | * {@inheritdoc} |
||
| 21 | */ |
||
| 22 | 8 | public function supports($object, string $property, $value): bool |
|
| 23 | { |
||
| 24 | 8 | $refl = new \ReflectionObject($object); |
|
| 25 | |||
| 26 | 8 | return $refl->hasMethod( |
|
| 27 | 8 | (string) $this->setter->sprintf(ucfirst($property)) |
|
| 28 | ); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 4 | View Code Duplication | public function inject($object, string $property, $value) |
|
0 ignored issues
–
show
|
|||
| 35 | { |
||
| 36 | 4 | if (!$this->supports($object, $property, $value)) { |
|
| 37 | 1 | throw new LogicException; |
|
| 38 | } |
||
| 39 | |||
| 40 | 3 | $setter = (string) $this->setter->sprintf(ucfirst($property)); |
|
| 41 | 3 | $object->$setter($value); |
|
| 42 | 3 | } |
|
| 43 | } |
||
| 44 |
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.