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 |
||
22 | class TaskRepository extends EntityRepository implements TaskRepositoryInterface |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | 10 | public function create($handlerClass, $workload = null) |
|
28 | { |
||
29 | 10 | return new Task($handlerClass, $workload); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function findByUuid($uuid) |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 24 | public function save(TaskInterface $task) |
|
44 | { |
||
45 | 24 | $this->_em->persist($task); |
|
46 | 24 | $this->_em->flush($task); |
|
47 | |||
48 | 24 | return $this; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function remove(TaskInterface $task) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 5 | View Code Duplication | public function findAll($page = 1, $pageSize = null) |
|
|||
66 | { |
||
67 | 5 | $query = $this->createQueryBuilder('t') |
|
68 | 5 | ->getQuery(); |
|
69 | |||
70 | 5 | if ($pageSize) { |
|
71 | $query->setMaxResults($pageSize); |
||
72 | $query->setFirstResult(($page - 1) * $pageSize); |
||
73 | } |
||
74 | |||
75 | 5 | return $query->getResult(); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 2 | public function findEndBeforeNow() |
|
85 | |||
86 | /** |
||
87 | * Returns task where last-execution is before given date-time. |
||
88 | * |
||
89 | * @param \DateTime $dateTime |
||
90 | * |
||
91 | * @return TaskInterface[] |
||
92 | */ |
||
93 | 2 | public function findEndBefore(\DateTime $dateTime) |
|
101 | |||
102 | /** |
||
103 | * Returns task identified by system-key. |
||
104 | * |
||
105 | * @param string $systemKey |
||
106 | * |
||
107 | * @return TaskInterface |
||
108 | */ |
||
109 | 3 | public function findBySystemKey($systemKey) |
|
121 | |||
122 | /** |
||
123 | * Returns all system-task. |
||
124 | * |
||
125 | * @return TaskInterface[] |
||
126 | */ |
||
127 | 2 | public function findSystemTasks() |
|
134 | } |
||
135 |
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.