Complex classes like FixturesLoader 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 FixturesLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class FixturesLoader |
||
29 | { |
||
30 | /** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */ |
||
31 | private $container; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private static $cachedMetadatas = array(); |
||
37 | |||
38 | public function __construct(ContainerInterface $container) |
||
42 | |||
43 | /** |
||
44 | * @param string $type |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | private function getExecutorClass($type) |
||
54 | |||
55 | /** |
||
56 | * Get file path of the SQLite database. |
||
57 | * |
||
58 | * @param Connection $connection |
||
59 | * |
||
60 | * @return string $name |
||
61 | */ |
||
62 | private function getNameParameter(Connection $connection) |
||
78 | |||
79 | /** |
||
80 | * Purge SQLite database. |
||
81 | * |
||
82 | * @param ObjectManager $om |
||
83 | * @param string $omName The name of object manager to use |
||
84 | */ |
||
85 | private function getCachedMetadatas(ObjectManager $om, $omName) |
||
94 | |||
95 | /** |
||
96 | * This function finds the time when the data blocks of a class definition |
||
97 | * file were being written to, that is, the time when the content of the |
||
98 | * file was changed. |
||
99 | * |
||
100 | * @param string $class The fully qualified class name of the fixture class to |
||
101 | * check modification date on. |
||
102 | * |
||
103 | * @return \DateTime|null |
||
104 | */ |
||
105 | protected function getFixtureLastModified($class) |
||
119 | |||
120 | /** |
||
121 | * Determine if the Fixtures that define a database backup have been |
||
122 | * modified since the backup was made. |
||
123 | * |
||
124 | * @param array $classNames The fixture classnames to check |
||
125 | * @param string $backup The fixture backup SQLite database file path |
||
126 | * |
||
127 | * @return bool TRUE if the backup was made since the modifications to the |
||
128 | * fixtures; FALSE otherwise |
||
129 | */ |
||
130 | protected function isBackupUpToDate(array $classNames, $backup) |
||
144 | |||
145 | /** |
||
146 | * Copy SQLite backup file. |
||
147 | * |
||
148 | * @param ObjectManager $om |
||
149 | * @param string $executorClass |
||
150 | * @param ProxyReferenceRepository $referenceRepository |
||
151 | * @param string $backup Path of the source file. |
||
152 | * @param string $name Path of the destination file. |
||
153 | */ |
||
154 | private function copySqliteBackup($om, $executorClass, |
||
172 | |||
173 | /** |
||
174 | * Purge database. |
||
175 | * |
||
176 | * @param ObjectManager $om |
||
177 | * @param string $type |
||
178 | * @param int $purgeMode |
||
179 | * @param string $executorClass |
||
180 | * @param ProxyReferenceRepository $referenceRepository |
||
181 | */ |
||
182 | private function purgeDatabase(ObjectManager $om, $type, $purgeMode, |
||
208 | |||
209 | /** |
||
210 | * Purge database. |
||
211 | * |
||
212 | * @param ObjectManager $om |
||
213 | * @param array $metadatas |
||
214 | * @param string $executorClass |
||
215 | * @param ProxyReferenceRepository $referenceRepository |
||
216 | */ |
||
217 | private function createSqliteSchema(ObjectManager $om, |
||
232 | |||
233 | /** |
||
234 | * Set the database to the provided fixtures. |
||
235 | * |
||
236 | * Drops the current database and then loads fixtures using the specified |
||
237 | * classes. The parameter is a list of fully qualified class names of |
||
238 | * classes that implement Doctrine\Common\DataFixtures\FixtureInterface |
||
239 | * so that they can be loaded by the DataFixtures Loader::addFixture |
||
240 | * |
||
241 | * When using SQLite this method will automatically make a copy of the |
||
242 | * loaded schema and fixtures which will be restored automatically in |
||
243 | * case the same fixture classes are to be loaded again. Caveat: changes |
||
244 | * to references and/or identities may go undetected. |
||
245 | * |
||
246 | * Depends on the doctrine data-fixtures library being available in the |
||
247 | * class path. |
||
248 | * |
||
249 | * @param array $classNames List of fully qualified class names of fixtures to load |
||
250 | * @param string $omName The name of object manager to use |
||
251 | * @param string $registryName The service id of manager registry to use |
||
252 | * @param int $purgeMode Sets the ORM purge mode |
||
253 | * |
||
254 | * @return null|AbstractExecutor |
||
255 | */ |
||
256 | public function loadFixtures(array $classNames, $omName = null, $registryName = 'doctrine', $purgeMode = null) |
||
314 | |||
315 | /** |
||
316 | * Clean database. |
||
317 | * |
||
318 | * @param ManagerRegistry $registry |
||
319 | * @param EntityManager $om |
||
320 | */ |
||
321 | private function cleanDatabase(ManagerRegistry $registry, EntityManager $om) |
||
338 | |||
339 | /** |
||
340 | * Locate fixture files. |
||
341 | * |
||
342 | * @param array $paths |
||
343 | * |
||
344 | * @return array $files |
||
345 | */ |
||
346 | private function locateResources($paths) |
||
363 | |||
364 | /** |
||
365 | * @param array $paths Either symfony resource locators (@ BundleName/etc) or actual file paths |
||
366 | * @param bool $append |
||
367 | * @param null $omName |
||
368 | * @param string $registryName |
||
369 | * |
||
370 | * @return array |
||
371 | * |
||
372 | * @throws \BadMethodCallException |
||
373 | */ |
||
374 | public function loadFixtureFiles(array $paths = array(), $append = false, $omName = null, $registryName = 'doctrine') |
||
393 | |||
394 | /** |
||
395 | * Retrieve Doctrine DataFixtures loader. |
||
396 | * |
||
397 | * @param array $classNames |
||
398 | * |
||
399 | * @return Loader |
||
400 | */ |
||
401 | protected function getFixtureLoader(array $classNames) |
||
417 | |||
418 | /** |
||
419 | * Load a data fixture class. |
||
420 | * |
||
421 | * @param Loader $loader |
||
422 | * @param string $className |
||
423 | */ |
||
424 | protected function loadFixtureClass($loader, $className) |
||
442 | |||
443 | /** |
||
444 | * Callback function to be executed after Schema creation. |
||
445 | * Use this to execute acl:init or other things necessary. |
||
446 | */ |
||
447 | protected function postFixtureSetup() |
||
450 | |||
451 | /** |
||
452 | * Callback function to be executed after Schema restore. |
||
453 | * |
||
454 | * @return WebTestCase |
||
455 | */ |
||
456 | protected function postFixtureRestore() |
||
459 | |||
460 | /** |
||
461 | * Callback function to be executed before Schema restore. |
||
462 | * |
||
463 | * @param ObjectManager $manager The object manager |
||
464 | * @param ProxyReferenceRepository $referenceRepository The reference repository |
||
465 | * |
||
466 | * @return WebTestCase |
||
467 | */ |
||
468 | protected function preFixtureRestore(ObjectManager $manager, ProxyReferenceRepository $referenceRepository) |
||
471 | |||
472 | /** |
||
473 | * Callback function to be executed after save of references. |
||
474 | * |
||
475 | * @param ObjectManager $manager The object manager |
||
476 | * @param AbstractExecutor $executor Executor of the data fixtures |
||
477 | * @param string $backupFilePath Path of file used to backup the references of the data fixtures |
||
478 | * |
||
479 | * @return WebTestCase |
||
480 | */ |
||
481 | protected function postReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath) |
||
484 | |||
485 | /** |
||
486 | * Callback function to be executed before save of references. |
||
487 | * |
||
488 | * @param ObjectManager $manager The object manager |
||
489 | * @param AbstractExecutor $executor Executor of the data fixtures |
||
490 | * @param string $backupFilePath Path of file used to backup the references of the data fixtures |
||
491 | * |
||
492 | * @return WebTestCase |
||
493 | */ |
||
494 | protected function preReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath) |
||
497 | } |
||
498 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.