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 |
||
11 | class NodeRepository extends EntityRepository |
||
12 | { |
||
13 | /** |
||
14 | * @param $locale |
||
15 | * @return array |
||
16 | */ |
||
17 | View Code Duplication | public function findAllWithLocale($locale) |
|
28 | |||
29 | /** |
||
30 | * @param $slug |
||
31 | * @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
||
32 | */ |
||
33 | public function findOnePublishedBySlug($slug) |
||
43 | |||
44 | /** |
||
45 | * @param $slug |
||
46 | * @param $locale |
||
47 | * @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
||
48 | */ |
||
49 | View Code Duplication | public function findOnePublishedBySlugAndLocale($slug, $locale) |
|
61 | |||
62 | /** |
||
63 | * @param $slug |
||
64 | * @param $locale |
||
65 | * @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
||
66 | */ |
||
67 | public function findOneBySlugAndLocale($slug, $locale) |
||
78 | |||
79 | /** |
||
80 | * @param \Alpixel\Bundle\CMSBundle\Entity\Node $node |
||
81 | * @param $locale |
||
82 | * @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
||
83 | */ |
||
84 | View Code Duplication | public function findTranslation(Node $node, $locale) |
|
112 | |||
113 | /** |
||
114 | * @param \Alpixel\Bundle\CMSBundle\Entity\Node $node |
||
115 | * @return array |
||
116 | */ |
||
117 | public function findTranslations(Node $node) |
||
142 | } |
||
143 |
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.