Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Manager |
||
20 | { |
||
21 | /** |
||
22 | * Manager instance |
||
23 | * |
||
24 | * @var Manager |
||
25 | */ |
||
26 | protected static $instance; |
||
27 | |||
28 | /** |
||
29 | * Driver Manager |
||
30 | * |
||
31 | * @var \Analogue\ORM\Drivers\Manager |
||
32 | */ |
||
33 | protected $drivers; |
||
34 | |||
35 | /** |
||
36 | * Registered entity classes and corresponding map objects. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $entityClasses = []; |
||
41 | |||
42 | /** |
||
43 | * Key value store of ValueObject Classes and corresponding map classes |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $valueClasses = []; |
||
48 | |||
49 | /** |
||
50 | * Morph map |
||
51 | */ |
||
52 | protected $morphMap = []; |
||
53 | |||
54 | /** |
||
55 | * Loaded Mappers |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $mappers = []; |
||
60 | |||
61 | /** |
||
62 | * Loaded Repositories |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $repositories = []; |
||
67 | |||
68 | /** |
||
69 | * Event dispatcher instance |
||
70 | * |
||
71 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
72 | */ |
||
73 | protected $eventDispatcher; |
||
74 | |||
75 | /** |
||
76 | * Available Analogue Events |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $events = [ |
||
81 | 'initializing', |
||
82 | 'initialized', |
||
83 | 'store', |
||
84 | 'stored', |
||
85 | 'creating', |
||
86 | 'created', |
||
87 | 'updating', |
||
88 | 'updated', |
||
89 | 'deleting', |
||
90 | 'deleted', |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * If strictMode is set to true, Manager will throw |
||
95 | * an exception if no entityMap class are registered |
||
96 | * for a given entity class. |
||
97 | * |
||
98 | * @var boolean |
||
99 | */ |
||
100 | protected $strictMode = false; |
||
101 | |||
102 | /** |
||
103 | * @param \Analogue\ORM\Drivers\Manager $driverManager |
||
104 | * @param Dispatcher $event |
||
105 | */ |
||
106 | public function __construct(DriverManager $driverManager, Dispatcher $event) |
||
114 | |||
115 | /** |
||
116 | * Create a mapper for a given entity (static alias) |
||
117 | * |
||
118 | * @param \Analogue\ORM\Mappable|string $entity |
||
119 | * @param null|EntityMap $entityMap |
||
120 | * @throws MappingException |
||
121 | * @return Mapper |
||
122 | */ |
||
123 | public static function getMapper($entity, $entityMap = null) |
||
127 | |||
128 | /** |
||
129 | * Create a mapper for a given entity |
||
130 | * |
||
131 | * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
||
132 | * @param mixed $entityMap |
||
133 | * @throws MappingException |
||
134 | * @throws \InvalidArgumentException |
||
135 | * @return Mapper |
||
136 | */ |
||
137 | public function mapper($entity, $entityMap = null) |
||
154 | |||
155 | /** |
||
156 | * This method resolve entity class from mappable instances or iterators |
||
157 | * |
||
158 | * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
||
159 | * @return string |
||
160 | * |
||
161 | * @throws \InvalidArgumentException |
||
162 | */ |
||
163 | protected function resolveEntityClass($entity) |
||
189 | |||
190 | /** |
||
191 | * @param string $key |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getInverseMorphMap($key) |
||
198 | |||
199 | /** |
||
200 | * Build a new Mapper instance for a given Entity |
||
201 | * |
||
202 | * @param string $entity |
||
203 | * @param $entityMap |
||
204 | * @throws MappingException |
||
205 | * @return Mapper |
||
206 | */ |
||
207 | protected function buildMapper($entity, $entityMap) |
||
230 | |||
231 | /** |
||
232 | * Check if the entity is already registered |
||
233 | * |
||
234 | * @param string|object $entity |
||
235 | * @return boolean |
||
236 | */ |
||
237 | public function isRegisteredEntity($entity) |
||
245 | |||
246 | /** |
||
247 | * Return true if an object is an array or iterator |
||
248 | * |
||
249 | * @param mixed $argument |
||
250 | * @return boolean |
||
251 | */ |
||
252 | public function isTraversable($argument) |
||
256 | |||
257 | /** |
||
258 | * Set strict mode for entityMap instantiation |
||
259 | * |
||
260 | * @param boolean $mode |
||
261 | */ |
||
262 | public function setStrictMode($mode) |
||
266 | |||
267 | /** |
||
268 | * Register an entity |
||
269 | * |
||
270 | * @param string|\Analogue\ORM\Mappable $entity entity's class name |
||
271 | * @param string|EntityMap $entityMap map's class name |
||
272 | * @throws MappingException |
||
273 | * @return void |
||
274 | */ |
||
275 | public function register($entity, $entityMap = null) |
||
308 | |||
309 | /** |
||
310 | * Get the entity map instance for a custom entity |
||
311 | * |
||
312 | * @param string $entity |
||
313 | * @return \Analogue\ORM\Mappable |
||
314 | */ |
||
315 | protected function getEntityMapInstanceFor($entity) |
||
329 | |||
330 | /** |
||
331 | * Dynamically create an entity map for a custom entity class |
||
332 | * |
||
333 | * @return EntityMap |
||
334 | */ |
||
335 | protected function getNewEntityMap() |
||
339 | |||
340 | /** |
||
341 | * Return the Singleton instance of the manager |
||
342 | * |
||
343 | * @return Manager |
||
344 | */ |
||
345 | public static function getInstance() |
||
349 | |||
350 | /** |
||
351 | * Return the Driver Manager's instance |
||
352 | * |
||
353 | * @return \Analogue\ORM\Drivers\Manager |
||
354 | */ |
||
355 | public function getDriverManager() |
||
359 | |||
360 | /** |
||
361 | * Get the Repository instance for the given Entity |
||
362 | * |
||
363 | * @param \Analogue\ORM\Mappable|string $entity |
||
364 | * @throws \InvalidArgumentException |
||
365 | * @throws MappingException |
||
366 | * @return \Analogue\ORM\Repository |
||
367 | */ |
||
368 | public function repository($entity) |
||
383 | |||
384 | /** |
||
385 | * Return true is the object is registered as value object |
||
386 | * |
||
387 | * @param mixed $object |
||
388 | * @return boolean |
||
389 | */ |
||
390 | public function isValueObject($object) |
||
398 | |||
399 | /** |
||
400 | * Get the Value Map for a given Value Object Class |
||
401 | * |
||
402 | * @param string $valueObject |
||
403 | * @throws MappingException |
||
404 | * @return \Analogue\ORM\ValueMap |
||
405 | */ |
||
406 | public function getValueMap($valueObject) |
||
421 | |||
422 | /** |
||
423 | * Register a Value Object |
||
424 | * |
||
425 | * @param string $valueObject |
||
426 | * @param string $valueMap |
||
427 | * @throws MappingException |
||
428 | * @return void |
||
429 | */ |
||
430 | public function registerValueObject($valueObject, $valueMap = null) |
||
446 | |||
447 | /** |
||
448 | * Instantiate a new Value Object instance |
||
449 | * |
||
450 | * @param string $valueObject |
||
451 | * @return \Analogue\ORM\ValueObject |
||
452 | */ |
||
453 | public function getValueObjectInstance($valueObject) |
||
459 | |||
460 | /** |
||
461 | * Register Analogue Plugin |
||
462 | * |
||
463 | * @param string $plugin class |
||
464 | * @return void |
||
465 | */ |
||
466 | public function registerPlugin($plugin) |
||
474 | |||
475 | /** |
||
476 | * Register event listeners that will be fired regardless the type |
||
477 | * of the entity. |
||
478 | * |
||
479 | * @param string $event |
||
480 | * @param \Closure $callback |
||
481 | * @throws \Exception |
||
482 | * @return void |
||
483 | */ |
||
484 | public function registerGlobalEvent($event, $callback) |
||
491 | |||
492 | /** |
||
493 | * Shortcut to Mapper store |
||
494 | * |
||
495 | * @param mixed $entity |
||
496 | * @throws MappingException |
||
497 | * @return mixed |
||
498 | */ |
||
499 | public function store($entity) |
||
503 | |||
504 | /** |
||
505 | * Shortcut to Mapper delete |
||
506 | * |
||
507 | * @param mixed $entity |
||
508 | * @throws MappingException |
||
509 | * @return \Illuminate\Support\Collection|null |
||
510 | */ |
||
511 | public function delete($entity) |
||
515 | |||
516 | /** |
||
517 | * Shortcut to Mapper query |
||
518 | * |
||
519 | * @param mixed $entity |
||
520 | * @throws MappingException |
||
521 | * @return Query |
||
522 | */ |
||
523 | public function query($entity) |
||
527 | |||
528 | /** |
||
529 | * Shortcut to Mapper Global Query |
||
530 | * |
||
531 | * @param mixed $entity |
||
532 | * @throws MappingException |
||
533 | * @return Query |
||
534 | */ |
||
535 | public function globalQuery($entity) |
||
539 | |||
540 | /** |
||
541 | * @param array $morphMap |
||
542 | * @return $this |
||
543 | */ |
||
544 | public function morphMap(array $morphMap) |
||
550 | |||
551 | /** |
||
552 | * @param string $class |
||
553 | * @return mixed |
||
554 | */ |
||
555 | public function getMorphMap($class) |
||
561 | } |
||
562 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: