Total Complexity | 67 |
Total Lines | 419 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 2 | Features | 0 |
Complex classes like Document 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.
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 Document, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class Document |
||
51 | { |
||
52 | /** |
||
53 | * @var PDFObject[] |
||
54 | */ |
||
55 | protected $objects = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $dictionary = []; |
||
61 | |||
62 | /** |
||
63 | * @var Header |
||
64 | */ |
||
65 | protected $trailer; |
||
66 | |||
67 | /** |
||
68 | * @var array<mixed> |
||
69 | */ |
||
70 | protected $metadata = []; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $details; |
||
76 | |||
77 | public function __construct() |
||
78 | { |
||
79 | $this->trailer = new Header([], $this); |
||
80 | } |
||
81 | |||
82 | public function init() |
||
83 | { |
||
84 | $this->buildDictionary(); |
||
85 | |||
86 | $this->buildDetails(); |
||
87 | |||
88 | // Propagate init to objects. |
||
89 | foreach ($this->objects as $object) { |
||
90 | $object->getHeader()->init(); |
||
91 | $object->init(); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Build dictionary based on type header field. |
||
97 | */ |
||
98 | protected function buildDictionary() |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Build details array. |
||
130 | */ |
||
131 | protected function buildDetails() |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Extract XMP Metadata |
||
203 | */ |
||
204 | public function extractXMPMetadata(string $content): void |
||
205 | { |
||
206 | $xml = xml_parser_create(); |
||
207 | xml_parser_set_option($xml, \XML_OPTION_SKIP_WHITE, 1); |
||
208 | |||
209 | if (1 === xml_parse_into_struct($xml, $content, $values, $index)) { |
||
210 | /* |
||
211 | * short overview about the following code parts: |
||
212 | * |
||
213 | * The output of xml_parse_into_struct is a single dimensional array (= $values), and the $stack is a last-on, |
||
214 | * first-off array of pointers to positions in $metadata, while iterating through it, that potentially turn the |
||
215 | * results into a more intuitive multi-dimensional array. When an "open" XML tag is encountered, |
||
216 | * we save the current $metadata context in the $stack, then create a child array of $metadata and |
||
217 | * make that the current $metadata context. When a "close" XML tag is encountered, the operations are |
||
218 | * reversed: the most recently added $metadata context from $stack (IOW, the parent of the current |
||
219 | * element) is set as the current $metadata context. |
||
220 | */ |
||
221 | $metadata = []; |
||
222 | $stack = []; |
||
223 | foreach ($values as $val) { |
||
224 | // Standardize to lowercase |
||
225 | $val['tag'] = strtolower($val['tag']); |
||
226 | |||
227 | // Ignore structural x: and rdf: XML elements |
||
228 | if (0 === strpos($val['tag'], 'x:')) { |
||
229 | continue; |
||
230 | } elseif (0 === strpos($val['tag'], 'rdf:') && 'rdf:li' != $val['tag']) { |
||
231 | continue; |
||
232 | } |
||
233 | |||
234 | switch ($val['type']) { |
||
235 | case 'open': |
||
236 | // Create an array of list items |
||
237 | if ('rdf:li' == $val['tag']) { |
||
238 | $metadata[] = []; |
||
239 | |||
240 | // Move up one level in the stack |
||
241 | $stack[\count($stack)] = &$metadata; |
||
242 | $metadata = &$metadata[\count($metadata) - 1]; |
||
243 | } else { |
||
244 | // Else create an array of named values |
||
245 | $metadata[$val['tag']] = []; |
||
246 | |||
247 | // Move up one level in the stack |
||
248 | $stack[\count($stack)] = &$metadata; |
||
249 | $metadata = &$metadata[$val['tag']]; |
||
250 | } |
||
251 | break; |
||
252 | |||
253 | case 'complete': |
||
254 | if (isset($val['value'])) { |
||
255 | // Assign a value to this list item |
||
256 | if ('rdf:li' == $val['tag']) { |
||
257 | $metadata[] = $val['value']; |
||
258 | |||
259 | // Else assign a value to this property |
||
260 | } else { |
||
261 | $metadata[$val['tag']] = $val['value']; |
||
262 | } |
||
263 | } |
||
264 | break; |
||
265 | |||
266 | case 'close': |
||
267 | // If the value of this property is an array |
||
268 | if (\is_array($metadata)) { |
||
269 | // If the value is a single element array |
||
270 | // where the element is of type string, use |
||
271 | // the value of the first list item as the |
||
272 | // value for this property |
||
273 | if (1 == \count($metadata) && isset($metadata[0]) && \is_string($metadata[0])) { |
||
274 | $metadata = $metadata[0]; |
||
275 | } elseif (0 == \count($metadata)) { |
||
276 | // if the value is an empty array, set |
||
277 | // the value of this property to the empty |
||
278 | // string |
||
279 | $metadata = ''; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | // Move down one level in the stack |
||
284 | $metadata = &$stack[\count($stack) - 1]; |
||
285 | unset($stack[\count($stack) - 1]); |
||
286 | break; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | // Only use this metadata if it's referring to a PDF |
||
291 | if (!isset($metadata['dc:format']) || 'application/pdf' == $metadata['dc:format']) { |
||
292 | // According to the XMP specifications: 'Conflict resolution |
||
293 | // for separate packets that describe the same resource is |
||
294 | // beyond the scope of this document.' - Section 6.1 |
||
295 | // Source: https://www.adobe.com/devnet/xmp.html |
||
296 | // Source: https://github.com/adobe/XMP-Toolkit-SDK/blob/main/docs/XMPSpecificationPart1.pdf |
||
297 | // So if there are multiple XMP blocks, just merge the values |
||
298 | // of each found block over top of the existing values |
||
299 | $this->metadata = array_merge($this->metadata, $metadata); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | // TODO: remove this if-clause and its content when dropping PHP 7 support |
||
304 | if (version_compare(PHP_VERSION, '8.0.0', '<')) { |
||
305 | // ref: https://www.php.net/manual/en/function.xml-parser-free.php |
||
306 | xml_parser_free($xml); |
||
307 | |||
308 | // to avoid memory leaks; documentation said: |
||
309 | // > it was necessary to also explicitly unset the reference to parser to avoid memory leaks |
||
310 | unset($xml); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | public function getDictionary(): array |
||
315 | { |
||
316 | return $this->dictionary; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param PDFObject[] $objects |
||
321 | */ |
||
322 | public function setObjects($objects = []) |
||
323 | { |
||
324 | $this->objects = (array) $objects; |
||
325 | |||
326 | $this->init(); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return PDFObject[] |
||
331 | */ |
||
332 | public function getObjects() |
||
333 | { |
||
334 | return $this->objects; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return PDFObject|Font|Page|Element|null |
||
339 | */ |
||
340 | public function getObjectById(string $id) |
||
341 | { |
||
342 | if (isset($this->objects[$id])) { |
||
343 | return $this->objects[$id]; |
||
344 | } |
||
345 | |||
346 | return null; |
||
347 | } |
||
348 | |||
349 | public function hasObjectsByType(string $type, ?string $subtype = null): bool |
||
350 | { |
||
351 | return 0 < \count($this->getObjectsByType($type, $subtype)); |
||
352 | } |
||
353 | |||
354 | public function getObjectsByType(string $type, ?string $subtype = null): array |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return Font[] |
||
373 | */ |
||
374 | public function getFonts() |
||
375 | { |
||
376 | return $this->getObjectsByType('Font'); |
||
377 | } |
||
378 | |||
379 | public function getFirstFont(): ?Font |
||
380 | { |
||
381 | $fonts = $this->getFonts(); |
||
382 | if ([] === $fonts) { |
||
383 | return null; |
||
384 | } |
||
385 | |||
386 | return reset($fonts); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return Page[] |
||
391 | * |
||
392 | * @throws MissingCatalogException |
||
393 | */ |
||
394 | public function getPages() |
||
429 | } |
||
430 | |||
431 | public function getText(?int $pageLimit = null): string |
||
432 | { |
||
433 | $texts = []; |
||
434 | $pages = $this->getPages(); |
||
435 | |||
436 | // Only use the first X number of pages if $pageLimit is set and numeric. |
||
437 | if (\is_int($pageLimit) && 0 < $pageLimit) { |
||
438 | $pages = \array_slice($pages, 0, $pageLimit); |
||
439 | } |
||
440 | |||
441 | foreach ($pages as $index => $page) { |
||
442 | /** |
||
443 | * In some cases, the $page variable may be null. |
||
444 | */ |
||
445 | if (null === $page) { |
||
446 | continue; |
||
447 | } |
||
448 | if ($text = trim($page->getText())) { |
||
449 | $texts[] = $text; |
||
450 | } |
||
451 | } |
||
452 | |||
453 | return implode("\n\n", $texts); |
||
454 | } |
||
455 | |||
456 | public function getTrailer(): Header |
||
457 | { |
||
458 | return $this->trailer; |
||
459 | } |
||
460 | |||
461 | public function setTrailer(Header $trailer) |
||
462 | { |
||
463 | $this->trailer = $trailer; |
||
464 | } |
||
465 | |||
466 | public function getDetails(): array |
||
469 | } |
||
470 | } |
||
471 |