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 |
||
24 | class TranslatableRepository extends Repository |
||
25 | { |
||
26 | /** |
||
27 | * @var LocaleContextInterface |
||
28 | */ |
||
29 | private $localeContext; |
||
30 | |||
31 | /** |
||
32 | * @var string[] |
||
33 | */ |
||
34 | private $cache; |
||
35 | |||
36 | /** |
||
37 | * @param DocumentManager $dm |
||
38 | * @param UnitOfWork $uow |
||
39 | * @param ClassMetadata $class |
||
40 | * @param ResourceInterface $resource |
||
41 | * @param LocaleContextInterface $localeContext |
||
42 | */ |
||
43 | 2 | public function __construct( |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 1 | public function createQueryBuilderForCollection() |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 1 | View Code Duplication | public function getProperty($property, $root = null) |
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | private function getTranslationAlias() |
|
93 | |||
94 | /** |
||
95 | * @param ClassMetadata $metadata |
||
96 | * |
||
97 | * @return string[] |
||
98 | */ |
||
99 | 1 | private function getProperties(ClassMetadata $metadata) |
|
108 | |||
109 | /** |
||
110 | * @param string $property |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 1 | private function getRootProperty($property) |
|
118 | |||
119 | /** |
||
120 | * @return string[] |
||
121 | */ |
||
122 | 1 | View Code Duplication | private function getLocales() |
132 | } |
||
133 |
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.