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