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 |
||
18 | View Code Duplication | class ProjectRepository |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * Entity alias. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | const ENTITY_ALIAS = 'p'; |
||
26 | |||
27 | /** |
||
28 | * Entity to use. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | const ENTITY_CLASS = 'AppBundle:Project'; |
||
33 | |||
34 | /** |
||
35 | * Object manager. |
||
36 | * |
||
37 | * @var ObjectManager |
||
38 | */ |
||
39 | private $objectManager; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param ObjectManager $objectManager |
||
45 | */ |
||
46 | public function __construct(ObjectManager $objectManager) |
||
50 | |||
51 | /** |
||
52 | * Find all projects. |
||
53 | * |
||
54 | * @param string $sortField |
||
55 | * @param string $sortOrder |
||
56 | * |
||
57 | * @return Project[] |
||
58 | */ |
||
59 | public function findAll($sortField = 'createdAt', $sortOrder = 'DESC') |
||
66 | |||
67 | /** |
||
68 | * Create a project. |
||
69 | * |
||
70 | * @param Project $project |
||
71 | */ |
||
72 | public function create(Project $project) |
||
77 | |||
78 | /** |
||
79 | * Update a project. |
||
80 | * |
||
81 | * @param Project $project |
||
82 | */ |
||
83 | public function update(Project $project) |
||
88 | |||
89 | /** |
||
90 | * Delete a project. |
||
91 | * |
||
92 | * @param Project $project |
||
93 | */ |
||
94 | public function delete(Project $project) |
||
99 | |||
100 | /** |
||
101 | * get QueryBuilder. |
||
102 | * |
||
103 | * @return QueryBuilder |
||
104 | */ |
||
105 | public function getQueryBuilder() |
||
111 | } |
||
112 |
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.