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 |
||
40 | class AutoloadGenerator extends BaseGenerator { |
||
41 | |||
42 | /** |
||
43 | * Instantiate an AutoloadGenerator object. |
||
44 | * |
||
45 | * @param IOInterface $io IO object. |
||
|
|||
46 | */ |
||
47 | public function __construct( IOInterface $io = null ) { |
||
50 | |||
51 | /** |
||
52 | * Dump the autoloader. |
||
53 | * |
||
54 | * @param Config $config Config object. |
||
55 | * @param InstalledRepositoryInterface $localRepo Installed Reposetories object. |
||
56 | * @param PackageInterface $mainPackage Main Package object. |
||
57 | * @param InstallationManager $installationManager Manager for installing packages. |
||
58 | * @param string $targetDir Path to the current target directory. |
||
59 | * @param bool $scanPsr0Packages Whether to search for packages. Currently hard coded to always be false. |
||
60 | * @param string $suffix The autoloader suffix, ignored since we want our autoloader to only be included once. |
||
61 | */ |
||
62 | public function dump( |
||
93 | |||
94 | /** |
||
95 | * This function differs from the composer parseAutoloadsType in that beside returning the path. |
||
96 | * It also return the path and the version of a package. |
||
97 | * |
||
98 | * Currently supports only psr-4 and clasmap parsing. |
||
99 | * |
||
100 | * @param array $packageMap Map of all the packages. |
||
101 | * @param string $type Type of autoloader to use, currently not used, since we only support psr-4. |
||
102 | * @param PackageInterface $mainPackage Instance of the Package Object. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | protected function parseAutoloadsType( array $packageMap, $type, PackageInterface $mainPackage ) { |
||
154 | |||
155 | /** |
||
156 | * Take the autoloads array and return the classMap that contains the path and the version for each namespace. |
||
157 | * |
||
158 | * @param array $autoloads Array of autoload settings defined defined by the packages. |
||
159 | * @param Filesystem $filesystem Filesystem class instance. |
||
160 | * @param string $vendorPath Path to the vendor directory. |
||
161 | * @param string $basePath Base Path. |
||
162 | * |
||
163 | * @return array $classMap |
||
164 | */ |
||
165 | private function getClassMap( array $autoloads, Filesystem $filesystem, $vendorPath, $basePath ) { |
||
166 | $blacklist = null; |
||
167 | |||
168 | if ( ! empty( $autoloads['exclude-from-classmap'] ) ) { |
||
169 | $blacklist = '{(' . implode( '|', $autoloads['exclude-from-classmap'] ) . ')}'; |
||
170 | } |
||
171 | |||
172 | $classmapString = ''; |
||
173 | |||
174 | // Scan the PSR-4 and classmap directories for class files, and add them to the class map. |
||
175 | foreach ( $autoloads['psr-4'] as $namespace => $packages_info ) { |
||
176 | foreach ( $packages_info as $package ) { |
||
177 | $dir = $filesystem->normalizePath( |
||
178 | $filesystem->isAbsolutePath( $package['path'] ) |
||
179 | ? $package['path'] |
||
180 | : $basePath . '/' . $package['path'] |
||
181 | ); |
||
182 | $namespace = empty( $namespace ) ? null : $namespace; |
||
183 | $map = ClassMapGenerator::createMap( $dir, $blacklist, $this->io, $namespace ); |
||
184 | |||
185 | View Code Duplication | foreach ( $map as $class => $path ) { |
|
186 | $classCode = var_export( $class, true ); |
||
187 | $pathCode = $this->getPathCode( $filesystem, $basePath, $vendorPath, $path ); |
||
188 | $versionCode = var_export( $package['version'], true ); |
||
189 | $classmapString .= <<<CLASS_CODE |
||
190 | $classCode => array( |
||
191 | 'version' => $versionCode, |
||
192 | 'path' => $pathCode |
||
193 | ), |
||
194 | CLASS_CODE; |
||
195 | $classmapString .= PHP_EOL; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | foreach ( $autoloads['classmap'] as $package ) { |
||
201 | $dir = $filesystem->normalizePath( |
||
202 | $filesystem->isAbsolutePath( $package['path'] ) |
||
203 | ? $package['path'] |
||
204 | : $basePath . '/' . $package['path'] |
||
205 | ); |
||
206 | $map = ClassMapGenerator::createMap( $dir, $blacklist, $this->io, null ); |
||
207 | |||
208 | View Code Duplication | foreach ( $map as $class => $path ) { |
|
209 | $classCode = var_export( $class, true ); |
||
210 | $pathCode = $this->getPathCode( $filesystem, $basePath, $vendorPath, $path ); |
||
211 | $versionCode = var_export( $package['version'], true ); |
||
212 | $classmapString .= <<<CLASS_CODE |
||
213 | $classCode => array( |
||
214 | 'version' => $versionCode, |
||
215 | 'path' => $pathCode |
||
216 | ), |
||
217 | CLASS_CODE; |
||
218 | $classmapString .= PHP_EOL; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return 'array( ' . PHP_EOL . $classmapString . ');' . PHP_EOL; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Generate the PHP that will be used in the autoload_classmap_package.php files. |
||
227 | * |
||
228 | * @param srting $classMap class map array string that is to be written out to the file. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | private function getAutoloadClassmapPackagesFile( $classMap ) { |
||
246 | |||
247 | /** |
||
248 | * Generate the PHP that will be used in the autoload_packages.php files. |
||
249 | * |
||
250 | * @param string $suffix Unique suffix added to the jetpack_enqueue_packages function. |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | private function getAutoloadPackageFile( $suffix ) { |
||
286 | } |
||
287 |
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.