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 |
||
25 | class LazyDouble |
||
26 | { |
||
27 | private $doubler; |
||
28 | private $class; |
||
29 | private $interfaces = array(); |
||
30 | private $arguments = null; |
||
31 | private $double; |
||
32 | |||
33 | /** |
||
34 | * Initializes lazy double. |
||
35 | * |
||
36 | * @param Doubler $doubler |
||
37 | */ |
||
38 | public function __construct(Doubler $doubler) |
||
42 | |||
43 | /** |
||
44 | * Tells doubler to use specific class as parent one for double. |
||
45 | * |
||
46 | * @param string|ReflectionClass $class |
||
47 | * |
||
48 | * @throws \Prophecy\Exception\Doubler\ClassNotFoundException |
||
49 | * @throws \Prophecy\Exception\Doubler\DoubleException |
||
50 | */ |
||
51 | View Code Duplication | public function setParentClass($class) |
|
67 | |||
68 | /** |
||
69 | * Tells doubler to implement specific interface with double. |
||
70 | * |
||
71 | * @param string|ReflectionClass $interface |
||
72 | * |
||
73 | * @throws \Prophecy\Exception\Doubler\InterfaceNotFoundException |
||
74 | * @throws \Prophecy\Exception\Doubler\DoubleException |
||
75 | */ |
||
76 | View Code Duplication | public function addInterface($interface) |
|
97 | |||
98 | /** |
||
99 | * Sets constructor arguments. |
||
100 | * |
||
101 | * @param array $arguments |
||
102 | */ |
||
103 | public function setArguments(array $arguments = null) |
||
107 | |||
108 | /** |
||
109 | * Creates double instance or returns already created one. |
||
110 | * |
||
111 | * @return DoubleInterface |
||
112 | */ |
||
113 | public function getInstance() |
||
127 | } |
||
128 |
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.