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 |
||
7 | class NetteTester extends Command |
||
8 | { |
||
9 | |||
10 | private $workingDir; |
||
11 | |||
12 | private $target; |
||
13 | |||
14 | private $options; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Returns working directory. System will switch to this directory before running tests |
||
19 | * @return string |
||
20 | */ |
||
21 | 2 | public function getWorkingDir() |
|
25 | |||
26 | |||
27 | /** |
||
28 | * Sets working directory. System will switch to this directory before running tests |
||
29 | * @param mixed $workingDir |
||
30 | */ |
||
31 | 2 | public function setWorkingDir($workingDir) |
|
35 | |||
36 | |||
37 | /** |
||
38 | * Returns target to be executed. Dir in working directory, dot (current dir), TestFile, etc |
||
39 | * @return mixed |
||
40 | */ |
||
41 | 1 | public function getTarget() |
|
45 | |||
46 | |||
47 | /** |
||
48 | * Sets target to be executed. Dir in working directory, dot (current dir), TestFile, etc |
||
49 | * @param mixed $target |
||
50 | */ |
||
51 | 2 | public function setTarget($target) |
|
55 | |||
56 | |||
57 | /** |
||
58 | * @return array|NULL |
||
59 | */ |
||
60 | 2 | public function getOptions() |
|
64 | |||
65 | |||
66 | /** |
||
67 | * Possible options: |
||
68 | * - executable (mandatory) |
||
69 | * - iniFile |
||
70 | * - interpreter |
||
71 | * - threads |
||
72 | * - mode |
||
73 | * @param array|NULL $options |
||
74 | */ |
||
75 | 2 | public function setOptions(array $options = NULL) |
|
79 | |||
80 | |||
81 | 2 | public function execute() |
|
122 | |||
123 | } |
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.