Complex classes like Blameable 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 Blameable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | 1 | final class Blameable |
|
37 | { |
||
38 | /** |
||
39 | * Implement nette smart magic |
||
40 | */ |
||
41 | 1 | use Nette\SmartObject; |
|
42 | |||
43 | /** |
||
44 | * Annotation field is blameable |
||
45 | */ |
||
46 | private const EXTENSION_ANNOTATION = 'IPub\DoctrineBlameable\Mapping\Annotation\Blameable'; |
||
47 | |||
48 | /** |
||
49 | * @var DoctrineBlameable\Configuration |
||
50 | */ |
||
51 | private $configuration; |
||
52 | |||
53 | /** |
||
54 | * List of cached object configurations |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private static $objectConfigurations = []; |
||
59 | |||
60 | /** |
||
61 | * List of types which are valid for blame |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | private $validTypes = [ |
||
66 | 'one', |
||
67 | 'string', |
||
68 | 'int', |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * @param DoctrineBlameable\Configuration $configuration |
||
73 | */ |
||
74 | public function __construct(DoctrineBlameable\Configuration $configuration) |
||
78 | |||
79 | /** |
||
80 | * @param Common\Persistence\ObjectManager $objectManager |
||
81 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
82 | * |
||
83 | * @return void |
||
84 | * |
||
85 | * @throws ORM\Mapping\MappingException |
||
86 | */ |
||
87 | public function loadMetadataForObjectClass(Common\Persistence\ObjectManager $objectManager, ORM\Mapping\ClassMetadata $classMetadata) : void |
||
88 | { |
||
89 | 1 | if ($classMetadata->isMappedSuperclass) { |
|
90 | 1 | return; // Ignore mappedSuperclasses for now |
|
91 | } |
||
92 | |||
93 | // The annotation reader accepts a ReflectionClass, which can be |
||
94 | // obtained from the $classMetadata |
||
95 | 1 | $reflectionClass = $classMetadata->getReflectionClass(); |
|
96 | |||
97 | 1 | $config = []; |
|
98 | |||
99 | 1 | $useObjectName = $classMetadata->getName(); |
|
100 | |||
101 | // Collect metadata from inherited classes |
||
102 | 1 | if ($reflectionClass !== NULL) { |
|
103 | 1 | foreach (array_reverse(class_parents($classMetadata->getName())) as $parentClass) { |
|
104 | // Read only inherited mapped classes |
||
105 | if ($objectManager->getMetadataFactory()->hasMetadataFor($parentClass)) { |
||
106 | /** @var ORM\Mapping\ClassMetadata $parentClassMetadata */ |
||
107 | $parentClassMetadata = $objectManager->getClassMetadata($parentClass); |
||
108 | |||
109 | $config = $this->readExtendedMetadata($parentClassMetadata, $config); |
||
110 | |||
111 | $isBaseInheritanceLevel = !$parentClassMetadata->isInheritanceTypeNone() |
||
112 | && $parentClassMetadata->parentClasses !== [] |
||
113 | && $config !== []; |
||
114 | |||
115 | if ($isBaseInheritanceLevel === TRUE) { |
||
116 | $useObjectName = $reflectionClass->getName(); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | 1 | $config = $this->readExtendedMetadata($classMetadata, $config); |
|
122 | } |
||
123 | |||
124 | 1 | if ($config !== []) { |
|
125 | 1 | $config['useObjectClass'] = $useObjectName; |
|
126 | } |
||
127 | |||
128 | // Cache the metadata (even if it's empty) |
||
129 | // Caching empty metadata will prevent re-parsing non-existent annotations |
||
130 | 1 | $cacheId = self::getCacheId($classMetadata->getName()); |
|
131 | |||
132 | /** @var Common\Cache\Cache $cacheDriver */ |
||
133 | 1 | if ($cacheDriver = $objectManager->getMetadataFactory()->getCacheDriver()) { |
|
134 | 1 | $cacheDriver->save($cacheId, $config, NULL); |
|
135 | } |
||
136 | |||
137 | 1 | self::$objectConfigurations[$classMetadata->getName()] = $config; |
|
138 | 1 | } |
|
139 | |||
140 | /** |
||
141 | * @param ORM\Mapping\ClassMetadata $metadata |
||
142 | * @param array $config |
||
143 | * |
||
144 | * @return array |
||
145 | * |
||
146 | * @throws Common\Annotations\AnnotationException |
||
147 | * @throws ORM\Mapping\MappingException |
||
148 | */ |
||
149 | private function readExtendedMetadata(ORM\Mapping\ClassMetadata $metadata, array $config) : array |
||
243 | |||
244 | /** |
||
245 | * Get the configuration for specific object class |
||
246 | * if cache driver is present it scans it also |
||
247 | * |
||
248 | * @param Common\Persistence\ObjectManager $objectManager |
||
249 | * @param string $class |
||
250 | * |
||
251 | * @return array |
||
252 | * |
||
253 | * @throws ORM\Mapping\MappingException |
||
254 | */ |
||
255 | public function getObjectConfigurations(Common\Persistence\ObjectManager $objectManager, string $class) : array |
||
296 | |||
297 | /** |
||
298 | * Create default annotation reader for extensions |
||
299 | * |
||
300 | * @return Common\Annotations\CachedReader |
||
301 | * |
||
302 | * @throws Common\Annotations\AnnotationException |
||
303 | */ |
||
304 | private function getDefaultAnnotationReader() : Common\Annotations\CachedReader |
||
316 | |||
317 | /** |
||
318 | * Checks if $field type is valid |
||
319 | * |
||
320 | * @param ORM\Mapping\ClassMetadata $meta |
||
321 | * @param string $field |
||
322 | * |
||
323 | * @return bool |
||
324 | * |
||
325 | * @throws ORM\Mapping\MappingException |
||
326 | */ |
||
327 | private function isValidField(ORM\Mapping\ClassMetadata $meta, string $field) : bool |
||
333 | |||
334 | /** |
||
335 | * Get the cache id |
||
336 | * |
||
337 | * @param string $className |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | private static function getCacheId(string $className) : string |
||
345 | |||
346 | } |
||
347 |