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 |
||
20 | class CLI |
||
21 | { |
||
22 | 31 | public function mergeLongOpts(&$params, &$longopts) |
|
31 | |||
32 | 4 | public function usageHighlight() |
|
37 | |||
38 | 1 | public function getopt($opt, $long) |
|
42 | |||
43 | 15 | public function parseHighlight() |
|
44 | { |
||
45 | $longopts = array( |
||
46 | 15 | 'help', |
|
47 | 'query:', |
||
48 | 'format:', |
||
49 | 'ansi' |
||
50 | ); |
||
51 | 15 | $params = $this->getopt( |
|
52 | 15 | 'hq:f:a', |
|
53 | $longopts |
||
54 | ); |
||
55 | 15 | if ($params === false) { |
|
56 | 2 | return false; |
|
57 | } |
||
58 | 13 | $this->mergeLongOpts($params, $longopts); |
|
59 | 13 | if (! isset($params['f'])) { |
|
60 | 7 | $params['f'] = 'cli'; |
|
61 | } |
||
62 | 13 | if (! in_array($params['f'], array('html', 'cli', 'text'))) { |
|
63 | 2 | echo "ERROR: Invalid value for format!\n"; |
|
64 | |||
65 | 2 | return false; |
|
66 | } |
||
67 | |||
68 | 11 | return $params; |
|
69 | } |
||
70 | |||
71 | 15 | public function runHighlight() |
|
105 | |||
106 | 4 | public function usageLint() |
|
111 | |||
112 | 9 | View Code Duplication | public function parseLint() |
|
|||
113 | { |
||
114 | $longopts = array( |
||
115 | 9 | 'help', |
|
116 | 'query:', |
||
117 | 'context:', |
||
118 | 'ansi' |
||
119 | ); |
||
120 | 9 | $params = $this->getopt( |
|
121 | 9 | 'hq:c:a', |
|
122 | $longopts |
||
123 | ); |
||
124 | 9 | $this->mergeLongOpts($params, $longopts); |
|
125 | |||
126 | 9 | return $params; |
|
127 | } |
||
128 | |||
129 | 9 | public function runLint() |
|
130 | { |
||
131 | 9 | $params = $this->parseLint(); |
|
132 | 9 | if ($params === false) { |
|
133 | 2 | return 1; |
|
134 | } |
||
135 | 7 | if (isset($params['h'])) { |
|
136 | 2 | $this->usageLint(); |
|
137 | |||
138 | 2 | return 0; |
|
139 | } |
||
140 | 5 | if (isset($params['c'])) { |
|
141 | Context::load($params['c']); |
||
142 | } |
||
143 | 5 | View Code Duplication | if (!isset($params['q'])) { |
144 | 3 | if ($stdIn = $this->readStdin()) { |
|
145 | 1 | $params['q'] = $stdIn; |
|
146 | } |
||
147 | } |
||
148 | 5 | if (isset($params['a'])) { |
|
149 | Context::setMode('ANSI_QUOTES'); |
||
150 | } |
||
151 | |||
152 | 5 | if (isset($params['q'])) { |
|
153 | 3 | $lexer = new Lexer($params['q'], false); |
|
154 | 3 | $parser = new Parser($lexer->list); |
|
155 | 3 | $errors = Error::get(array($lexer, $parser)); |
|
156 | 3 | if (count($errors) === 0) { |
|
157 | 3 | return 0; |
|
158 | } |
||
159 | $output = Error::format($errors); |
||
160 | echo implode("\n", $output); |
||
161 | echo "\n"; |
||
162 | |||
163 | return 10; |
||
164 | } |
||
165 | 2 | echo "ERROR: Missing parameters!\n"; |
|
166 | 2 | $this->usageLint(); |
|
167 | |||
168 | 2 | return 1; |
|
169 | } |
||
170 | |||
171 | 4 | public function usageTokenize() |
|
176 | |||
177 | 9 | View Code Duplication | public function parseTokenize() |
178 | { |
||
179 | $longopts = array( |
||
180 | 9 | 'help', |
|
181 | 'query:', |
||
182 | 'ansi' |
||
183 | ); |
||
184 | 9 | $params = $this->getopt( |
|
185 | 9 | 'hq:a', |
|
186 | $longopts |
||
187 | ); |
||
188 | 9 | $this->mergeLongOpts($params, $longopts); |
|
189 | |||
190 | 9 | return $params; |
|
191 | } |
||
192 | |||
193 | 9 | public function runTokenize() |
|
235 | |||
236 | 3 | public function readStdin() { |
|
243 | } |
||
244 |
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.