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 |
||
35 | abstract class AbstractDriver implements FactoryInterface |
||
36 | { |
||
37 | /** @var ReflectionClass */ |
||
38 | protected $reflector; |
||
39 | |||
40 | /** @var array */ |
||
41 | protected $parsed = []; |
||
42 | |||
43 | /** @var BuilderFactory */ |
||
44 | protected $builder; |
||
45 | |||
46 | /** @var */ |
||
47 | protected $annotation; |
||
48 | |||
49 | /** @var ReflectionProperty */ |
||
50 | protected $property; |
||
51 | |||
52 | /** @var ReflectionMethod */ |
||
53 | protected $method; |
||
54 | |||
55 | /** |
||
56 | * @param array $parsed |
||
57 | * @param BuilderFactory $builder |
||
58 | */ |
||
59 | public function __construct(array $parsed, BuilderFactory $builder) |
||
64 | |||
65 | /** |
||
66 | * set ReflectionClass. |
||
67 | * |
||
68 | * @param ReflectionClass $reflection |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function setReflector(ReflectionClass $reflection): AbstractDriver |
||
78 | |||
79 | /** |
||
80 | * @param $annotation |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | public function setAnnotationInstance($annotation): AbstractDriver |
||
90 | |||
91 | /** |
||
92 | * @param ReflectionProperty $name |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function setProperty(ReflectionProperty $name): AbstractDriver |
||
102 | |||
103 | /** |
||
104 | * @param Class_ $part |
||
105 | */ |
||
106 | protected function removeConstructor(Class_ $part) |
||
112 | |||
113 | /** |
||
114 | * @param Class_ $part |
||
115 | * @param string $name |
||
116 | */ |
||
117 | protected function removeMethod(Class_ $part, string $name) |
||
127 | |||
128 | /** |
||
129 | * detect constructor access level. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | protected function setAccessLevel(): string |
||
144 | } |
||
145 |
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.