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 |
||
12 | class Updater implements UpdaterInterface |
||
13 | { |
||
14 | use ErrorAwareTrait; |
||
15 | |||
16 | /** |
||
17 | * @var ValidatorInterface |
||
18 | */ |
||
19 | protected $validator; |
||
20 | |||
21 | /** |
||
22 | * @var RepositoryInterface |
||
23 | */ |
||
24 | protected $repository; |
||
25 | |||
26 | /** |
||
27 | * @var EventManagerInterface |
||
28 | */ |
||
29 | protected $eventManager; |
||
30 | |||
31 | /** |
||
32 | * @param ValidatorInterface $validator |
||
33 | * @param RepositoryInterface $repository |
||
34 | */ |
||
35 | View Code Duplication | public function __construct( |
|
45 | |||
46 | /** |
||
47 | * @param mixed $id |
||
48 | * @param array $data |
||
49 | * @return EntityInterface|null |
||
50 | */ |
||
51 | public function update($id, array $data) |
||
83 | } |
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.