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 AutoloadGenerator 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 AutoloadGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
||
| 39 | class AutoloadGenerator { |
||
| 40 | |||
| 41 | const COMMENT = <<<AUTOLOADER_COMMENT |
||
| 42 | /** |
||
| 43 | * This file was automatically generated by automattic/jetpack-autoloader. |
||
| 44 | * |
||
| 45 | * @package automattic/jetpack-autoloader |
||
| 46 | */ |
||
| 47 | |||
| 48 | AUTOLOADER_COMMENT; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The filesystem utility. |
||
| 52 | * |
||
| 53 | * @var Filesystem |
||
| 54 | */ |
||
| 55 | private $filesystem; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Instantiate an AutoloadGenerator object. |
||
| 59 | * |
||
| 60 | * @param IOInterface $io IO object. |
||
|
|
|||
| 61 | */ |
||
| 62 | public function __construct( IOInterface $io = null ) { |
||
| 63 | $this->io = $io; |
||
| 64 | $this->filesystem = new Filesystem(); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Dump the Jetpack autoloader files. |
||
| 69 | * |
||
| 70 | * @param Composer $composer The Composer object. |
||
| 71 | * @param Config $config Config object. |
||
| 72 | * @param InstalledRepositoryInterface $localRepo Installed Repository object. |
||
| 73 | * @param PackageInterface $mainPackage Main Package object. |
||
| 74 | * @param InstallationManager $installationManager Manager for installing packages. |
||
| 75 | * @param string $targetDir Path to the current target directory. |
||
| 76 | * @param bool $scanPsrPackages Whether or not PSR packages should be converted to a classmap. |
||
| 77 | * @param string $suffix The autoloader suffix. |
||
| 78 | */ |
||
| 79 | public function dump( |
||
| 80 | Composer $composer, |
||
| 81 | Config $config, |
||
| 82 | InstalledRepositoryInterface $localRepo, |
||
| 83 | PackageInterface $mainPackage, |
||
| 84 | InstallationManager $installationManager, |
||
| 85 | $targetDir, |
||
| 86 | $scanPsrPackages = false, |
||
| 87 | $suffix = null |
||
| 88 | ) { |
||
| 89 | $this->filesystem->ensureDirectoryExists( $config->get( 'vendor-dir' ) ); |
||
| 90 | |||
| 91 | $packageMap = $composer->getAutoloadGenerator()->buildPackageMap( $installationManager, $mainPackage, $localRepo->getCanonicalPackages() ); |
||
| 92 | $autoloads = $this->parseAutoloads( $packageMap, $mainPackage ); |
||
| 93 | |||
| 94 | // Convert the autoloads into a format that the manifest generator can consume more easily. |
||
| 95 | $basePath = $this->filesystem->normalizePath( realpath( getcwd() ) ); |
||
| 96 | $vendorPath = $this->filesystem->normalizePath( realpath( $config->get( 'vendor-dir' ) ) ); |
||
| 97 | $processedAutoloads = $this->processAutoloads( $autoloads, $scanPsrPackages, $vendorPath, $basePath ); |
||
| 98 | unset( $packageMap, $autoloads ); |
||
| 99 | |||
| 100 | // Make sure none of the legacy files remain that can lead to problems with the autoloader. |
||
| 101 | $this->removeLegacyFiles( $vendorPath ); |
||
| 102 | |||
| 103 | // Write all of the files now that we're done. |
||
| 104 | $this->writeAutoloaderFiles( $vendorPath . '/jetpack-autoloader/', $suffix ); |
||
| 105 | $this->writeManifests( $vendorPath . '/' . $targetDir, $processedAutoloads ); |
||
| 106 | |||
| 107 | if ( ! $scanPsrPackages ) { |
||
| 108 | $this->io->writeError( '<warning>You are generating an unoptimized autoloader. If this is a production build, consider using the -o option.</warning>' ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Compiles an ordered list of namespace => path mappings |
||
| 114 | * |
||
| 115 | * @param array $packageMap Array of array(package, installDir-relative-to-composer.json). |
||
| 116 | * @param PackageInterface $mainPackage Main package instance. |
||
| 117 | * |
||
| 118 | * @return array The list of path mappings. |
||
| 119 | */ |
||
| 120 | public function parseAutoloads( array $packageMap, PackageInterface $mainPackage ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Sorts packages by dependency weight |
||
| 145 | * |
||
| 146 | * Packages of equal weight retain the original order |
||
| 147 | * |
||
| 148 | * @param array $packageMap The package map. |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function sortPackageMap( array $packageMap ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the file identifier. |
||
| 176 | * |
||
| 177 | * @param PackageInterface $package The package instance. |
||
| 178 | * @param string $path The path. |
||
| 179 | */ |
||
| 180 | protected function getFileIdentifier( PackageInterface $package, $path ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the path code for the given path. |
||
| 186 | * |
||
| 187 | * @param Filesystem $filesystem The filesystem instance. |
||
| 188 | * @param string $basePath The base path. |
||
| 189 | * @param string $vendorPath The vendor path. |
||
| 190 | * @param string $path The path. |
||
| 191 | * |
||
| 192 | * @return string The path code. |
||
| 193 | */ |
||
| 194 | protected function getPathCode( Filesystem $filesystem, $basePath, $vendorPath, $path ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * This function differs from the composer parseAutoloadsType in that beside returning the path. |
||
| 225 | * It also return the path and the version of a package. |
||
| 226 | * |
||
| 227 | * Supports PSR-4, PSR-0, and classmap parsing. |
||
| 228 | * |
||
| 229 | * @param array $packageMap Map of all the packages. |
||
| 230 | * @param string $type Type of autoloader to use. |
||
| 231 | * @param PackageInterface $mainPackage Instance of the Package Object. |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function parseAutoloadsType( array $packageMap, $type, PackageInterface $mainPackage ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Given Composer's autoloads this will convert them to a version that we can use to generate the manifests. |
||
| 294 | * |
||
| 295 | * When the $scanPsrPackages argument is true, PSR-4 namespaces are converted to classmaps. When $scanPsrPackages |
||
| 296 | * is false, PSR-4 namespaces are not converted to classmaps. |
||
| 297 | * |
||
| 298 | * PSR-0 namespaces are always converted to classmaps. |
||
| 299 | * |
||
| 300 | * @param array $autoloads The autoloads we want to process. |
||
| 301 | * @param bool $scanPsrPackages Whether or not PSR-4 packages should be converted to a classmap. |
||
| 302 | * @param string $vendorPath The path to the vendor directory. |
||
| 303 | * @param string $basePath The path to the current directory. |
||
| 304 | * |
||
| 305 | * @return array $processedAutoloads |
||
| 306 | */ |
||
| 307 | private function processAutoloads( $autoloads, $scanPsrPackages, $vendorPath, $basePath ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Removes all of the legacy autoloader files so they don't cause any problems. |
||
| 334 | * |
||
| 335 | * @param string $outDir The directory legacy files are written to. |
||
| 336 | */ |
||
| 337 | private function removeLegacyFiles( $outDir ) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Writes all of the autoloader files to disk. |
||
| 353 | * |
||
| 354 | * @param string $outDir The directory to write to. |
||
| 355 | * @param string $suffix The unique autoloader suffix. |
||
| 356 | */ |
||
| 357 | private function writeAutoloaderFiles( $outDir, $suffix ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Writes all of the manifest files to disk. |
||
| 389 | * |
||
| 390 | * @param string $outDir The directory to write to. |
||
| 391 | * @param array $processedAutoloads The processed autoloads. |
||
| 392 | */ |
||
| 393 | private function writeManifests( $outDir, $processedAutoloads ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Generate the PHP that will be used in the autoload_packages.php files. |
||
| 424 | * |
||
| 425 | * @param String $filename a file to prepare. |
||
| 426 | * @param String $suffix Unique suffix used in the namespace. |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private function getAutoloadPackageFile( $filename, $suffix ) { |
||
| 444 | } |
||
| 445 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.