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) |
105 | |||
106 | /** |
||
107 | * @param QueryBuilder|string|null $root |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 3 | private function getTranslationAlias($root) |
|
115 | |||
116 | /** |
||
117 | * @param QueryBuilder|string|null $root |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 3 | private function getRootAlias($root) |
|
129 | |||
130 | /** |
||
131 | * @return string[] |
||
132 | */ |
||
133 | 1 | View Code Duplication | private function getLocales() |
143 | } |
||
144 |
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.