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 |
||
16 | View Code Duplication | class EntityExists extends AbstractEntityValidator |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * Error constants |
||
20 | */ |
||
21 | const ERROR_NO_OBJECT_FOUND = 'noObjectFound'; |
||
22 | |||
23 | /** |
||
24 | * @var array Message templates |
||
25 | */ |
||
26 | protected $messageTemplates = array( |
||
27 | self::ERROR_NO_OBJECT_FOUND => "No object matching '%value%' was found", |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * Returns true when $this->entityService returns an entity |
||
32 | * when $this->method is called with the given criteria |
||
33 | * |
||
34 | * @param mixed $value |
||
35 | * @return boolean |
||
36 | */ |
||
37 | 12 | public function isValid($value) |
|
51 | } |
||
52 |
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.