| Total Complexity | 44 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 18 | ||
| Bugs | 4 | Features | 2 |
Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Parser |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var PDFObject[] |
||
| 52 | */ |
||
| 53 | protected $objects = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | public function __construct() |
||
| 59 | { |
||
| 60 | |||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param $filename |
||
| 65 | * @return Document |
||
| 66 | * @throws \Exception |
||
| 67 | */ |
||
| 68 | public function parseFile($filename) |
||
| 69 | { |
||
| 70 | $content = file_get_contents($filename); |
||
| 71 | /* |
||
| 72 | * 2018/06/20 @doganoo as multiple times a |
||
| 73 | * users have complained that the parseFile() |
||
| 74 | * method dies silently, it is an better option |
||
| 75 | * to remove the error control operator (@) and |
||
| 76 | * let the users know that the method throws an exception |
||
| 77 | * by adding @throws tag to PHPDoc. |
||
| 78 | * |
||
| 79 | * See here for an example: https://github.com/smalot/pdfparser/issues/204 |
||
| 80 | */ |
||
| 81 | return $this->parseContent($content); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $content |
||
| 86 | * @return Document |
||
| 87 | * @throws \Exception |
||
| 88 | */ |
||
| 89 | public function parseContent($content) |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function parseTrailer($structure, $document) |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $id |
||
| 145 | * @param array $structure |
||
| 146 | * @param Document $document |
||
| 147 | */ |
||
| 148 | protected function parseObject($id, $structure, $document) |
||
| 149 | { |
||
| 150 | $header = new Header(array(), $document); |
||
| 151 | $content = ''; |
||
| 152 | |||
| 153 | foreach ($structure as $position => $part) { |
||
| 154 | switch ($part[0]) { |
||
| 155 | case '[': |
||
| 156 | $elements = array(); |
||
| 157 | |||
| 158 | foreach ($part[1] as $sub_element) { |
||
| 159 | $sub_type = $sub_element[0]; |
||
| 160 | $sub_value = $sub_element[1]; |
||
| 161 | $elements[] = $this->parseHeaderElement($sub_type, $sub_value, $document); |
||
| 162 | } |
||
| 163 | |||
| 164 | $header = new Header($elements, $document); |
||
| 165 | break; |
||
| 166 | |||
| 167 | case '<<': |
||
| 168 | $header = $this->parseHeader($part[1], $document); |
||
| 169 | break; |
||
| 170 | |||
| 171 | case 'stream': |
||
| 172 | $content = isset($part[3][0]) ? $part[3][0] : $part[1]; |
||
| 173 | |||
| 174 | if ($header->get('Type')->equals('ObjStm')) { |
||
| 175 | $match = array(); |
||
| 176 | |||
| 177 | // Split xrefs and contents. |
||
| 178 | preg_match('/^((\d+\s+\d+\s*)*)(.*)$/s', $content, $match); |
||
| 179 | $content = $match[3]; |
||
| 180 | |||
| 181 | // Extract xrefs. |
||
| 182 | $xrefs = preg_split( |
||
| 183 | '/(\d+\s+\d+\s*)/s', |
||
| 184 | $match[1], |
||
| 185 | -1, |
||
| 186 | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
||
| 187 | ); |
||
| 188 | $table = array(); |
||
| 189 | |||
| 190 | foreach ($xrefs as $xref) { |
||
| 191 | list($id, $position) = explode(' ', trim($xref)); |
||
| 192 | $table[$position] = $id; |
||
| 193 | } |
||
| 194 | |||
| 195 | ksort($table); |
||
| 196 | |||
| 197 | $ids = array_values($table); |
||
| 198 | $positions = array_keys($table); |
||
| 199 | |||
| 200 | foreach ($positions as $index => $position) { |
||
| 201 | $id = $ids[$index] . '_0'; |
||
| 202 | $next_position = isset($positions[$index + 1]) ? $positions[$index + 1] : strlen($content); |
||
| 203 | $sub_content = substr($content, $position, $next_position - $position); |
||
| 204 | |||
| 205 | $sub_header = Header::parse($sub_content, $document); |
||
| 206 | $object = PDFObject::factory($document, $sub_header, ''); |
||
| 207 | $this->objects[$id] = $object; |
||
| 208 | } |
||
| 209 | |||
| 210 | // It is not necessary to store this content. |
||
| 211 | $content = ''; |
||
| 212 | |||
| 213 | return; |
||
| 214 | } |
||
| 215 | break; |
||
| 216 | |||
| 217 | default: |
||
| 218 | if ($part != 'null') { |
||
| 219 | $element = $this->parseHeaderElement($part[0], $part[1], $document); |
||
| 220 | |||
| 221 | if ($element) { |
||
| 222 | $header = new Header(array($element), $document); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | break; |
||
| 226 | |||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if (!isset($this->objects[$id])) { |
||
| 231 | $this->objects[$id] = PDFObject::factory($document, $header, $content); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param array $structure |
||
| 237 | * @param Document $document |
||
| 238 | * |
||
| 239 | * @return Header |
||
| 240 | * @throws \Exception |
||
| 241 | */ |
||
| 242 | protected function parseHeader($structure, $document) |
||
| 243 | { |
||
| 244 | $elements = array(); |
||
| 245 | $count = count($structure); |
||
| 246 | |||
| 247 | for ($position = 0; $position < $count; $position += 2) { |
||
| 248 | $name = $structure[$position][1]; |
||
| 249 | $type = $structure[$position + 1][0]; |
||
| 250 | $value = $structure[$position + 1][1]; |
||
| 251 | |||
| 252 | $elements[$name] = $this->parseHeaderElement($type, $value, $document); |
||
| 253 | } |
||
| 254 | |||
| 255 | return new Header($elements, $document); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $type |
||
| 260 | * @param $value |
||
| 261 | * @param $document |
||
| 262 | * |
||
| 263 | * @return Element|Header |
||
| 264 | * @throws \Exception |
||
| 265 | */ |
||
| 266 | protected function parseHeaderElement($type, $value, $document) |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 |