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 DoctrineStorage implements StorageInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var EntityManagerInterface |
||
15 | */ |
||
16 | private $entityManager; |
||
17 | |||
18 | /** |
||
19 | * @var TaskRepository |
||
20 | */ |
||
21 | private $taskRepository; |
||
22 | |||
23 | 105 | public function __construct(EntityManagerInterface $entityManager, TaskRepository $taskRepository) |
|
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 102 | public function store(TaskInterface $task) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 6 | public function findScheduled() |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 3 | View Code Duplication | public function findAll($limit = null) |
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | View Code Duplication | public function findByKey($key, $limit = null) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 3 | public function persist(TaskInterface $task) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 6 | public function clear() |
|
116 | |||
117 | 78 | private function setTask(TaskEntity $entity, TaskInterface $task) |
|
125 | } |
||
126 |
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.