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 |
||
25 | class TranslatableRepository extends Repository |
||
26 | { |
||
27 | /** |
||
28 | * @var LocaleContextInterface |
||
29 | */ |
||
30 | private $localeContext; |
||
31 | |||
32 | /** |
||
33 | * @var string[] |
||
34 | */ |
||
35 | private $cache; |
||
36 | |||
37 | /** |
||
38 | * @param EntityManager $em |
||
39 | * @param ClassMetadata $class |
||
40 | * @param ResourceInterface $resource |
||
41 | * @param LocaleContextInterface $localeContext |
||
42 | */ |
||
43 | 4 | public function __construct( |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 2 | public function createQueryBuilder($alias = null, $indexBy = null) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 1 | public function createQueryBuilderForCollection($alias = null, $indexBy = null) |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 3 | View Code Duplication | public function getProperty($property, $root = null) |
107 | |||
108 | /** |
||
109 | * @param QueryBuilder|string|null $root |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 3 | private function getTranslationAlias($root) |
|
117 | |||
118 | /** |
||
119 | * @param QueryBuilder|string|null $root |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 3 | private function getRootAlias($root) |
|
131 | |||
132 | /** |
||
133 | * @param ClassMetadata $metadata |
||
134 | * |
||
135 | * @return string[] |
||
136 | */ |
||
137 | 3 | private function getProperties(ClassMetadata $metadata) |
|
144 | |||
145 | /** |
||
146 | * @param string $property |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 3 | private function getRootProperty($property) |
|
154 | |||
155 | /** |
||
156 | * @return string[] |
||
157 | */ |
||
158 | 1 | View Code Duplication | private function getLocales() |
168 | } |
||
169 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.