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 | |||
| 9 | /** |
||
| 10 | * Looks for a method named exactly like the property |
||
| 11 | * |
||
| 12 | * Example: |
||
| 13 | * <code> |
||
| 14 | * private $foo; |
||
| 15 | * |
||
| 16 | * public function foo($foo); |
||
| 17 | * </code> |
||
| 18 | */ |
||
| 19 | View Code Duplication | class NamedMethodStrategy implements InjectionStrategyInterface |
|
|
0 ignored issues
–
show
|
|||
| 20 | { |
||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | 8 | public function supports($object, string $property, $value): bool |
|
| 25 | { |
||
| 26 | 8 | $refl = new \ReflectionObject($object); |
|
| 27 | |||
| 28 | 8 | return $refl->hasMethod($property) && |
|
| 29 | 8 | $refl->getMethod($property)->getNumberOfParameters() > 0; |
|
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | 4 | public function inject($object, string $property, $value) |
|
| 36 | { |
||
| 37 | 4 | if (!$this->supports($object, $property, $value)) { |
|
| 38 | 1 | throw new LogicException; |
|
| 39 | } |
||
| 40 | |||
| 41 | 3 | $object->$property($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.