Complex classes like DocumentParser 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 DocumentParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class DocumentParser |
||
30 | { |
||
31 | const OBJ_CACHED_FIELDS = 'ongr.obj_fields'; |
||
32 | const EMBEDDED_CACHED_FIELDS = 'ongr.embedded_fields'; |
||
33 | const ARRAY_CACHED_FIELDS = 'ongr.array_fields'; |
||
34 | |||
35 | private $reader; |
||
36 | private $properties = []; |
||
37 | private $methods = []; |
||
38 | private $analysisConfig = []; |
||
39 | private $cache; |
||
40 | |||
41 | public function __construct(Reader $reader, Cache $cache, array $analysisConfig = []) |
||
50 | |||
51 | public function getIndexAliasName(\ReflectionClass $class): string |
||
58 | |||
59 | public function isDefaultIndex(\ReflectionClass $class): bool |
||
66 | |||
67 | public function getIndexAnnotation(\ReflectionClass $class) |
||
74 | |||
75 | public function getIndexMetadata(\ReflectionClass $class): array |
||
76 | { |
||
77 | if ($class->isTrait()) { |
||
78 | return []; |
||
79 | } |
||
80 | |||
81 | /** @var Index $document */ |
||
82 | $document = $this->reader->getClassAnnotation($class, Index::class); |
||
83 | |||
84 | if ($document === null) { |
||
85 | return []; |
||
86 | } |
||
87 | |||
88 | $settings = $document->getSettings(); |
||
89 | $settings['analysis'] = $this->getAnalysisConfig($class); |
||
90 | |||
91 | return array_filter(array_map('array_filter', [ |
||
92 | 'settings' => $settings, |
||
93 | 'mappings' => [ |
||
94 | 'properties' => array_filter($this->getClassMetadata($class)) |
||
95 | ] |
||
96 | ])); |
||
97 | } |
||
98 | |||
99 | private function getClassMetadata(\ReflectionClass $class): array |
||
100 | { |
||
101 | $mapping = []; |
||
102 | $objFields = null; |
||
103 | $arrayFields = null; |
||
104 | $embeddedFields = null; |
||
105 | |||
106 | /** @var \ReflectionProperty $property */ |
||
107 | foreach ($this->getDocumentPropertiesReflection($class) as $name => $property) { |
||
108 | $annotations = $this->reader->getPropertyAnnotations($property); |
||
109 | $this->getPropertyMapping($annotations, $embeddedFields, $name, $mapping, $objFields, $arrayFields); |
||
110 | } |
||
111 | |||
112 | /** @var \ReflectionMethod $method */ |
||
113 | foreach ($this->getDocumentMethodsReflection($class) as $name => $method) { |
||
114 | $name = $this->guessPropertyNameFromGetter($name); |
||
115 | $annotations = $this->reader->getMethodAnnotations($method); |
||
116 | $this->getPropertyMapping($annotations, $embeddedFields, $name, $mapping, $objFields, $arrayFields); |
||
117 | } |
||
118 | |||
119 | //Embeded fields are option compared to the array or object mapping. |
||
120 | if ($embeddedFields) { |
||
121 | $cacheItem = $this->cache->fetch(self::EMBEDDED_CACHED_FIELDS) ?? []; |
||
122 | $cacheItem[$class->getName()] = $embeddedFields; |
||
123 | $t = $this->cache->save(self::EMBEDDED_CACHED_FIELDS, $cacheItem); |
||
124 | } |
||
125 | |||
126 | $cacheItem = $this->cache->fetch(self::ARRAY_CACHED_FIELDS) ?? []; |
||
127 | $cacheItem[$class->getName()] = $arrayFields; |
||
128 | $this->cache->save(self::ARRAY_CACHED_FIELDS, $cacheItem); |
||
129 | |||
130 | $cacheItem = $this->cache->fetch(self::OBJ_CACHED_FIELDS) ?? []; |
||
131 | $cacheItem[$class->getName()] = $objFields; |
||
132 | $this->cache->save(self::OBJ_CACHED_FIELDS, $cacheItem); |
||
133 | |||
134 | return $mapping; |
||
135 | } |
||
136 | |||
137 | public function getPropertyMetadata(\ReflectionClass $class, bool $subClass = false): array |
||
138 | { |
||
139 | if ($class->isTrait() || (!$this->reader->getClassAnnotation($class, Index::class) && !$subClass)) { |
||
140 | return []; |
||
141 | } |
||
142 | |||
143 | $metadata = []; |
||
144 | |||
145 | /** @var \ReflectionProperty $method */ |
||
146 | foreach ($this->getDocumentPropertiesReflection($class) as $name => $method) { |
||
147 | /** @var AbstractAnnotation $annotation */ |
||
148 | foreach ($this->reader->getPropertyAnnotations($method) as $annotation) { |
||
149 | if (!$annotation instanceof PropertiesAwareInterface) { |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | $propertyMetadata = [ |
||
154 | 'identifier' => false, |
||
155 | 'class' => null, |
||
156 | 'embeded' => false, |
||
157 | 'type' => null, |
||
158 | 'public' => $method->isPublic(), |
||
159 | 'getter' => null, |
||
160 | 'setter' => null, |
||
161 | 'sub_properties' => [] |
||
162 | ]; |
||
163 | |||
164 | $name = $method->getName(); |
||
165 | $propertyMetadata['name'] = $name; |
||
166 | |||
167 | if (!$propertyMetadata['public']) { |
||
168 | $propertyMetadata['getter'] = $this->guessGetter($class, $name); |
||
169 | } |
||
170 | |||
171 | if ($annotation instanceof Id) { |
||
172 | $propertyMetadata['identifier'] = true; |
||
173 | } else { |
||
174 | if (!$propertyMetadata['public']) { |
||
175 | $propertyMetadata['setter'] = $this->guessSetter($class, $name); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | if ($annotation instanceof Property) { |
||
180 | // we need the type (and possibly settings?) in Converter::denormalize() |
||
181 | $propertyMetadata['type'] = $annotation->type; |
||
182 | $propertyMetadata['settings'] = $annotation->settings; |
||
183 | } |
||
184 | |||
185 | if ($annotation instanceof Embedded) { |
||
186 | $propertyMetadata['embeded'] = true; |
||
187 | $propertyMetadata['class'] = $annotation->class; |
||
188 | $propertyMetadata['sub_properties'] = $this->getPropertyMetadata( |
||
189 | new \ReflectionClass($annotation->class), |
||
190 | true |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | $metadata[$annotation->getName() ?? Caser::snake($name)] = $propertyMetadata; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** @var \ReflectionProperty $property */ |
||
199 | foreach ($this->getDocumentMethodsReflection($class) as $name => $method) { |
||
200 | /** @var AbstractAnnotation $annotation */ |
||
201 | foreach ($this->reader->getMethodAnnotations($method) as $annotation) { |
||
202 | if (!$annotation instanceof PropertiesAwareInterface) { |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | $guessedName = $this->guessPropertyNameFromGetter($name); |
||
207 | $fieldName = $annotation->getName() ?? Caser::snake($guessedName); |
||
208 | |||
209 | $propertyMetadata = [ |
||
210 | 'identifier' => false, |
||
211 | 'class' => null, |
||
212 | 'embeded' => false, |
||
213 | 'type' => null, |
||
214 | 'public' => false, |
||
215 | 'getter' => $name, |
||
216 | 'setter' => null, |
||
217 | 'sub_properties' => [], |
||
218 | 'name' => $annotation->getName() ?? $guessedName |
||
219 | ]; |
||
220 | |||
221 | if ($annotation instanceof Id) { |
||
222 | $propertyMetadata['identifier'] = true; |
||
223 | } |
||
224 | |||
225 | if ($annotation instanceof Property) { |
||
226 | // we need the type (and possibly settings?) in Converter::denormalize() |
||
227 | $propertyMetadata['type'] = $annotation->type; |
||
228 | $propertyMetadata['settings'] = $annotation->settings; |
||
229 | } |
||
230 | |||
231 | if ($annotation instanceof Embedded) { |
||
232 | $propertyMetadata['embeded'] = true; |
||
233 | $propertyMetadata['class'] = $annotation->class; |
||
234 | $propertyMetadata['sub_properties'] = $this->getPropertyMetadata( |
||
235 | new \ReflectionClass($annotation->class), |
||
236 | true |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | $metadata[$fieldName] = $propertyMetadata; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | return $metadata; |
||
245 | } |
||
246 | |||
247 | public function getAnalysisConfig(\ReflectionClass $class): array |
||
248 | { |
||
249 | $config = []; |
||
250 | $mapping = $this->getClassMetadata($class); |
||
251 | |||
252 | //Think how to remove these array merge |
||
253 | $analyzers = $this->getListFromArrayByKey('analyzer', $mapping); |
||
254 | $analyzers = array_merge($analyzers, $this->getListFromArrayByKey('search_analyzer', $mapping)); |
||
255 | $analyzers = array_merge($analyzers, $this->getListFromArrayByKey('search_quote_analyzer', $mapping)); |
||
256 | |||
257 | foreach ($analyzers as $analyzer) { |
||
258 | if (isset($this->analysisConfig['analyzer'][$analyzer])) { |
||
259 | $config['analyzer'][$analyzer] = $this->analysisConfig['analyzer'][$analyzer]; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | $normalizers = $this->getListFromArrayByKey('normalizer', $mapping); |
||
264 | foreach ($normalizers as $normalizer) { |
||
265 | if (isset($this->analysisConfig['normalizer'][$normalizer])) { |
||
266 | $config['normalizer'][$normalizer] = $this->analysisConfig['normalizer'][$normalizer]; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | foreach (['tokenizer', 'filter', 'char_filter'] as $type) { |
||
271 | $list = $this->getListFromArrayByKey($type, $config); |
||
272 | |||
273 | foreach ($list as $listItem) { |
||
274 | if (isset($this->analysisConfig[$type][$listItem])) { |
||
275 | $config[$type][$listItem] = $this->analysisConfig[$type][$listItem]; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | return $config; |
||
281 | } |
||
282 | |||
283 | protected function guessGetter(\ReflectionClass $class, $name): string |
||
310 | |||
311 | protected function guessPropertyNameFromGetter($name): string |
||
312 | { |
||
313 | if (preg_match('/^get([A-Z_0-9].*?)$/', $name, $matches)) { |
||
314 | return lcfirst($matches[1]); |
||
319 | |||
320 | protected function guessSetter(\ReflectionClass $class, $name): string |
||
336 | |||
337 | private function getListFromArrayByKey(string $searchKey, array $array): array |
||
356 | |||
357 | private function getObjectMappingType(\ReflectionClass $class): string |
||
377 | |||
378 | private function getDocumentPropertiesReflection(\ReflectionClass $class): array |
||
411 | |||
412 | private function getDocumentMethodsReflection(\ReflectionClass $class): array |
||
438 | |||
439 | /** |
||
440 | * @param array $annotations |
||
441 | * @param $embeddedFields |
||
442 | * @param string $name |
||
443 | * @param array $mapping |
||
444 | * @param array $objFields |
||
445 | * @param array $arrayFields |
||
446 | * @throws \ReflectionException |
||
447 | */ |
||
448 | private function getPropertyMapping(array $annotations, &$embeddedFields, string $name, array &$mapping, ?array &$objFields, ?array &$arrayFields): void |
||
481 | } |
||
482 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.