| Total Complexity | 115 |
| Total Lines | 766 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 2 | Features | 2 |
Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Page extends PDFObject |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var Font[] |
||
| 45 | */ |
||
| 46 | protected $fonts = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var PDFObject[] |
||
| 50 | */ |
||
| 51 | protected $xobjects = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var[] |
||
| 55 | */ |
||
| 56 | protected $dataTm = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return Font[] |
||
| 60 | */ |
||
| 61 | public function getFonts() |
||
| 62 | { |
||
| 63 | if (null !== $this->fonts) { |
||
| 64 | return $this->fonts; |
||
| 65 | } |
||
| 66 | |||
| 67 | $resources = $this->get('Resources'); |
||
| 68 | |||
| 69 | if (method_exists($resources, 'has') && $resources->has('Font')) { |
||
| 70 | if ($resources->get('Font') instanceof Header) { |
||
|
|
|||
| 71 | $fonts = $resources->get('Font')->getElements(); |
||
| 72 | } else { |
||
| 73 | $fonts = $resources->get('Font')->getHeader()->getElements(); |
||
| 74 | } |
||
| 75 | |||
| 76 | $table = []; |
||
| 77 | |||
| 78 | foreach ($fonts as $id => $font) { |
||
| 79 | if ($font instanceof Font) { |
||
| 80 | $table[$id] = $font; |
||
| 81 | |||
| 82 | // Store too on cleaned id value (only numeric) |
||
| 83 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 84 | if ('' != $id) { |
||
| 85 | $table[$id] = $font; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->fonts = $table; |
||
| 91 | } else { |
||
| 92 | return []; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $id |
||
| 98 | * |
||
| 99 | * @return Font |
||
| 100 | */ |
||
| 101 | public function getFont($id) |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Support for XObject |
||
| 120 | * |
||
| 121 | * @return PDFObject[] |
||
| 122 | */ |
||
| 123 | public function getXObjects() |
||
| 124 | { |
||
| 125 | if (null !== $this->xobjects) { |
||
| 126 | return $this->xobjects; |
||
| 127 | } |
||
| 128 | |||
| 129 | $resources = $this->get('Resources'); |
||
| 130 | |||
| 131 | if (method_exists($resources, 'has') && $resources->has('XObject')) { |
||
| 132 | if ($resources->get('XObject') instanceof Header) { |
||
| 133 | $xobjects = $resources->get('XObject')->getElements(); |
||
| 134 | } else { |
||
| 135 | $xobjects = $resources->get('XObject')->getHeader()->getElements(); |
||
| 136 | } |
||
| 137 | |||
| 138 | $table = []; |
||
| 139 | |||
| 140 | foreach ($xobjects as $id => $xobject) { |
||
| 141 | $table[$id] = $xobject; |
||
| 142 | |||
| 143 | // Store too on cleaned id value (only numeric) |
||
| 144 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 145 | if ('' != $id) { |
||
| 146 | $table[$id] = $xobject; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return $this->xobjects = $table; |
||
| 151 | } else { |
||
| 152 | return []; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $id |
||
| 158 | * |
||
| 159 | * @return PDFObject |
||
| 160 | */ |
||
| 161 | public function getXObject($id) |
||
| 162 | { |
||
| 163 | $xobjects = $this->getXObjects(); |
||
| 164 | |||
| 165 | if (isset($xobjects[$id])) { |
||
| 166 | return $xobjects[$id]; |
||
| 167 | } else { |
||
| 168 | return null; |
||
| 169 | /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 170 | |||
| 171 | if (isset($xobjects[$id])) { |
||
| 172 | return $xobjects[$id]; |
||
| 173 | } else { |
||
| 174 | return null; |
||
| 175 | }*/ |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param Page |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getText(self $page = null) |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Page |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function getTextArray(self $page = null) |
||
| 273 | } |
||
| 274 | |||
| 275 | /* |
||
| 276 | * Gets all the text data with its internal representation of the page. |
||
| 277 | * |
||
| 278 | * @return array An array with the data and the internal representation |
||
| 279 | * |
||
| 280 | */ |
||
| 281 | |||
| 282 | public function extractRawData() |
||
| 283 | { |
||
| 284 | $text = $this->getText(); |
||
| 285 | /* |
||
| 286 | * Now you can get the complete content of the object with the text on it |
||
| 287 | */ |
||
| 288 | $extractedData = []; |
||
| 289 | $content = $this->get('Contents'); |
||
| 290 | $values = $content->getContent(); |
||
| 291 | if (isset($values) and \is_array($values)) { |
||
| 292 | $text = ''; |
||
| 293 | foreach ($values as $section) { |
||
| 294 | $text .= $section->getContent(); |
||
| 295 | } |
||
| 296 | $sectionsText = $this->getSectionsText($text); |
||
| 297 | foreach ($sectionsText as $sectionText) { |
||
| 298 | $commandsText = $this->getCommandsText($sectionText); |
||
| 299 | foreach ($commandsText as $command) { |
||
| 300 | $extractedData[] = $command; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | $sectionsText = $content->getSectionsText($content->getContent()); |
||
| 305 | foreach ($sectionsText as $sectionText) { |
||
| 306 | $commandsText = $content->getCommandsText($sectionText); |
||
| 307 | foreach ($commandsText as $command) { |
||
| 308 | $extractedData[] = $command; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | return $extractedData; |
||
| 314 | } |
||
| 315 | |||
| 316 | /* |
||
| 317 | * Gets all the decoded text data with it internal representation from a page. |
||
| 318 | * |
||
| 319 | * @param array $extractedRawData the extracted data return by extractRawData or |
||
| 320 | * null if extractRawData should be called |
||
| 321 | * |
||
| 322 | * @return array An array with the data and the internal representation |
||
| 323 | * |
||
| 324 | */ |
||
| 325 | public function extractDecodedRawData($extractedRawData = null) |
||
| 326 | { |
||
| 327 | if (!isset($extractedRawData) or !$extractedRawData) { |
||
| 328 | $extractedRawData = $this->extractRawData(); |
||
| 329 | } |
||
| 330 | $unicode = true; |
||
| 331 | $currentFont = null; |
||
| 332 | foreach ($extractedRawData as &$command) { |
||
| 333 | if ('Tj' == $command['o'] or 'TJ' == $command['o']) { |
||
| 334 | $text = []; |
||
| 335 | $data = $command['c']; |
||
| 336 | if (!\is_array($data)) { |
||
| 337 | if (isset($currentFont)) { |
||
| 338 | $tmpText = $currentFont->decodeOctal($data); |
||
| 339 | //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
| 340 | } |
||
| 341 | $tmpText = $tjText = str_replace( |
||
| 342 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 343 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 344 | $tmpText |
||
| 345 | ); |
||
| 346 | $tmpText = utf8_encode($tmpText); |
||
| 347 | if (isset($currentFont)) { |
||
| 348 | $tmpText = $currentFont->decodeContent($tmpText, $unicode); |
||
| 349 | } |
||
| 350 | $command['c'] = $tmpText; |
||
| 351 | continue; |
||
| 352 | } |
||
| 353 | $numText = \count($data); |
||
| 354 | for ($i = 0; $i < $numText; ++$i) { |
||
| 355 | if (0 != ($i % 2)) { |
||
| 356 | continue; |
||
| 357 | } |
||
| 358 | $tmpText = $data[$i]['c']; |
||
| 359 | if (isset($currentFont)) { |
||
| 360 | $decodedText = $currentFont->decodeOctal($tmpText); |
||
| 361 | //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
| 362 | } |
||
| 363 | $decodedText = $tjText = str_replace( |
||
| 364 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 365 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 366 | $decodedText |
||
| 367 | ); |
||
| 368 | $decodedText = utf8_encode($decodedText); |
||
| 369 | if (isset($currentFont)) { |
||
| 370 | $decodedText = $currentFont->decodeContent($decodedText, $unicode); |
||
| 371 | } |
||
| 372 | $command['c'][$i]['c'] = $decodedText; |
||
| 373 | continue; |
||
| 374 | } |
||
| 375 | } elseif ('Tf' == $command['o'] or 'TF' == $command['o']) { |
||
| 376 | $fontId = explode(' ', $command['c'])[0]; |
||
| 377 | $currentFont = $this->getFont($fontId); |
||
| 378 | continue; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | return $extractedRawData; |
||
| 383 | } |
||
| 384 | |||
| 385 | /* |
||
| 386 | * Gets just the Text commands that are involved in text positions and |
||
| 387 | * Text Matrix (Tm) |
||
| 388 | * |
||
| 389 | * It extract just the PDF commands that are involved with text positions, and |
||
| 390 | * the Text Matrix (Tm). These are: BT, ET, TL, Td, TD, Tm, T*, Tj, ', ", and TJ |
||
| 391 | * |
||
| 392 | * @param array $extractedDecodedRawData The data extracted by extractDecodeRawData |
||
| 393 | if it is null, the method extractDecodeRawData is called. |
||
| 394 | * |
||
| 395 | * @return array An array with the text command of the page |
||
| 396 | * |
||
| 397 | */ |
||
| 398 | public function getDataCommands($extractedDecodedRawData = null) |
||
| 528 | } |
||
| 529 | |||
| 530 | /* |
||
| 531 | * Gets the Text Matrix of the text in the page |
||
| 532 | * |
||
| 533 | * Return an array where every item is an array where the first item is the |
||
| 534 | * Text Matrix (Tm) and the second is a string with the text data. The Text matrix |
||
| 535 | * is an array of 6 numbers. The last 2 numbers are the coordinates X and Y of the |
||
| 536 | * text. The first 4 numbers has to be with Scalation, Rotation and Skew of the text. |
||
| 537 | * |
||
| 538 | * @param array $dataCommands the data extracted by getDataCommands |
||
| 539 | * if null getDataCommands is called. |
||
| 540 | * |
||
| 541 | * @return array An array with the data of the page including the Tm information |
||
| 542 | * of any text in the page. |
||
| 543 | */ |
||
| 544 | |||
| 545 | public function getDataTm($dataCommands = null) |
||
| 732 | } |
||
| 733 | |||
| 734 | /* |
||
| 735 | * Gets text data that are around the given coordinates (X,Y) |
||
| 736 | * |
||
| 737 | * If the text is in near the given coordinates (X,Y) (or the TM info), |
||
| 738 | * the text is returned. The extractedData return by getDataTm, could be use to see |
||
| 739 | * where is the coordinates of a given text, using the TM info for it. |
||
| 740 | * |
||
| 741 | * @param float $x The X value of the coordinate to search for. if null |
||
| 742 | * just the Y value is considered (same Row) |
||
| 743 | * @param float $y The Y value of the coordinate to search for |
||
| 744 | * just the X value is considered (same column) |
||
| 745 | * @param float $xError The value less or more to consider an X to be "near" |
||
| 746 | * @param float $yError The value less or more to consider an Y to be "near" |
||
| 747 | * |
||
| 748 | * @return array An array of text that are near the given coordinates. If no text |
||
| 749 | * "near" the x,y coordinate, an empty array is returned. If Both, x |
||
| 750 | * and y coordinates are null, null is returned. |
||
| 751 | */ |
||
| 752 | public function getTextXY($x, $y, $xError = 0, $yError = 0) |
||
| 809 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.