| Total Complexity | 42 |
| Total Lines | 384 |
| Duplicated Lines | 8.59 % |
| Changes | 0 | ||
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:
Complex classes like Box often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Box, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Box |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * The source code compactors. |
||
| 42 | * |
||
| 43 | * @var SplObjectStorage |
||
| 44 | */ |
||
| 45 | private $compactors; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The path to the Phar file. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $file; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The Phar instance. |
||
| 56 | * |
||
| 57 | * @var Phar |
||
| 58 | */ |
||
| 59 | private $phar; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The placeholder values. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $values = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the Phar instance. |
||
| 70 | * |
||
| 71 | * @param Phar $phar the instance |
||
| 72 | * @param string $file the path to the Phar file |
||
| 73 | */ |
||
| 74 | public function __construct(Phar $phar, $file) |
||
| 75 | { |
||
| 76 | $this->compactors = new SplObjectStorage(); |
||
| 77 | $this->file = $file; |
||
| 78 | $this->phar = $phar; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Adds a file contents compactor. |
||
| 83 | * |
||
| 84 | * @param Compactor $compactor the compactor |
||
| 85 | */ |
||
| 86 | public function addCompactor(Compactor $compactor): void |
||
| 87 | { |
||
| 88 | $this->compactors->attach($compactor); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Adds a file to the Phar, after compacting it and replacing its |
||
| 93 | * placeholders. |
||
| 94 | * |
||
| 95 | * @param string $file the file name |
||
| 96 | * @param string $local the local file name |
||
| 97 | * |
||
| 98 | * @throws Exception\Exception |
||
| 99 | * @throws FileException if the file could not be used |
||
| 100 | */ |
||
| 101 | public function addFile($file, $local = null): void |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Adds the contents from a file to the Phar, after compacting it and |
||
| 123 | * replacing its placeholders. |
||
| 124 | * |
||
| 125 | * @param string $local the local name |
||
| 126 | * @param string $contents the contents |
||
| 127 | */ |
||
| 128 | public function addFromString($local, $contents): void |
||
| 129 | { |
||
| 130 | $this->phar->addFromString( |
||
| 131 | $local, |
||
| 132 | $this->replaceValues($this->compactContents($local, $contents)) |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Similar to Phar::buildFromDirectory(), except the files will be |
||
| 138 | * compacted and their placeholders replaced. |
||
| 139 | * |
||
| 140 | * @param string $dir the directory |
||
| 141 | * @param string $regex the regular expression filter |
||
| 142 | */ |
||
| 143 | public function buildFromDirectory($dir, $regex = null): void |
||
| 144 | { |
||
| 145 | $iterator = new RecursiveIteratorIterator( |
||
| 146 | new RecursiveDirectoryIterator( |
||
| 147 | $dir, |
||
| 148 | FilesystemIterator::KEY_AS_PATHNAME |
||
| 149 | | FilesystemIterator::CURRENT_AS_FILEINFO |
||
| 150 | | FilesystemIterator::SKIP_DOTS |
||
| 151 | ) |
||
| 152 | ); |
||
| 153 | |||
| 154 | if ($regex) { |
||
| 155 | $iterator = new RegexIterator($iterator, $regex); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->buildFromIterator($iterator, $dir); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Similar to Phar::buildFromIterator(), except the files will be compacted |
||
| 163 | * and their placeholders replaced. |
||
| 164 | * |
||
| 165 | * @param Traversable $iterator the iterator |
||
| 166 | * @param string $base the base directory path |
||
| 167 | * |
||
| 168 | * @throws Exception\Exception |
||
| 169 | * @throws UnexpectedValueException if the iterator value is unexpected |
||
| 170 | */ |
||
| 171 | public function buildFromIterator(Traversable $iterator, $base = null): void |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Compacts the file contents using the supported compactors. |
||
| 229 | * |
||
| 230 | * @param string $file the file name |
||
| 231 | * @param string $contents the file contents |
||
| 232 | * |
||
| 233 | * @return string the compacted contents |
||
| 234 | */ |
||
| 235 | public function compactContents($file, $contents) |
||
| 236 | { |
||
| 237 | foreach ($this->compactors as $compactor) { |
||
| 238 | /** @var $compactor CompactorInterface */ |
||
| 239 | if ($compactor->supports($file)) { |
||
| 240 | $contents = $compactor->compact($contents); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $contents; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Creates a new Phar and Box instance. |
||
| 249 | * |
||
| 250 | * @param string $file the file name |
||
| 251 | * @param int $flags the RecursiveDirectoryIterator flags |
||
| 252 | * @param string $alias the Phar alias |
||
| 253 | * |
||
| 254 | * @return Box the Box instance |
||
| 255 | */ |
||
| 256 | public static function create($file, $flags = null, $alias = null) |
||
| 257 | { |
||
| 258 | return new self(new Phar($file, (int) $flags, $alias), $file); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the Phar instance. |
||
| 263 | * |
||
| 264 | * @return Phar the instance |
||
| 265 | */ |
||
| 266 | public function getPhar() |
||
| 267 | { |
||
| 268 | return $this->phar; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the signature of the phar. |
||
| 273 | * |
||
| 274 | * This method does not use the extension to extract the phar's signature. |
||
| 275 | * |
||
| 276 | * @param string $path the phar file path |
||
| 277 | * |
||
| 278 | * @return array the signature |
||
| 279 | */ |
||
| 280 | public static function getSignature($path) |
||
| 281 | { |
||
| 282 | return Signature::create($path)->get(); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Replaces the placeholders with their values. |
||
| 287 | * |
||
| 288 | * @param string $contents the contents |
||
| 289 | * |
||
| 290 | * @return string the replaced contents |
||
| 291 | */ |
||
| 292 | public function replaceValues($contents) |
||
| 293 | { |
||
| 294 | return str_replace( |
||
| 295 | array_keys($this->values), |
||
| 296 | array_values($this->values), |
||
| 297 | $contents |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Sets the bootstrap loader stub using a file. |
||
| 303 | * |
||
| 304 | * @param string $file the file path |
||
| 305 | * @param bool $replace Replace placeholders? |
||
| 306 | * |
||
| 307 | * @throws Exception\Exception |
||
| 308 | * @throws FileException if the stub file could not be used |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function setStubUsingFile($file, $replace = false): void |
|
| 311 | { |
||
| 312 | if (false === is_file($file)) { |
||
| 313 | throw FileException::create( |
||
| 314 | 'The file "%s" does not exist or is not a file.', |
||
| 315 | $file |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | |||
| 319 | if (false === ($contents = @file_get_contents($file))) { |
||
| 320 | throw FileException::lastError(); |
||
| 321 | } |
||
| 322 | |||
| 323 | if ($replace) { |
||
| 324 | $contents = $this->replaceValues($contents); |
||
| 325 | } |
||
| 326 | |||
| 327 | $this->phar->setStub($contents); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sets the placeholder values. |
||
| 332 | * |
||
| 333 | * @param array $values the values |
||
| 334 | * |
||
| 335 | * @throws Exception\Exception |
||
| 336 | * @throws InvalidArgumentException if a non-scalar value is used |
||
| 337 | */ |
||
| 338 | public function setValues(array $values): void |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Signs the Phar using a private key. |
||
| 354 | * |
||
| 355 | * @param string $key the private key |
||
| 356 | * @param string $password the private key password |
||
| 357 | * |
||
| 358 | * @throws Exception\Exception |
||
| 359 | * @throws OpenSslException if the "openssl" extension could not be used |
||
| 360 | * or has generated an error |
||
| 361 | */ |
||
| 362 | public function sign($key, $password = null): void |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Signs the Phar using a private key file. |
||
| 401 | * |
||
| 402 | * @param string $file the private key file name |
||
| 403 | * @param string $password the private key password |
||
| 404 | * |
||
| 405 | * @throws Exception\Exception |
||
| 406 | * @throws FileException if the private key file could not be read |
||
| 407 | */ |
||
| 408 | View Code Duplication | public function signUsingFile($file, $password = null): void |
|
| 422 | } |
||
| 423 | } |
||
| 424 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: