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 Compiler |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $path; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $files = array(); |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $index = array(); |
||
36 | |||
37 | /** |
||
38 | * Creates a Compiler instance. |
||
39 | * |
||
40 | * @param string $path The root path of the project |
||
41 | * @throws \LogicException if the creation of Phar archives is disabled in php.ini. |
||
42 | */ |
||
43 | public function __construct($path) |
||
51 | |||
52 | /** |
||
53 | * Compiles all files into a single PHAR file. |
||
54 | * |
||
55 | * @param string $outputFile The full name of the file to create |
||
56 | * @throws \LogicException if no index files are defined. |
||
57 | */ |
||
58 | public function compile($outputFile) |
||
101 | |||
102 | /** |
||
103 | * Gets the root path of the project. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getPath() |
||
111 | |||
112 | /** |
||
113 | * Gets list of all added files. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getFiles() |
||
121 | |||
122 | /** |
||
123 | * Adds a file. |
||
124 | * |
||
125 | * @param string $file The name of the file relative to the project root |
||
126 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
127 | */ |
||
128 | public function addFile($file, $strip = true) |
||
133 | |||
134 | /** |
||
135 | * Adds files of the given directory recursively. |
||
136 | * |
||
137 | * @param string $directory The name of the directory relative to the project root |
||
138 | * @param string|array $exclude List of file name patterns to exclude (optional) |
||
139 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
140 | */ |
||
141 | public function addDirectory($directory, $exclude = null, $strip = true) |
||
167 | |||
168 | /** |
||
169 | * Gets list of defined index files. |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getIndexFiles() |
||
177 | |||
178 | /** |
||
179 | * Adds an index file. |
||
180 | * |
||
181 | * @param string $file The name of the file relative to the project root |
||
182 | * @param string $type The SAPI type (Default: 'cli') |
||
183 | */ |
||
184 | public function addIndexFile($file, $type = 'cli') |
||
194 | |||
195 | /** |
||
196 | * Gets list of all supported SAPIs. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public function getSupportedSapis() |
||
204 | |||
205 | /** |
||
206 | * Returns whether the compiled program will support the given SAPI type. |
||
207 | * |
||
208 | * @param string $sapi The SAPI type |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function supportsSapi($sapi) |
||
215 | |||
216 | /** |
||
217 | * Generates the stub. |
||
218 | * |
||
219 | * @param string $name The internal Phar name |
||
220 | * @return string |
||
221 | */ |
||
222 | protected function generateStub($name) |
||
249 | |||
250 | /** |
||
251 | * Matches the given path. |
||
252 | * |
||
253 | * @param string $path |
||
254 | * @param string $pattern |
||
255 | * @return bool |
||
256 | */ |
||
257 | protected function match($path, $pattern) |
||
268 | |||
269 | /** |
||
270 | * Filters the given path. |
||
271 | * |
||
272 | * @param string $path |
||
273 | * @param array $patterns |
||
274 | * @return bool |
||
275 | */ |
||
276 | protected function filter($path, array $patterns) |
||
286 | |||
287 | /** |
||
288 | * Removes whitespace from a PHP source string while preserving line numbers. |
||
289 | * |
||
290 | * @param string $source A PHP string |
||
291 | * @return string The PHP string with the whitespace removed |
||
292 | */ |
||
293 | protected function stripWhitespace($source) |
||
320 | } |
||
321 |