| Total Complexity | 113 |
| Total Lines | 757 |
| 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 |
||
| 38 | class Page extends PDFObject |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var Font[] |
||
| 42 | */ |
||
| 43 | protected $fonts = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var PDFObject[] |
||
| 47 | */ |
||
| 48 | protected $xobjects = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $dataTm = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return Font[] |
||
| 57 | */ |
||
| 58 | public function getFonts() |
||
| 59 | { |
||
| 60 | if (null !== $this->fonts) { |
||
| 61 | return $this->fonts; |
||
| 62 | } |
||
| 63 | |||
| 64 | $resources = $this->get('Resources'); |
||
| 65 | |||
| 66 | if (method_exists($resources, 'has') && $resources->has('Font')) { |
||
| 67 | if ($resources->get('Font') instanceof Header) { |
||
|
|
|||
| 68 | $fonts = $resources->get('Font')->getElements(); |
||
| 69 | } else { |
||
| 70 | $fonts = $resources->get('Font')->getHeader()->getElements(); |
||
| 71 | } |
||
| 72 | |||
| 73 | $table = []; |
||
| 74 | |||
| 75 | foreach ($fonts as $id => $font) { |
||
| 76 | if ($font instanceof Font) { |
||
| 77 | $table[$id] = $font; |
||
| 78 | |||
| 79 | // Store too on cleaned id value (only numeric) |
||
| 80 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 81 | if ('' != $id) { |
||
| 82 | $table[$id] = $font; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return $this->fonts = $table; |
||
| 88 | } |
||
| 89 | |||
| 90 | return []; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $id |
||
| 95 | * |
||
| 96 | * @return Font|null |
||
| 97 | */ |
||
| 98 | public function getFont($id) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Support for XObject |
||
| 117 | * |
||
| 118 | * @return PDFObject[] |
||
| 119 | */ |
||
| 120 | public function getXObjects() |
||
| 121 | { |
||
| 122 | if (null !== $this->xobjects) { |
||
| 123 | return $this->xobjects; |
||
| 124 | } |
||
| 125 | |||
| 126 | $resources = $this->get('Resources'); |
||
| 127 | |||
| 128 | if (method_exists($resources, 'has') && $resources->has('XObject')) { |
||
| 129 | if ($resources->get('XObject') instanceof Header) { |
||
| 130 | $xobjects = $resources->get('XObject')->getElements(); |
||
| 131 | } else { |
||
| 132 | $xobjects = $resources->get('XObject')->getHeader()->getElements(); |
||
| 133 | } |
||
| 134 | |||
| 135 | $table = []; |
||
| 136 | |||
| 137 | foreach ($xobjects as $id => $xobject) { |
||
| 138 | $table[$id] = $xobject; |
||
| 139 | |||
| 140 | // Store too on cleaned id value (only numeric) |
||
| 141 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 142 | if ('' != $id) { |
||
| 143 | $table[$id] = $xobject; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $this->xobjects = $table; |
||
| 148 | } |
||
| 149 | |||
| 150 | return []; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $id |
||
| 155 | * |
||
| 156 | * @return PDFObject|null |
||
| 157 | */ |
||
| 158 | public function getXObject($id) |
||
| 159 | { |
||
| 160 | $xobjects = $this->getXObjects(); |
||
| 161 | |||
| 162 | if (isset($xobjects[$id])) { |
||
| 163 | return $xobjects[$id]; |
||
| 164 | } |
||
| 165 | |||
| 166 | return null; |
||
| 167 | /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 168 | |||
| 169 | if (isset($xobjects[$id])) { |
||
| 170 | return $xobjects[$id]; |
||
| 171 | } else { |
||
| 172 | return null; |
||
| 173 | }*/ |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param Page $page |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getText(self $page = null) |
||
| 182 | { |
||
| 183 | if ($contents = $this->get('Contents')) { |
||
| 184 | if ($contents instanceof ElementMissing) { |
||
| 185 | return ''; |
||
| 186 | } elseif ($contents instanceof ElementNull) { |
||
| 187 | return ''; |
||
| 188 | } elseif ($contents instanceof PDFObject) { |
||
| 189 | $elements = $contents->getHeader()->getElements(); |
||
| 190 | |||
| 191 | if (is_numeric(key($elements))) { |
||
| 192 | $new_content = ''; |
||
| 193 | |||
| 194 | foreach ($elements as $element) { |
||
| 195 | if ($element instanceof ElementXRef) { |
||
| 196 | $new_content .= $element->getObject()->getContent(); |
||
| 197 | } else { |
||
| 198 | $new_content .= $element->getContent(); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | $header = new Header([], $this->document); |
||
| 203 | $contents = new PDFObject($this->document, $header, $new_content); |
||
| 204 | } |
||
| 205 | } elseif ($contents instanceof ElementArray) { |
||
| 206 | // Create a virtual global content. |
||
| 207 | $new_content = ''; |
||
| 208 | |||
| 209 | foreach ($contents->getContent() as $content) { |
||
| 210 | $new_content .= $content->getContent()."\n"; |
||
| 211 | } |
||
| 212 | |||
| 213 | $header = new Header([], $this->document); |
||
| 214 | $contents = new PDFObject($this->document, $header, $new_content); |
||
| 215 | } |
||
| 216 | |||
| 217 | return $contents->getText($this); |
||
| 218 | } |
||
| 219 | |||
| 220 | return ''; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Page $page |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getTextArray(self $page = null) |
||
| 229 | { |
||
| 230 | if ($contents = $this->get('Contents')) { |
||
| 231 | if ($contents instanceof ElementMissing) { |
||
| 232 | return []; |
||
| 233 | } elseif ($contents instanceof ElementNull) { |
||
| 234 | return []; |
||
| 235 | } elseif ($contents instanceof PDFObject) { |
||
| 236 | $elements = $contents->getHeader()->getElements(); |
||
| 237 | |||
| 238 | if (is_numeric(key($elements))) { |
||
| 239 | $new_content = ''; |
||
| 240 | |||
| 241 | /** @var PDFObject $element */ |
||
| 242 | foreach ($elements as $element) { |
||
| 243 | if ($element instanceof ElementXRef) { |
||
| 244 | $new_content .= $element->getObject()->getContent(); |
||
| 245 | } else { |
||
| 246 | $new_content .= $element->getContent(); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $header = new Header([], $this->document); |
||
| 251 | $contents = new PDFObject($this->document, $header, $new_content); |
||
| 252 | } |
||
| 253 | } elseif ($contents instanceof ElementArray) { |
||
| 254 | // Create a virtual global content. |
||
| 255 | $new_content = ''; |
||
| 256 | |||
| 257 | /** @var PDFObject $content */ |
||
| 258 | foreach ($contents->getContent() as $content) { |
||
| 259 | $new_content .= $content->getContent()."\n"; |
||
| 260 | } |
||
| 261 | |||
| 262 | $header = new Header([], $this->document); |
||
| 263 | $contents = new PDFObject($this->document, $header, $new_content); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $contents->getTextArray($this); |
||
| 267 | } |
||
| 268 | |||
| 269 | return []; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Gets all the text data with its internal representation of the page. |
||
| 274 | * |
||
| 275 | * @return array An array with the data and the internal representation |
||
| 276 | */ |
||
| 277 | public function extractRawData() |
||
| 278 | { |
||
| 279 | /* |
||
| 280 | * Now you can get the complete content of the object with the text on it |
||
| 281 | */ |
||
| 282 | $extractedData = []; |
||
| 283 | $content = $this->get('Contents'); |
||
| 284 | $values = $content->getContent(); |
||
| 285 | if (isset($values) and \is_array($values)) { |
||
| 286 | $text = ''; |
||
| 287 | foreach ($values as $section) { |
||
| 288 | $text .= $section->getContent(); |
||
| 289 | } |
||
| 290 | $sectionsText = $this->getSectionsText($text); |
||
| 291 | foreach ($sectionsText as $sectionText) { |
||
| 292 | $commandsText = $this->getCommandsText($sectionText); |
||
| 293 | foreach ($commandsText as $command) { |
||
| 294 | $extractedData[] = $command; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } else { |
||
| 298 | $sectionsText = $content->getSectionsText($content->getContent()); |
||
| 299 | foreach ($sectionsText as $sectionText) { |
||
| 300 | $commandsText = $content->getCommandsText($sectionText); |
||
| 301 | foreach ($commandsText as $command) { |
||
| 302 | $extractedData[] = $command; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | return $extractedData; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Gets all the decoded text data with it internal representation from a page. |
||
| 312 | * |
||
| 313 | * @param array $extractedRawData the extracted data return by extractRawData or |
||
| 314 | * null if extractRawData should be called |
||
| 315 | * |
||
| 316 | * @return array An array with the data and the internal representation |
||
| 317 | */ |
||
| 318 | public function extractDecodedRawData($extractedRawData = null) |
||
| 319 | { |
||
| 320 | if (!isset($extractedRawData) or !$extractedRawData) { |
||
| 321 | $extractedRawData = $this->extractRawData(); |
||
| 322 | } |
||
| 323 | $unicode = true; |
||
| 324 | $currentFont = null; |
||
| 325 | foreach ($extractedRawData as &$command) { |
||
| 326 | if ('Tj' == $command['o'] or 'TJ' == $command['o']) { |
||
| 327 | $data = $command['c']; |
||
| 328 | if (!\is_array($data)) { |
||
| 329 | $tmpText = ''; |
||
| 330 | if (isset($currentFont)) { |
||
| 331 | $tmpText = $currentFont->decodeOctal($data); |
||
| 332 | //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
| 333 | } |
||
| 334 | $tmpText = str_replace( |
||
| 335 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 336 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 337 | $tmpText |
||
| 338 | ); |
||
| 339 | $tmpText = utf8_encode($tmpText); |
||
| 340 | if (isset($currentFont)) { |
||
| 341 | $tmpText = $currentFont->decodeContent($tmpText, $unicode); |
||
| 342 | } |
||
| 343 | $command['c'] = $tmpText; |
||
| 344 | continue; |
||
| 345 | } |
||
| 346 | $numText = \count($data); |
||
| 347 | for ($i = 0; $i < $numText; ++$i) { |
||
| 348 | if (0 != ($i % 2)) { |
||
| 349 | continue; |
||
| 350 | } |
||
| 351 | $tmpText = $data[$i]['c']; |
||
| 352 | $decodedText = ''; |
||
| 353 | if (isset($currentFont)) { |
||
| 354 | $decodedText = $currentFont->decodeOctal($tmpText); |
||
| 355 | //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
| 356 | } |
||
| 357 | $decodedText = str_replace( |
||
| 358 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 359 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 360 | $decodedText |
||
| 361 | ); |
||
| 362 | $decodedText = utf8_encode($decodedText); |
||
| 363 | if (isset($currentFont)) { |
||
| 364 | $decodedText = $currentFont->decodeContent($decodedText, $unicode); |
||
| 365 | } |
||
| 366 | $command['c'][$i]['c'] = $decodedText; |
||
| 367 | continue; |
||
| 368 | } |
||
| 369 | } elseif ('Tf' == $command['o'] or 'TF' == $command['o']) { |
||
| 370 | $fontId = explode(' ', $command['c'])[0]; |
||
| 371 | $currentFont = $this->getFont($fontId); |
||
| 372 | continue; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | return $extractedRawData; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Gets just the Text commands that are involved in text positions and |
||
| 381 | * Text Matrix (Tm) |
||
| 382 | * |
||
| 383 | * It extract just the PDF commands that are involved with text positions, and |
||
| 384 | * the Text Matrix (Tm). These are: BT, ET, TL, Td, TD, Tm, T*, Tj, ', ", and TJ |
||
| 385 | * |
||
| 386 | * @param array $extractedDecodedRawData The data extracted by extractDecodeRawData. |
||
| 387 | * If it is null, the method extractDecodeRawData is called. |
||
| 388 | * |
||
| 389 | * @return array An array with the text command of the page |
||
| 390 | */ |
||
| 391 | public function getDataCommands($extractedDecodedRawData = null) |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Gets the Text Matrix of the text in the page |
||
| 525 | * |
||
| 526 | * Return an array where every item is an array where the first item is the |
||
| 527 | * Text Matrix (Tm) and the second is a string with the text data. The Text matrix |
||
| 528 | * is an array of 6 numbers. The last 2 numbers are the coordinates X and Y of the |
||
| 529 | * text. The first 4 numbers has to be with Scalation, Rotation and Skew of the text. |
||
| 530 | * |
||
| 531 | * @param array $dataCommands the data extracted by getDataCommands |
||
| 532 | * if null getDataCommands is called |
||
| 533 | * |
||
| 534 | * @return array an array with the data of the page including the Tm information |
||
| 535 | * of any text in the page |
||
| 536 | */ |
||
| 537 | public function getDataTm($dataCommands = null) |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Gets text data that are around the given coordinates (X,Y) |
||
| 728 | * |
||
| 729 | * If the text is in near the given coordinates (X,Y) (or the TM info), |
||
| 730 | * the text is returned. The extractedData return by getDataTm, could be use to see |
||
| 731 | * where is the coordinates of a given text, using the TM info for it. |
||
| 732 | * |
||
| 733 | * @param float $x The X value of the coordinate to search for. if null |
||
| 734 | * just the Y value is considered (same Row) |
||
| 735 | * @param float $y The Y value of the coordinate to search for |
||
| 736 | * just the X value is considered (same column) |
||
| 737 | * @param float $xError The value less or more to consider an X to be "near" |
||
| 738 | * @param float $yError The value less or more to consider an Y to be "near" |
||
| 739 | * |
||
| 740 | * @return array An array of text that are near the given coordinates. If no text |
||
| 741 | * "near" the x,y coordinate, an empty array is returned. If Both, x |
||
| 742 | * and y coordinates are null, null is returned. |
||
| 743 | */ |
||
| 744 | public function getTextXY($x = null, $y = null, $xError = 0, $yError = 0) |
||
| 797 |
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.