Passed
Push — master ( fdbbb5...1f4056 )
by Konrad
02:33
created

src/Smalot/PdfParser/Element/ElementHexa.php (3 issues)

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
35
/**
36
 * Class ElementHexa
37
 */
38
class ElementHexa extends ElementString
39
{
40
    /**
41
     * @param string   $content
42
     * @param Document $document
43
     * @param int      $offset
44
     *
45
     * @return bool|ElementHexa|ElementDate
46
     */
47 9
    public static function parse($content, Document $document = null, &$offset = 0)
48
    {
49 9
        if (preg_match('/^\s*\<(?P<name>[A-F0-9]+)\>/is', $content, $match)) {
50 2
            $name = $match['name'];
51 2
            $offset += strpos($content, '<'.$name) + \strlen($name) + 2; // 1 for '>'
52
            // repackage string as standard
53 2
            $name = '('.self::decode($name, $document).')';
54 2
            $element = false;
0 ignored issues
show
The assignment to $element is dead and can be removed.
Loading history...
55
56 2
            if (!($element = ElementDate::parse($name, $document))) {
57 2
                $element = ElementString::parse($name, $document);
58
            }
59
60 2
            return $element;
61
        }
62
63 9
        return false;
64
    }
65
66
    /**
67
     * @param string   $value
68
     * @param Document $document
69
     */
70 9
    public static function decode($value, Document $document = null)
0 ignored issues
show
The parameter $document is not used and could be removed. ( Ignorable by Annotation )

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

70
    public static function decode($value, /** @scrutinizer ignore-unused */ Document $document = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72 9
        $text = '';
73 9
        $length = \strlen($value);
74
75 9
        if ('00' === substr($value, 0, 2)) {
76 4
            for ($i = 0; $i < $length; $i += 4) {
77 4
                $hex = substr($value, $i, 4);
78 4
                $text .= '&#'.str_pad(hexdec($hex), 4, '0', STR_PAD_LEFT).';';
79
            }
80
        } else {
81 8
            for ($i = 0; $i < $length; $i += 2) {
82 8
                $hex = substr($value, $i, 2);
83 8
                $text .= \chr(hexdec($hex));
0 ignored issues
show
It seems like hexdec($hex) can also be of type double; however, parameter $ascii of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

83
                $text .= \chr(/** @scrutinizer ignore-type */ hexdec($hex));
Loading history...
84
            }
85
        }
86
87 9
        $text = html_entity_decode($text, ENT_NOQUOTES, 'UTF-8');
88
89 9
        return $text;
90
    }
91
}
92