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 |
||
33 | class Compiler |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $path; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $files = array(); |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $index = array(); |
||
49 | |||
50 | /** |
||
51 | * Creates a Compiler instance. |
||
52 | * |
||
53 | * @param string $path The root path of the project |
||
54 | * @throws \LogicException if the creation of Phar archives is disabled in php.ini. |
||
55 | */ |
||
56 | public function __construct($path) |
||
64 | |||
65 | /** |
||
66 | * Compiles all files into a single PHAR file. |
||
67 | * |
||
68 | * @param string $outputfile The full name of the file to create |
||
69 | * @throws \LogicException if no index files are defined. |
||
70 | */ |
||
71 | public function compile($outputfile) |
||
114 | |||
115 | /** |
||
116 | * Gets the root path of the project. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getPath() |
||
124 | |||
125 | /** |
||
126 | * Gets list of all added files. |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function getFiles() |
||
134 | |||
135 | /** |
||
136 | * Adds a file. |
||
137 | * |
||
138 | * @param string $file The name of the file relative to the project root |
||
139 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
140 | */ |
||
141 | public function addFile($file, $strip = true) |
||
146 | |||
147 | /** |
||
148 | * Adds files of the given directory recursively. |
||
149 | * |
||
150 | * @param string $directory The name of the directory relative to the project root |
||
151 | * @param string|array $exclude List of file name patterns to exclude (optional) |
||
152 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
153 | */ |
||
154 | public function addDirectory($directory, $exclude = null, $strip = true) |
||
177 | |||
178 | /** |
||
179 | * Gets list of defined index files. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getIndexFiles() |
||
187 | |||
188 | /** |
||
189 | * Adds an index file. |
||
190 | * |
||
191 | * @param string $file The name of the file relative to the project root |
||
192 | * @param string $type The SAPI type (Default: 'cli') |
||
193 | */ |
||
194 | public function addIndexFile($file, $type = 'cli') |
||
204 | |||
205 | /** |
||
206 | * Gets list of all supported SAPIs. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getSupportedSapis() |
||
214 | |||
215 | /** |
||
216 | * Returns whether the compiled program will support the given SAPI type. |
||
217 | * |
||
218 | * @param string $sapi The SAPI type |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function supportsSapi($sapi) |
||
225 | |||
226 | /** |
||
227 | * Generates the stub. |
||
228 | * |
||
229 | * @param string $name The internal Phar name |
||
230 | * @return string |
||
231 | */ |
||
232 | protected function generateStub($name) |
||
259 | |||
260 | /** |
||
261 | * Filters the given path. |
||
262 | * |
||
263 | * @param array $patterns |
||
264 | * @return bool |
||
265 | */ |
||
266 | protected function filter($path, array $patterns) |
||
276 | |||
277 | /** |
||
278 | * Removes whitespace from a PHP source string while preserving line numbers. |
||
279 | * |
||
280 | * @param string $source A PHP string |
||
281 | * @return string The PHP string with the whitespace removed |
||
282 | */ |
||
283 | protected function stripWhitespace($source) |
||
310 | } |
||
311 |