Complex classes like JsonLD 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 JsonLD, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | class JsonLD extends AbstractParser |
||
61 | { |
||
62 | /** |
||
63 | * Format |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | const FORMAT = Format::JSON_LD; |
||
68 | /** |
||
69 | * Regex pattern for matching leading comments in a JSON string |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const JSON_COMMENT_PATTERN = '#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#'; |
||
74 | /** |
||
75 | * Vocabulary cache |
||
76 | * |
||
77 | * @var VocabularyCache |
||
78 | */ |
||
79 | protected $vocabularyCache; |
||
80 | /** |
||
81 | * Context loader |
||
82 | * |
||
83 | * @var CachingContextLoader |
||
84 | */ |
||
85 | protected $contextLoader; |
||
86 | /** |
||
87 | * Array for keeping track of the hierarchy of objects, to prevent recursion |
||
88 | * |
||
89 | * @var NodeInterface[] |
||
90 | */ |
||
91 | protected $chain = []; |
||
92 | |||
93 | /** |
||
94 | * JSON-LD parser constructor |
||
95 | * |
||
96 | * @param UriInterface $uri Base URI |
||
97 | * @param LoggerInterface $logger Logger |
||
98 | */ |
||
99 | 7 | public function __construct(UriInterface $uri, LoggerInterface $logger) |
|
105 | |||
106 | /** |
||
107 | * Parse a DOM document |
||
108 | * |
||
109 | * @param \DOMDocument $dom DOM Document |
||
110 | * |
||
111 | * @return ParsingResultInterface Micro information items |
||
112 | * @throws \ReflectionException |
||
113 | */ |
||
114 | 6 | public function parseDom(\DOMDocument $dom) |
|
133 | |||
134 | /** |
||
135 | * Parse a JSON-LD document |
||
136 | * |
||
137 | * @param string $jsonLDDocSource JSON-LD document |
||
138 | * |
||
139 | * @return array Items |
||
140 | */ |
||
141 | 6 | protected function parseDocument($jsonLDDocSource) |
|
161 | |||
162 | /** |
||
163 | * Parse a JSON-LD root node |
||
164 | * |
||
165 | * @param \stdClass $jsonLDRoot JSON-LD root node |
||
166 | */ |
||
167 | 4 | protected function parseRootNode($jsonLDRoot) |
|
186 | |||
187 | /** |
||
188 | * Parse a JSON-LD node |
||
189 | * |
||
190 | * @param NodeInterface $node Node |
||
191 | * |
||
192 | * @return \stdClass|string Item or string ID |
||
193 | */ |
||
194 | 4 | protected function parseNode(NodeInterface $node) |
|
214 | |||
215 | /** |
||
216 | * Parse the type of a JSON-LD node |
||
217 | * |
||
218 | * @param NodeInterface $node Node |
||
219 | * |
||
220 | * @return array Item type |
||
221 | */ |
||
222 | 4 | protected function parseNodeType(NodeInterface $node): array |
|
244 | |||
245 | /** |
||
246 | * Parse the properties of a JSON-LD node |
||
247 | * |
||
248 | * @param NodeInterface $node Node |
||
249 | * |
||
250 | * @return array Item properties |
||
251 | */ |
||
252 | 4 | protected function parseNodeProperties(NodeInterface $node) |
|
272 | |||
273 | /** |
||
274 | * Initialize a JSON-LD node property (if necessary) |
||
275 | * |
||
276 | * @param string $name Property name |
||
277 | * @param array $properties Item properties |
||
278 | */ |
||
279 | 3 | protected function initializeNodeProperty($name, array &$properties) |
|
286 | |||
287 | /** |
||
288 | * Process a property value |
||
289 | * |
||
290 | * @param string $name Property name |
||
291 | * @param \stdClass|array|string $value Property value |
||
292 | * @param array $properties Item properties |
||
293 | */ |
||
294 | 3 | protected function processNodeProperty($name, $value, array &$properties) |
|
311 | |||
312 | /** |
||
313 | * Process a property value object |
||
314 | * |
||
315 | * @param string $name Property name |
||
316 | * @param \stdClass $value Property value |
||
317 | * @param array $properties Properties |
||
318 | */ |
||
319 | 2 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
329 | |||
330 | /** |
||
331 | * Parse a JSON-LD fragment |
||
332 | * |
||
333 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
334 | * |
||
335 | * @return \stdClass|string|array Parsed fragment |
||
336 | */ |
||
337 | 3 | protected function parse($jsonLD) |
|
355 | |||
356 | /** |
||
357 | * Parse a language tagged string |
||
358 | * |
||
359 | * @param LanguageTaggedString $value Language tagged string |
||
360 | * |
||
361 | * @return \stdClass Value |
||
362 | */ |
||
363 | 1 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
|
367 | |||
368 | /** |
||
369 | * Parse a typed value |
||
370 | * |
||
371 | * @param TypedValue $value Typed value |
||
372 | * |
||
373 | * @return string Value |
||
374 | */ |
||
375 | 2 | protected function parseTypedValue(TypedValue $value) |
|
379 | |||
380 | 6 | private function sanitizeJsonSource($jsonLDDocSource) |
|
390 | } |
||
391 |