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 |
||
16 | class Manager |
||
17 | { |
||
18 | /** |
||
19 | * Driver Manager |
||
20 | * |
||
21 | * @var \Analogue\ORM\Drivers\Manager |
||
22 | */ |
||
23 | protected $drivers; |
||
24 | |||
25 | /** |
||
26 | * Registered entity classes and corresponding map objects. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $entityClasses = []; |
||
31 | |||
32 | /** |
||
33 | * Key value store of ValueObject Classes and corresponding map classes |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $valueClasses = []; |
||
38 | |||
39 | /** |
||
40 | * Morph map |
||
41 | */ |
||
42 | protected $morphMap = []; |
||
43 | |||
44 | /** |
||
45 | * Loaded Mappers |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $mappers = []; |
||
50 | |||
51 | /** |
||
52 | * Loaded Repositories |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $repositories = []; |
||
57 | |||
58 | /** |
||
59 | * Event dispatcher instance |
||
60 | * |
||
61 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
62 | */ |
||
63 | protected $eventDispatcher; |
||
64 | |||
65 | /** |
||
66 | * Manager instance |
||
67 | * |
||
68 | * @var Manager |
||
69 | */ |
||
70 | protected static $instance; |
||
71 | |||
72 | /** |
||
73 | * Available Analogue Events |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $events = [ |
||
78 | 'initializing', |
||
79 | 'initialized', |
||
80 | 'store', |
||
81 | 'stored', |
||
82 | 'creating', |
||
83 | 'created', |
||
84 | 'updating', |
||
85 | 'updated', |
||
86 | 'deleting', |
||
87 | 'deleted', |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * @param \Analogue\ORM\Drivers\Manager $driverManager |
||
92 | * @param Dispatcher $event |
||
93 | */ |
||
94 | public function __construct(DriverManager $driverManager, Dispatcher $event) |
||
102 | |||
103 | /** |
||
104 | * Return the Driver Manager's instance |
||
105 | * |
||
106 | * @return \Analogue\ORM\Drivers\Manager |
||
107 | */ |
||
108 | public function getDriverManager() |
||
112 | |||
113 | /** |
||
114 | * Create a mapper for a given entity |
||
115 | * |
||
116 | * @param \Analogue\ORM\Mappable|string|array|Collection $entity |
||
117 | * @param mixed $entityMap |
||
118 | * @throws MappingException |
||
119 | * @throws \InvalidArgumentException |
||
120 | * @return Mapper |
||
121 | */ |
||
122 | public function mapper($entity, $entityMap = null) |
||
154 | |||
155 | /** |
||
156 | * Build a new Mapper instance for a given Entity |
||
157 | * |
||
158 | * @param string $entity |
||
159 | * @param $entityMap |
||
160 | * @throws MappingException |
||
161 | * @return Mapper |
||
162 | */ |
||
163 | protected function buildMapper($entity, $entityMap) |
||
186 | |||
187 | /** |
||
188 | * Create a mapper for a given entity (static alias) |
||
189 | * |
||
190 | * @param \Analogue\ORM\Mappable|string $entity |
||
191 | * @param null|EntityMap $entityMap |
||
192 | * @throws MappingException |
||
193 | * @return Mapper |
||
194 | */ |
||
195 | public static function getMapper($entity, $entityMap = null) |
||
199 | |||
200 | /** |
||
201 | * Get the Repository instance for the given Entity |
||
202 | * |
||
203 | * @param \Analogue\ORM\Mappable|string $entity |
||
204 | * @throws \InvalidArgumentException |
||
205 | * @throws MappingException |
||
206 | * @return \Analogue\ORM\Repository |
||
207 | */ |
||
208 | public function repository($entity) |
||
223 | |||
224 | /** |
||
225 | * Register an entity |
||
226 | * |
||
227 | * @param string|\Analogue\ORM\Mappable $entity entity's class name |
||
228 | * @param string|EntityMap $entityMap map's class name |
||
229 | * @throws MappingException |
||
230 | * @return void |
||
231 | */ |
||
232 | public function register($entity, $entityMap = null) |
||
265 | |||
266 | /** |
||
267 | * Get the entity map instance for a custom entity |
||
268 | * |
||
269 | * @param string $entity |
||
270 | * @return \Analogue\ORM\Mappable |
||
271 | */ |
||
272 | protected function getEntityMapInstanceFor($entity) |
||
284 | |||
285 | /** |
||
286 | * Dynamically create an entity map for a custom entity class |
||
287 | * |
||
288 | * @return EntityMap |
||
289 | */ |
||
290 | protected function getNewEntityMap() |
||
294 | |||
295 | /** |
||
296 | * Register a Value Object |
||
297 | * |
||
298 | * @param string $valueObject |
||
299 | * @param string $valueMap |
||
300 | * @throws MappingException |
||
301 | * @return void |
||
302 | */ |
||
303 | public function registerValueObject($valueObject, $valueMap = null) |
||
319 | |||
320 | /** |
||
321 | * Return true is the object is registered as value object |
||
322 | * |
||
323 | * @param mixed $object |
||
324 | * @return boolean |
||
325 | */ |
||
326 | public function isValueObject($object) |
||
334 | |||
335 | /** |
||
336 | * Get the Value Map for a given Value Object Class |
||
337 | * |
||
338 | * @param string $valueObject |
||
339 | * @throws MappingException |
||
340 | * @return \Analogue\ORM\ValueMap |
||
341 | */ |
||
342 | public function getValueMap($valueObject) |
||
357 | |||
358 | /** |
||
359 | * Instantiate a new Value Object instance |
||
360 | * |
||
361 | * @param string $valueObject |
||
362 | * @return \Analogue\ORM\ValueObject |
||
363 | */ |
||
364 | public function getValueObjectInstance($valueObject) |
||
369 | |||
370 | /** |
||
371 | * Register Analogue Plugin |
||
372 | * |
||
373 | * @param string $plugin class |
||
374 | * @return void |
||
375 | */ |
||
376 | public function registerPlugin($plugin) |
||
384 | |||
385 | /** |
||
386 | * Check if the entity is already registered |
||
387 | * |
||
388 | * @param string|object $entity |
||
389 | * @return boolean |
||
390 | */ |
||
391 | public function isRegisteredEntity($entity) |
||
399 | |||
400 | /** |
||
401 | * Register event listeners that will be fired regardless the type |
||
402 | * of the entity. |
||
403 | * |
||
404 | * @param string $event |
||
405 | * @param \Closure $callback |
||
406 | * @throws \Exception |
||
407 | * @return void |
||
408 | */ |
||
409 | public function registerGlobalEvent($event, $callback) |
||
416 | |||
417 | /** |
||
418 | * Shortcut to Mapper store |
||
419 | * |
||
420 | * @param mixed $entity |
||
421 | * @throws MappingException |
||
422 | * @return mixed |
||
423 | */ |
||
424 | public function store($entity) |
||
428 | |||
429 | /** |
||
430 | * Shortcut to Mapper delete |
||
431 | * |
||
432 | * @param mixed $entity |
||
433 | * @throws MappingException |
||
434 | * @return \Illuminate\Support\Collection|null |
||
435 | */ |
||
436 | public function delete($entity) |
||
440 | |||
441 | /** |
||
442 | * Shortcut to Mapper query |
||
443 | * |
||
444 | * @param mixed $entity |
||
445 | * @throws MappingException |
||
446 | * @return Query |
||
447 | */ |
||
448 | public function query($entity) |
||
452 | |||
453 | /** |
||
454 | * Shortcut to Mapper Global Query |
||
455 | * |
||
456 | * @param mixed $entity |
||
457 | * @throws MappingException |
||
458 | * @return Query |
||
459 | */ |
||
460 | public function globalQuery($entity) |
||
464 | |||
465 | public function morphMap(array $morphMap) |
||
470 | |||
471 | public function getMorphMap($class) |
||
476 | |||
477 | public function getInverseMorphMap($key) |
||
481 | |||
482 | /** |
||
483 | * Return the Singleton instance of the manager |
||
484 | * |
||
485 | * @return Manager |
||
486 | */ |
||
487 | public static function getInstance() |
||
491 | } |
||
492 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.