Passed
Push — fix/invalid-characters ( f93ec3...37d7f1 )
by Jeremy
02:18
created

ElementArray::getDetails()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10.0363

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
cc 10
nc 7
nop 1
crap 10.0363

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 * @date    2017-01-03
9
 *
10
 * @license LGPLv3
11
 * @url     <https://github.com/smalot/pdfparser>
12
 *
13
 *  PdfParser is a pdf library written in PHP, extraction oriented.
14
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
15
 *
16
 *  This program is free software: you can redistribute it and/or modify
17
 *  it under the terms of the GNU Lesser General Public License as published by
18
 *  the Free Software Foundation, either version 3 of the License, or
19
 *  (at your option) any later version.
20
 *
21
 *  This program is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU Lesser General Public License for more details.
25
 *
26
 *  You should have received a copy of the GNU Lesser General Public License
27
 *  along with this program.
28
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
29
 */
30
31
namespace Smalot\PdfParser\Element;
32
33
use Smalot\PdfParser\Document;
34
use Smalot\PdfParser\Element;
35
use Smalot\PdfParser\Header;
36
use Smalot\PdfParser\PDFObject;
37
38
/**
39
 * Class ElementArray
40
 */
41
class ElementArray extends Element
42
{
43 44
    public function __construct($value, ?Document $document = null)
44
    {
45 44
        parent::__construct($value, $document);
46 44
    }
47
48 39
    public function getContent()
49
    {
50 39
        foreach ($this->value as $name => $element) {
51 39
            $this->resolveXRef($name);
52
        }
53
54 39
        return parent::getContent();
55
    }
56
57 7
    public function getRawContent(): array
58
    {
59 7
        return $this->value;
60
    }
61
62 2
    public function getDetails(bool $deep = true): array
63
    {
64 2
        $values = [];
65 2
        $elements = $this->getContent();
66
67 2
        foreach ($elements as $key => $element) {
68 2
            if ($element instanceof Header && $deep) {
69 1
                $values[$key] = $element->getDetails($deep);
70 2
            } elseif ($element instanceof PDFObject && $deep) {
71
                $values[$key] = $element->getDetails(false);
72 2
            } elseif ($element instanceof self) {
73 1
                if ($deep) {
74 1
                    $values[$key] = $element->getDetails();
75
                }
76 2
            } elseif ($element instanceof Element && !($element instanceof self)) {
77 2
                $values[$key] = $element->getContent();
78
            }
79
        }
80
81 2
        return $values;
82
    }
83
84 1
    public function __toString(): string
85
    {
86 1
        return implode(',', $this->value);
87
    }
88
89
    /**
90
     * @return Element|PDFObject
91
     */
92 39
    protected function resolveXRef(string $name)
93
    {
94 39
        if (($obj = $this->value[$name]) instanceof ElementXRef) {
95
            /** @var ElementXRef $obj */
96 37
            $obj = $this->document->getObjectById($obj->getId());
97 37
            $this->value[$name] = $obj;
98
        }
99
100 39
        return $this->value[$name];
101
    }
102
103
    /**
104
     * @todo: These methods return mixed and mismatched types throughout the hierarchy
105
     *
106
     * @return bool|ElementArray
107
     */
108 16
    public static function parse(string $content, ?Document $document = null, int &$offset = 0)
109
    {
110 16
        if (preg_match('/^\s*\[(?P<array>.*)/is', $content, $match)) {
111 15
            preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches);
112
113 15
            $level = 0;
114 15
            $sub = '';
115 15
            foreach ($matches[0] as $part) {
116 15
                $sub .= $part;
117 15
                $level += (false !== strpos($part, '[') ? 1 : -1);
118 15
                if ($level <= 0) {
119 15
                    break;
120
                }
121
            }
122
123
            // Removes 1 level [ and ].
124 15
            $sub = substr(trim($sub), 1, -1);
125 15
            $sub_offset = 0;
126 15
            $values = Element::parse($sub, $document, $sub_offset, true);
127
128 15
            $offset += strpos($content, '[') + 1;
129
            // Find next ']' position
130 15
            $offset += \strlen($sub) + 1;
131
132 15
            return new self($values, $document);
133
        }
134
135 6
        return false;
136
    }
137
}
138