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 extends BaseGenerator { |
||
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 | * Instantiate an AutoloadGenerator object. |
||
52 | * |
||
53 | * @param IOInterface $io IO object. |
||
|
|||
54 | */ |
||
55 | public function __construct( IOInterface $io = null ) { |
||
58 | |||
59 | /** |
||
60 | * Dump the autoloader. |
||
61 | * |
||
62 | * @param Config $config Config object. |
||
63 | * @param InstalledRepositoryInterface $localRepo Installed Reposetories object. |
||
64 | * @param PackageInterface $mainPackage Main Package object. |
||
65 | * @param InstallationManager $installationManager Manager for installing packages. |
||
66 | * @param string $targetDir Path to the current target directory. |
||
67 | * @param bool $scanPsr0Packages Whether to search for packages. Currently hard coded to always be false. |
||
68 | * @param string $suffix The autoloader suffix. |
||
69 | */ |
||
70 | public function dump( |
||
71 | Config $config, |
||
72 | InstalledRepositoryInterface $localRepo, |
||
73 | PackageInterface $mainPackage, |
||
74 | InstallationManager $installationManager, |
||
75 | $targetDir, |
||
76 | $scanPsr0Packages = null, // Not used we always optimize. |
||
77 | $suffix = null |
||
78 | ) { |
||
79 | |||
80 | $filesystem = new Filesystem(); |
||
81 | $filesystem->ensureDirectoryExists( $config->get( 'vendor-dir' ) ); |
||
82 | |||
83 | $basePath = $filesystem->normalizePath( realpath( getcwd() ) ); |
||
84 | $vendorPath = $filesystem->normalizePath( realpath( $config->get( 'vendor-dir' ) ) ); |
||
85 | $targetDir = $vendorPath . '/' . $targetDir; |
||
86 | $filesystem->ensureDirectoryExists( $targetDir ); |
||
87 | |||
88 | $packageMap = $this->buildPackageMap( $installationManager, $mainPackage, $localRepo->getCanonicalPackages() ); |
||
89 | $autoloads = $this->parseAutoloads( $packageMap, $mainPackage ); |
||
90 | |||
91 | $classMap = $this->getClassMap( $autoloads, $filesystem, $vendorPath, $basePath ); |
||
92 | $fileMap = $this->getFileMap( $autoloads, $filesystem, $vendorPath, $basePath ); |
||
93 | |||
94 | // Remove a file that was generated in versions 2.0.0 to 2.1.0. |
||
95 | $filesystem->remove( $vendorPath . '/autoload_functions.php' ); |
||
96 | |||
97 | // Generate the files. |
||
98 | file_put_contents( $targetDir . '/jetpack_autoload_classmap.php', $this->getAutoloadClassmapPackagesFile( $classMap ) ); |
||
99 | $this->io->writeError( '<info>Generated ' . $targetDir . '/jetpack_autoload_classmap.php</info>', true ); |
||
100 | |||
101 | file_put_contents( $targetDir . '/jetpack_autoload_filemap.php', $this->getAutoloadFilesPackagesFile( $fileMap ) ); |
||
102 | $this->io->writeError( '<info>Generated ' . $targetDir . '/jetpack_autoload_filemap.php</info>', true ); |
||
103 | |||
104 | file_put_contents( $vendorPath . '/autoload_packages.php', $this->getAutoloadPackageFile( 'autoload.php', $suffix ) ); |
||
105 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/autoload_packages.php</info>', true ); |
||
106 | |||
107 | $jetpackAutoloaderDir = $vendorPath . '/jetpack-autoloader'; |
||
108 | $filesystem->ensureDirectoryExists( $jetpackAutoloaderDir ); |
||
109 | file_put_contents( $jetpackAutoloaderDir . '/autoload_functions.php', $this->getAutoloadPackageFile( 'functions.php', $suffix ) ); |
||
110 | $this->io->writeError( '<info>Generated ' . $jetpackAutoloaderDir . '/jetpack-autoloader/autoload_functions.php</info>', true ); |
||
111 | |||
112 | file_put_contents( $vendorPath . '/class-autoloader-handler.php', $this->getAutoloadPackageFile( 'class-autoloader-handler.php', $suffix ) ); |
||
113 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/class-autoloader-handler.php</info>', true ); |
||
114 | |||
115 | file_put_contents( $vendorPath . '/class-classes-handler.php', $this->getAutoloadPackageFile( 'class-classes-handler.php', $suffix ) ); |
||
116 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/class-classes-handler.php</info>', true ); |
||
117 | |||
118 | file_put_contents( $vendorPath . '/class-files-handler.php', $this->getAutoloadPackageFile( 'class-files-handler.php', $suffix ) ); |
||
119 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/class-files-handler.php</info>', true ); |
||
120 | |||
121 | file_put_contents( $vendorPath . '/class-plugins-handler.php', $this->getAutoloadPackageFile( 'class-plugins-handler.php', $suffix ) ); |
||
122 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/class-plugins-handler.php</info>', true ); |
||
123 | |||
124 | file_put_contents( $vendorPath . '/class-version-selector.php', $this->getAutoloadPackageFile( 'class-version-selector.php', $suffix ) ); |
||
125 | $this->io->writeError( '<info>Generated ' . $vendorPath . '/class-version-selector.php</info>', true ); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * This function differs from the composer parseAutoloadsType in that beside returning the path. |
||
130 | * It also return the path and the version of a package. |
||
131 | * |
||
132 | * Currently supports only psr-4 and clasmap parsing. |
||
133 | * |
||
134 | * @param array $packageMap Map of all the packages. |
||
135 | * @param string $type Type of autoloader to use, currently not used, since we only support psr-4. |
||
136 | * @param PackageInterface $mainPackage Instance of the Package Object. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | protected function parseAutoloadsType( array $packageMap, $type, PackageInterface $mainPackage ) { |
||
200 | |||
201 | /** |
||
202 | * Take the autoloads array and return the classMap that contains the path and the version for each namespace. |
||
203 | * |
||
204 | * @param array $autoloads Array of autoload settings defined defined by the packages. |
||
205 | * @param Filesystem $filesystem Filesystem class instance. |
||
206 | * @param string $vendorPath Path to the vendor directory. |
||
207 | * @param string $basePath Base Path. |
||
208 | * |
||
209 | * @return string $classMap |
||
210 | */ |
||
211 | private function getClassMap( array $autoloads, Filesystem $filesystem, $vendorPath, $basePath ) { |
||
270 | |||
271 | /** |
||
272 | * Generate the PHP that will be used in the autoload_classmap_package.php files. |
||
273 | * |
||
274 | * @param string $classMap class map array string that is to be written out to the file. |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | private function getAutoloadClassmapPackagesFile( $classMap ) { |
||
292 | |||
293 | /** |
||
294 | * Take the autoloads array and return the fileMap that contains the path and the version for each namespace. |
||
295 | * |
||
296 | * @param array $autoloads Array of autoload settings defined defined by the packages. |
||
297 | * @param Filesystem $filesystem Filesystem class instance. |
||
298 | * @param string $vendorPath Path to the vendor directory. |
||
299 | * @param string $basePath Base Path. |
||
300 | * |
||
301 | * @return string $fileMap |
||
302 | */ |
||
303 | private function getFileMap( array $autoloads, Filesystem $filesystem, $vendorPath, $basePath ) { |
||
320 | |||
321 | /** |
||
322 | * Generate the PHP that will be used in the autoload_files_package.php files. |
||
323 | * |
||
324 | * @param string $filesMap files array as string that is to be written out to the file. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | private function getAutoloadFilesPackagesFile( $filesMap ) { |
||
342 | |||
343 | /** |
||
344 | * Generate the PHP that will be used in the autoload_packages.php files. |
||
345 | * |
||
346 | * @param String $filename a file to prepare. |
||
347 | * @param String $suffix Unique suffix used in the namespace. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | private function getAutoloadPackageFile( $filename, $suffix ) { |
||
365 | } |
||
366 |
This check looks for
@param
annotations 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.