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 |
||
17 | View Code Duplication | abstract class AbstractDefinitionSource implements DefinitionSourceInterface |
|
|
|||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $definitions = []; |
||
23 | |||
24 | /** |
||
25 | * DefinitionSource constructor. |
||
26 | * |
||
27 | * @param array $definitions |
||
28 | */ |
||
29 | public function __construct(array $definitions) |
||
33 | |||
34 | /** |
||
35 | * Add definitions to the DI. |
||
36 | * |
||
37 | * @param array $definitions |
||
38 | */ |
||
39 | public function add(array $definitions) |
||
43 | |||
44 | /** |
||
45 | * Returns true if the DI can return an entry for the given name. |
||
46 | * Returns false otherwise. |
||
47 | * |
||
48 | * @param $name |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function has($name) |
||
60 | |||
61 | /** |
||
62 | * Set the DI definition for the entry name. |
||
63 | * |
||
64 | * @param $name |
||
65 | * @param $value |
||
66 | */ |
||
67 | public function set($name, $value) |
||
71 | } |
||
72 |
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.