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 |
||
19 | class CLI |
||
20 | { |
||
21 | 18 | public function mergeLongOpts(&$params, &$longopts) |
|
22 | { |
||
23 | 18 | foreach ($longopts as $value) { |
|
24 | 18 | $value = rtrim($value, ':'); |
|
25 | 18 | if (isset($params[$value])) { |
|
26 | 18 | $params[$value[0]] = $params[$value]; |
|
27 | } |
||
28 | } |
||
29 | 18 | } |
|
30 | |||
31 | 2 | public function usageHighlight() |
|
35 | |||
36 | 1 | public function getopt($opt, $long) |
|
37 | { |
||
38 | 1 | return getopt($opt, $long); |
|
39 | } |
||
40 | |||
41 | 8 | public function parseHighlight() |
|
42 | { |
||
43 | 8 | $longopts = array('help', 'query:', 'format:'); |
|
44 | 8 | $params = $this->getopt( |
|
45 | 8 | 'hq:f:', $longopts |
|
46 | ); |
||
47 | 8 | if ($params === false) { |
|
48 | 1 | return false; |
|
49 | } |
||
50 | 7 | $this->mergeLongOpts($params, $longopts); |
|
51 | 7 | if (!isset($params['f'])) { |
|
52 | 4 | $params['f'] = 'cli'; |
|
53 | } |
||
54 | 7 | if (!in_array($params['f'], array('html', 'cli', 'text'))) { |
|
55 | 1 | echo "ERROR: Invalid value for format!\n"; |
|
56 | |||
57 | 1 | return false; |
|
58 | } |
||
59 | |||
60 | 6 | return $params; |
|
61 | } |
||
62 | |||
63 | 8 | public function runHighlight() |
|
64 | { |
||
65 | 8 | $params = $this->parseHighlight(); |
|
66 | 8 | if ($params === false) { |
|
67 | 2 | return 1; |
|
68 | } |
||
69 | 6 | if (isset($params['h'])) { |
|
70 | 1 | $this->usageHighlight(); |
|
71 | |||
72 | 1 | return 0; |
|
73 | } |
||
74 | 5 | if (isset($params['q'])) { |
|
75 | 4 | echo Formatter::format( |
|
76 | 4 | $params['q'], array('type' => $params['f']) |
|
77 | ); |
||
78 | 4 | echo "\n"; |
|
79 | |||
80 | 4 | return 0; |
|
81 | } |
||
82 | 1 | echo "ERROR: Missing parameters!\n"; |
|
83 | 1 | $this->usageHighlight(); |
|
84 | |||
85 | 1 | return 1; |
|
86 | } |
||
87 | |||
88 | 2 | public function usageLint() |
|
92 | |||
93 | 6 | View Code Duplication | public function parseLint() |
|
|||
94 | { |
||
95 | 6 | $longopts = array('help', 'query:'); |
|
96 | 6 | $params = $this->getopt( |
|
97 | 6 | 'hq:', $longopts |
|
98 | ); |
||
99 | 6 | $this->mergeLongOpts($params, $longopts); |
|
100 | |||
101 | 6 | return $params; |
|
102 | } |
||
103 | |||
104 | 6 | public function runLint() |
|
133 | |||
134 | 2 | public function usageTokenize() |
|
135 | { |
||
136 | 2 | echo "Usage: tokenize-query --query SQL\n"; |
|
137 | 2 | } |
|
138 | |||
139 | 5 | View Code Duplication | public function parseTokenize() |
149 | |||
150 | 5 | public function runTokenize() |
|
183 | } |
||
184 |
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.