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 PatchedPharPackageTask 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PatchedPharPackageTask, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class PatchedPharPackageTask |
||
|
|
|||
| 20 | extends MatchingTask |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var PhingFile |
||
| 24 | */ |
||
| 25 | private $destinationFile; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $compression = Phar::NONE; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Base directory, from where local package paths will be calculated. |
||
| 34 | * |
||
| 35 | * @var PhingFile |
||
| 36 | */ |
||
| 37 | private $baseDirectory; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var PhingFile |
||
| 41 | */ |
||
| 42 | private $cliStubFile; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var PhingFile |
||
| 46 | */ |
||
| 47 | private $webStubFile; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $stubPath; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Private key the Phar will be signed with. |
||
| 56 | * |
||
| 57 | * @var PhingFile |
||
| 58 | */ |
||
| 59 | private $key; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Password for the private key. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $keyPassword = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $signatureAlgorithm = Phar::SHA1; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $filesets = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var PharMetadata |
||
| 80 | */ |
||
| 81 | private $metadata = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $alias; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return PharMetadata |
||
| 90 | */ |
||
| 91 | public function createMetadata() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return FileSet |
||
| 98 | */ |
||
| 99 | public function createFileSet() |
||
| 100 | { |
||
| 101 | $this->fileset = new IterableFileSet(); |
||
| 102 | $this->filesets[] = $this->fileset; |
||
| 103 | |||
| 104 | return $this->fileset; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Signature algorithm (md5, sha1, sha256, sha512, openssl), |
||
| 109 | * used for this package. |
||
| 110 | * |
||
| 111 | * @param string $algorithm |
||
| 112 | */ |
||
| 113 | public function setSignature($algorithm) |
||
| 114 | { |
||
| 115 | /* |
||
| 116 | * If we don't support passed algprithm, leave old one. |
||
| 117 | */ |
||
| 118 | switch ($algorithm) { |
||
| 119 | case 'md5': |
||
| 120 | $this->signatureAlgorithm = Phar::MD5; |
||
| 121 | break; |
||
| 122 | case 'sha1': |
||
| 123 | $this->signatureAlgorithm = Phar::SHA1; |
||
| 124 | break; |
||
| 125 | case 'sha256': |
||
| 126 | $this->signatureAlgorithm = Phar::SHA256; |
||
| 127 | break; |
||
| 128 | case 'sha512': |
||
| 129 | $this->signatureAlgorithm = Phar::SHA512; |
||
| 130 | break; |
||
| 131 | case 'openssl': |
||
| 132 | $this->signatureAlgorithm = Phar::OPENSSL; |
||
| 133 | break; |
||
| 134 | default: |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Compression type (gzip, bzip2, none) to apply to the packed files. |
||
| 141 | * |
||
| 142 | * @param string $compression |
||
| 143 | */ |
||
| 144 | public function setCompression($compression) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | private function getCompressionLabel() |
||
| 165 | { |
||
| 166 | $compression = $this->compression; |
||
| 167 | |||
| 168 | switch ($compression) { |
||
| 169 | case Phar::GZ: |
||
| 170 | return "gzip"; |
||
| 171 | |||
| 172 | case Phar::BZ2: |
||
| 173 | return "bzip2"; |
||
| 174 | |||
| 175 | default: |
||
| 176 | return sprintf("int(%d)", $compression); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Destination (output) file. |
||
| 182 | * |
||
| 183 | * @param PhingFile $destinationFile |
||
| 184 | */ |
||
| 185 | public function setDestFile(PhingFile $destinationFile) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Base directory, which will be deleted from each included file (from path). |
||
| 192 | * Paths with deleted basedir part are local paths in package. |
||
| 193 | * |
||
| 194 | * @param PhingFile $baseDirectory |
||
| 195 | */ |
||
| 196 | public function setBaseDir(PhingFile $baseDirectory) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Relative path within the phar package to run, |
||
| 203 | * if accessed on the command line. |
||
| 204 | * |
||
| 205 | * @param PhingFile $stubFile |
||
| 206 | */ |
||
| 207 | public function setCliStub(PhingFile $stubFile) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Relative path within the phar package to run, |
||
| 214 | * if accessed through a web browser. |
||
| 215 | * |
||
| 216 | * @param PhingFile $stubFile |
||
| 217 | */ |
||
| 218 | public function setWebStub(PhingFile $stubFile) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * A path to a php file that contains a custom stub. |
||
| 225 | * |
||
| 226 | * @param string $stubPath |
||
| 227 | */ |
||
| 228 | public function setStub($stubPath) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * An alias to assign to the phar package. |
||
| 235 | * |
||
| 236 | * @param string $alias |
||
| 237 | */ |
||
| 238 | public function setAlias($alias) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets the private key to use to sign the Phar with. |
||
| 245 | * |
||
| 246 | * @param PhingFile $key Private key to sign the Phar with. |
||
| 247 | */ |
||
| 248 | public function setKey(PhingFile $key) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Password for the private key. |
||
| 255 | * |
||
| 256 | * @param string $keyPassword |
||
| 257 | */ |
||
| 258 | public function setKeyPassword($keyPassword) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @throws BuildException |
||
| 265 | */ |
||
| 266 | public function main() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @throws BuildException |
||
| 295 | */ |
||
| 296 | private function checkPreconditions() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Build and configure Phar object. |
||
| 346 | * |
||
| 347 | * @return Phar |
||
| 348 | */ |
||
| 349 | private function buildPhar() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return Phar |
||
| 407 | */ |
||
| 408 | private function initPhar() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param Phar $phar |
||
| 423 | * @param string $baseDirectory |
||
| 424 | */ |
||
| 425 | private function compressEachFile(Phar $phar, $baseDirectory) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param Phar $phar |
||
| 452 | * @param string $baseDirectory |
||
| 453 | */ |
||
| 454 | private function compressAllFiles(Phar $phar, $baseDirectory) |
||
| 497 | } |
||
| 498 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.