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 |
||
20 | final class AnnotationCollectionFactory |
||
21 | { |
||
22 | /** |
||
23 | * Doctrine annotation reader |
||
24 | * |
||
25 | * @var Reader |
||
26 | */ |
||
27 | private $reader; |
||
28 | |||
29 | /** |
||
30 | * Cache for collection of annotations |
||
31 | * |
||
32 | * @var Cache |
||
33 | */ |
||
34 | private $cache; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param Reader $reader |
||
40 | * @param Cache $cache |
||
41 | */ |
||
42 | 10 | public function __construct(Reader $reader, Cache $cache) |
|
47 | |||
48 | /** |
||
49 | * Create a set of property annotations |
||
50 | * |
||
51 | * @param string $className |
||
52 | * @param string $propertyName |
||
53 | * @return AnnotationSet |
||
54 | * @throws \InvalidArgumentException If the type does not exist |
||
55 | */ |
||
56 | 3 | View Code Duplication | public function createPropertyAnnotations(string $className, string $propertyName): AnnotationSet |
|
|||
57 | { |
||
58 | 3 | $key = $className.':'.$propertyName; |
|
59 | 3 | if ($this->cache->contains($key)) { |
|
60 | 1 | return $this->cache->fetch($key); |
|
61 | } |
||
62 | |||
63 | 2 | $reflectionProperty = new ReflectionProperty($className, $propertyName); |
|
64 | |||
65 | 2 | $annotations = new AnnotationSet(); |
|
66 | |||
67 | // start with with all property annotations |
||
68 | 2 | foreach ($this->reader->getPropertyAnnotations($reflectionProperty) as $defaultAnnotation) { |
|
69 | 2 | $annotations->addAnnotation($defaultAnnotation, AnnotationSet::TYPE_PROPERTY); |
|
70 | } |
||
71 | |||
72 | 2 | $reflectionClass = $reflectionProperty->getDeclaringClass(); |
|
73 | 2 | $parentClass = $reflectionClass->getParentClass(); |
|
74 | |||
75 | 2 | while (false !== $parentClass) { |
|
76 | // add parent property annotations if they exist |
||
77 | 1 | if ($parentClass->hasProperty($reflectionProperty->getName())) { |
|
78 | 1 | $parentProperty = $parentClass->getProperty($reflectionProperty->getName()); |
|
79 | 1 | foreach ($this->reader->getPropertyAnnotations($parentProperty) as $parentPropertyAnnotation) { |
|
80 | 1 | $annotations->addAnnotation($parentPropertyAnnotation, AnnotationSet::TYPE_PROPERTY); |
|
81 | } |
||
82 | } |
||
83 | |||
84 | // reset $parentClass |
||
85 | 1 | $parentClass = $parentClass->getParentClass(); |
|
86 | } |
||
87 | |||
88 | 2 | $this->cache->save($key, $annotations); |
|
89 | |||
90 | 2 | return $annotations; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Create a set of class annotations |
||
95 | * |
||
96 | * @param string $className |
||
97 | * @return AnnotationSet |
||
98 | * @throws \InvalidArgumentException If the type does not exist |
||
99 | */ |
||
100 | 3 | public function createClassAnnotations(string $className): AnnotationSet |
|
125 | |||
126 | /** |
||
127 | * Create a set of method annotations |
||
128 | * |
||
129 | * @param string $className |
||
130 | * @param string $methodName |
||
131 | * @return AnnotationSet |
||
132 | * @throws \InvalidArgumentException If the type does not exist |
||
133 | */ |
||
134 | 3 | View Code Duplication | public function createMethodAnnotations(string $className, string $methodName): AnnotationSet |
166 | } |
||
167 |
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.