Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class AnnotationExtractor implements ExtractorInterface |
||
8 | { |
||
9 | /** @var Reader */ |
||
10 | protected $reader; |
||
11 | |||
12 | 7 | public function __construct() |
|
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 2 | public function getConstructorInjections($fqcn) |
|
21 | { |
||
22 | 2 | if (! in_array('__construct', (array)get_class_methods($fqcn))) { |
|
23 | 1 | return null; |
|
24 | } |
||
25 | |||
26 | 1 | return $this->reader->getMethodAnnotation( |
|
27 | 1 | new \ReflectionMethod($fqcn, '__construct'), |
|
28 | AnnotationInterface::class |
||
29 | 1 | ); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 2 | View Code Duplication | public function getMethodsInjections($fqcn) |
36 | { |
||
37 | 2 | $injections = []; |
|
38 | |||
39 | 2 | $reflection = new \ReflectionClass($fqcn); |
|
40 | |||
41 | 2 | foreach ($reflection->getMethods() as $method) { |
|
42 | 2 | $name = $method->getName(); |
|
43 | 2 | if ('__construct' === $name) { |
|
44 | 1 | continue; |
|
45 | } |
||
46 | |||
47 | 1 | $inject = $this->reader->getMethodAnnotation( |
|
48 | 1 | new \ReflectionMethod($fqcn, $name), |
|
49 | AnnotationInterface::class |
||
50 | 1 | ); |
|
51 | |||
52 | 1 | if (null !== $inject) { |
|
53 | 1 | $injections[$name] = $inject; |
|
54 | 1 | } |
|
55 | 2 | } |
|
56 | |||
57 | 2 | return $injections; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 1 | View Code Duplication | public function getPropertiesInjections($fqcn) |
64 | { |
||
65 | 1 | $injections = []; |
|
66 | 1 | $reflection = new \ReflectionClass($fqcn); |
|
67 | |||
68 | 1 | foreach ($reflection->getProperties() as $property) { |
|
69 | 1 | $reflectionProperty = new \ReflectionProperty($fqcn, $property->getName()); |
|
70 | 1 | $inject = $this->reader->getPropertyAnnotation($reflectionProperty, AnnotationInterface::class); |
|
71 | |||
72 | 1 | if (null !== $inject) { |
|
73 | 1 | $injections[$property->getName()] = $inject; |
|
74 | 1 | } |
|
75 | 1 | } |
|
76 | |||
77 | 1 | return $injections; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 1 | public function getChangeSet($fqcn) |
|
87 | |||
88 | /** |
||
89 | * @param Reader $reader |
||
90 | */ |
||
91 | 1 | public function setReader(Reader $reader) |
|
95 | } |
||
96 |