Complex classes like Importer 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 Importer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Importer |
||
23 | { |
||
24 | /** |
||
25 | * The context. |
||
26 | * |
||
27 | * @var Context |
||
28 | */ |
||
29 | protected $context; |
||
30 | |||
31 | /** |
||
32 | * The cache. |
||
33 | * |
||
34 | * @var CacheInterface |
||
35 | */ |
||
36 | protected $cache; |
||
37 | |||
38 | /** |
||
39 | * Array of importers. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $importers = []; |
||
44 | |||
45 | /** |
||
46 | * Array of imported files. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $importedFiles = []; |
||
51 | |||
52 | /** |
||
53 | * @var PluginManager |
||
54 | */ |
||
55 | protected $pluginManager; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * @param Context $context The context |
||
61 | * @param array $importers Array of importers |
||
62 | * @param CacheInterface $cache The cache |
||
63 | * @param PluginManager $manager |
||
64 | */ |
||
65 | public function __construct(Context $context, array $importers, CacheInterface $cache, PluginManager $manager = null) |
||
72 | |||
73 | /** |
||
74 | * Sets The context. |
||
75 | * |
||
76 | * @param Context $context |
||
77 | * |
||
78 | * @return Importer |
||
79 | */ |
||
80 | public function setEnvironment(Context $context) |
||
86 | |||
87 | /** |
||
88 | * Sets the cache. |
||
89 | * |
||
90 | * @param CacheInterface $cache |
||
91 | * |
||
92 | * @return Importer |
||
93 | */ |
||
94 | public function setCache(CacheInterface $cache) |
||
100 | |||
101 | /** |
||
102 | * Returns the cache. |
||
103 | * |
||
104 | * @return CacheInterface |
||
105 | */ |
||
106 | public function getCache() |
||
110 | |||
111 | /** |
||
112 | * Returns the context. |
||
113 | * |
||
114 | * @return Context |
||
115 | */ |
||
116 | public function getContext() |
||
120 | |||
121 | /** |
||
122 | * Imports the file. |
||
123 | * |
||
124 | * @param string $path The path to import. Path will be searched by the importers |
||
125 | * @param bool $tryAppendLessExtension Whether to try appending the less extension (if the path has no extension) |
||
126 | * @param array $importOptions Import options |
||
127 | * @param int $index Current index |
||
128 | * |
||
129 | * @return array |
||
130 | * |
||
131 | * @throws ImportException If the $path could not be imported |
||
132 | */ |
||
133 | public function import( |
||
208 | |||
209 | /** |
||
210 | * Does the import. |
||
211 | * |
||
212 | * @param ImportedFile $file The imported file |
||
213 | * @param string $path The original path |
||
214 | * @param FileInfo $currentFileInfo Current file info |
||
215 | * @param array $importOptions Import options |
||
216 | * @param bool $fromCache Is the imported file coming from cache? |
||
217 | * |
||
218 | * @throws ParserException |
||
219 | * @throws Exception |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function doImport( |
||
304 | |||
305 | /** |
||
306 | * Updates the currentFileInfo object to the $value. |
||
307 | * |
||
308 | * @param Node $node The node to update |
||
309 | * @param FileInfo $newInfo The new file info |
||
310 | */ |
||
311 | protected function updateReferenceInCurrentFileInfo(Node $node, FileInfo $newInfo) |
||
326 | |||
327 | /** |
||
328 | * Returns the last modification time of the file. |
||
329 | * |
||
330 | * @param string $path |
||
331 | * @param FileInfo $currentFileInfo |
||
332 | * |
||
333 | * @return int |
||
334 | * |
||
335 | * @throws Exception If there was an error |
||
336 | */ |
||
337 | public function getLastModified($path, FileInfo $currentFileInfo) |
||
349 | |||
350 | /** |
||
351 | * Registers an importer. |
||
352 | * |
||
353 | * @param ImporterInterface $importer |
||
354 | * @param string $name The importer name (only for developer reference) |
||
355 | * @param bool $prepend Prepend before current importers? |
||
356 | * |
||
357 | * @return Importer |
||
358 | */ |
||
359 | public function registerImporter(ImporterInterface $importer, $name = null, $prepend = false) |
||
375 | |||
376 | /** |
||
377 | * Returns the importer with given name. |
||
378 | * |
||
379 | * @param string $name |
||
380 | * |
||
381 | * @return ImporterInterface |
||
382 | */ |
||
383 | public function getImporter($name) |
||
387 | |||
388 | /** |
||
389 | * Returns registered importers. |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | public function getImporters() |
||
397 | |||
398 | /** |
||
399 | * Registers an array of importers. |
||
400 | * |
||
401 | * @param array $importers |
||
402 | * |
||
403 | * @return Importer |
||
404 | */ |
||
405 | public function registerImporters(array $importers) |
||
413 | |||
414 | /** |
||
415 | * Clears all importers. |
||
416 | * |
||
417 | * @return Importer |
||
418 | */ |
||
419 | public function clearImporters() |
||
425 | |||
426 | /** |
||
427 | * Returns a list of imported files. |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | public function getImportedFiles() |
||
435 | |||
436 | /** |
||
437 | * Sets the imported file. |
||
438 | * |
||
439 | * @param string $pathAbsolute The absolute path |
||
440 | * @param ImportedFile $file The imported file |
||
441 | * @param string $path The original path to import |
||
442 | * @param FileInfo $currentFileInfo |
||
443 | * |
||
444 | * @return Importer |
||
445 | */ |
||
446 | public function setImportedFile($pathAbsolute, ImportedFile $file, $path, FileInfo $currentFileInfo) |
||
454 | |||
455 | /** |
||
456 | * Returns the imported file. |
||
457 | * |
||
458 | * @param string $absolutePath The absolute path of the file |
||
459 | * @param mixed $default The default when no file with given $path is already imported |
||
460 | * |
||
461 | * @return array Array(ImportedFile, $originalPath, CurrentFileInfo) |
||
462 | */ |
||
463 | public function getImportedFile($absolutePath, $default = null) |
||
467 | |||
468 | /** |
||
469 | * Generates unique cache key for given $filename. |
||
470 | * |
||
471 | * @param string $filename |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | protected function generateCacheKey($filename) |
||
479 | } |
||
480 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.