Passed
Pull Request — master (#698)
by
unknown
02:57
created

Pages::getPages()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0796

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 2
b 0
f 0
nc 7
nop 1
dl 0
loc 31
ccs 15
cts 17
cp 0.8824
crap 7.0796
rs 8.8333
1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
8
 *
9
 * @date    2017-01-03
10
 *
11
 * @license LGPLv3
12
 *
13
 * @url     <https://github.com/smalot/pdfparser>
14
 *
15
 *  PdfParser is a pdf library written in PHP, extraction oriented.
16
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
17
 *
18
 *  This program is free software: you can redistribute it and/or modify
19
 *  it under the terms of the GNU Lesser General Public License as published by
20
 *  the Free Software Foundation, either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  This program is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU Lesser General Public License for more details.
27
 *
28
 *  You should have received a copy of the GNU Lesser General Public License
29
 *  along with this program.
30
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31
 */
32
33
namespace Smalot\PdfParser;
34
35
use Smalot\PdfParser\Element\ElementArray;
36
37
/**
38
 * Class Pages
39
 */
40
class Pages extends PDFObject
41
{
42
    /**
43
     * @var Font[]
44
     */
45
    protected $fonts;
46
47
    /**
48
     * @todo Objects other than Pages or Page might need to be treated specifically in order to get Page objects out of them,
49
     *
50
     * @see https://github.com/smalot/pdfparser/issues/331
51
     */
52 65
    public function getPages(bool $deep = false): array
53
    {
54 65
        if (!$this->has('Kids')) {
55
            return [];
56
        }
57
58
        /** @var ElementArray $kidsElement */
59 65
        $kidsElement = $this->get('Kids');
60
61 65
        if (!$deep) {
62
            return $kidsElement->getContent();
63
        }
64
65
        // Prepare to apply the Pages' object's fonts to each page
66 65
        $fonts = $this->getFonts();
67
68 65
        $kids = $kidsElement->getContent();
69 65
        $pages = [];
70
71 65
        foreach ($kids as $kid) {
72 65
            if ($kid instanceof self) {
73 4
                $pages = array_merge($pages, $kid->getPages(true));
74 65
            } elseif ($kid instanceof Page) {
75 65
                if (!empty($this->fonts)) {
76 8
                    $kid->setFonts($fonts);
77
                }
78 65
                $pages[] = $kid;
79
            }
80
        }
81
82 65
        return $pages;
83
    }
84
85
    /**
86
     * @return Font[]
87
     */
88 65
    public function getFonts()
89
    {
90 65
        if (null !== $this->fonts) {
91 7
            return $this->fonts;
92
        }
93
94 65
        $resources = $this->get('Resources');
95
96 65
        if (method_exists($resources, 'has') && $resources->has('Font')) {
97 8
            if ($resources->get('Font') instanceof ElementMissing) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on Smalot\PdfParser\Element. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            if ($resources->/** @scrutinizer ignore-call */ get('Font') instanceof ElementMissing) {

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.

Loading history...
Bug introduced by
The type Smalot\PdfParser\ElementMissing was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
                return [];
99
            }
100
101 8
            if ($resources->get('Font') instanceof Header) {
102 5
                $fonts = $resources->get('Font')->getElements();
103
            } else {
104 3
                $fonts = $resources->get('Font')->getHeader()->getElements();
105
            }
106
107 8
            $table = [];
108
109 8
            foreach ($fonts as $id => $font) {
110 8
                if ($font instanceof Font) {
111 8
                    $table[$id] = $font;
112
113
                    // Store too on cleaned id value (only numeric)
114 8
                    $id = preg_replace('/[^0-9\.\-_]/', '', $id);
115 8
                    if ('' != $id) {
116 8
                        $table[$id] = $font;
117
                    }
118
                }
119
            }
120
121 8
            return $this->fonts = $table;
122
        }
123
124 57
        return [];
125
    }
126
}
127