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 |
||
10 | View Code Duplication | class ApplicationTester |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * @var Application $application |
||
14 | */ |
||
15 | private $application; |
||
16 | |||
17 | /** |
||
18 | * @var StringInput $input |
||
19 | */ |
||
20 | private $input; |
||
21 | |||
22 | /** |
||
23 | * @var StreamOutput $output |
||
24 | */ |
||
25 | private $output; |
||
26 | |||
27 | /** |
||
28 | * @var resource $inputStream |
||
29 | */ |
||
30 | private $inputStream; |
||
31 | |||
32 | /** |
||
33 | * @param Application $application |
||
34 | */ |
||
35 | public function __construct(Application $application) |
||
39 | |||
40 | /** |
||
41 | * @param string $input |
||
42 | * @param array $options |
||
43 | * |
||
44 | * @return integer |
||
45 | */ |
||
46 | public function run($input, array $options = array()) |
||
71 | |||
72 | /** |
||
73 | * @param boolean |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getDisplay($normalize = false) |
||
89 | |||
90 | /** |
||
91 | * @return InputInterface |
||
92 | */ |
||
93 | public function getInput() |
||
97 | |||
98 | /** |
||
99 | * @return OutputInterface |
||
100 | */ |
||
101 | public function getOutput() |
||
105 | |||
106 | /** |
||
107 | * @param string $input |
||
108 | */ |
||
109 | public function putToInputStream($input) |
||
113 | |||
114 | /** |
||
115 | * @return resource |
||
116 | */ |
||
117 | private function getInputStream() |
||
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.