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 |
||
| 25 | class Compiler |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $path; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $files = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $index = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Creates a Compiler instance. |
||
| 44 | * |
||
| 45 | * @param string $path The root path of the project |
||
| 46 | * @throws \LogicException if the creation of Phar archives is disabled in php.ini. |
||
| 47 | */ |
||
| 48 | 12 | public function __construct($path) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Compiles all files into a single PHAR file. |
||
| 59 | * |
||
| 60 | * @param string $outputFile The full name of the file to create |
||
| 61 | * @throws \LogicException if no index files are defined. |
||
| 62 | */ |
||
| 63 | 12 | public function compile($outputFile) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Gets the root path of the project. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getPath() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Gets list of all added files. |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getFiles() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Adds a file. |
||
| 129 | * |
||
| 130 | * @param string $file The name of the file relative to the project root |
||
| 131 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
| 132 | */ |
||
| 133 | 12 | public function addFile($file, $strip = true) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Adds files of the given directory recursively. |
||
| 141 | * |
||
| 142 | * @param string $directory The name of the directory relative to the project root |
||
| 143 | * @param string|array $exclude List of file name patterns to exclude (optional) |
||
| 144 | * @param bool $strip Strip whitespace (Default: TRUE) |
||
| 145 | */ |
||
| 146 | 9 | public function addDirectory($directory, $exclude = null, $strip = true) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Gets list of defined index files. |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getIndexFiles() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds an index file. |
||
| 185 | * |
||
| 186 | * @param string $file The name of the file relative to the project root |
||
| 187 | * @param string $type The SAPI type (Default: 'cli') |
||
| 188 | */ |
||
| 189 | 12 | public function addIndexFile($file, $type = 'cli') |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Gets list of all supported SAPIs. |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function getSupportedSapis() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns whether the compiled program will support the given SAPI type. |
||
| 212 | * |
||
| 213 | * @param string $sapi The SAPI type |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function supportsSapi($sapi) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Generates the stub. |
||
| 223 | * |
||
| 224 | * @param string $name The internal Phar name |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 12 | protected function generateStub($name) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Matches the given path. |
||
| 257 | * |
||
| 258 | * @param string $path |
||
| 259 | * @param string $pattern |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | 6 | protected function match($path, $pattern) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Filters the given path. |
||
| 276 | * |
||
| 277 | * @param string $path |
||
| 278 | * @param array $patterns |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 6 | protected function filter($path, array $patterns) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Removes whitespace from a PHP source string while preserving line numbers. |
||
| 294 | * |
||
| 295 | * @param string $source A PHP string |
||
| 296 | * @return string The PHP string with the whitespace removed |
||
| 297 | */ |
||
| 298 | 12 | protected function stripWhitespace($source) |
|
| 325 | } |
||
| 326 |