Passed
Pull Request — master (#716)
by
unknown
03:14
created

ElementString::parse()   D

Complexity

Conditions 18
Paths 17

Size

Total Lines 81
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 57
CRAP Score 18

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 61
c 2
b 0
f 0
nc 17
nop 3
dl 0
loc 81
ccs 57
cts 57
cp 1
crap 18
rs 4.8666

How to fix   Long Method    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
 *
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\Element;
34
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Element;
37
use Smalot\PdfParser\Font;
38
39
/**
40
 * Class ElementString
41
 */
42
class ElementString extends Element
43
{
44 78
    public function __construct($value)
45
    {
46 78
        parent::__construct($value, null);
47
    }
48
49 2
    public function equals($value): bool
50
    {
51 2
        return $value == $this->value;
52
    }
53
54
    /**
55
     * @return bool|ElementString
56
     */
57 71
    public static function parse(string $content, ?Document $document = null, int &$offset = 0)
0 ignored issues
show
Unused Code introduced by
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

57
    public static function parse(string $content, /** @scrutinizer ignore-unused */ ?Document $document = null, int &$offset = 0)

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...
58
    {
59 71
        if (preg_match('/^\s*\((?P<name>.*)/s', $content, $match)) {
60 68
            $name = $match['name'];
61
62 68
            $delimiterCount = 0;
63 68
            $position = 0;
64 68
            $processedName = '';
65
            do {
66 68
                $char = substr($name, 0, 1);
67 68
                $name = substr($name, 1);
68 68
                ++$position;
69
                switch ($char) {
70
                    // matched delimiters should be treated as part of string
71 68
                    case '(':
72 4
                        $processedName .= $char;
73 4
                        ++$delimiterCount;
74 4
                        break;
75 68
                    case ')':
76 68
                        if (0 === $delimiterCount) {
77 68
                            $name = substr($name, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
78 68
                            break 2;
79
                        }
80 4
                        $processedName .= $char;
81 4
                        --$delimiterCount;
82 4
                        break;
83
                        // escaped chars
84 68
                    case '\\':
85 33
                        $nextChar = substr($name, 0, 1);
86
                        switch ($nextChar) {
87
                            // end-of-line markers (CR, LF, CRLF) should be ignored
88 33
                            case "\r":
89 31
                            case "\n":
90 2
                                preg_match('/^\\r?\\n?/', $name, $matches);
91 2
                                $name = substr($name, \strlen($matches[0]));
92 2
                                $position += \strlen($matches[0]);
93 2
                                break;
94
                                // process LF, CR, HT, BS, FF
95 31
                            case 'n':
96 31
                            case 't':
97 31
                            case 'r':
98 30
                            case 'b':
99 30
                            case 'f':
100 1
                                $processedName .= stripcslashes('\\'.$nextChar);
101 1
                                $name = substr($name, 1);
102 1
                                ++$position;
103 1
                                break;
104
                                // decode escaped parentheses and backslash
105 30
                            case '(':
106 30
                            case ')':
107 20
                            case '\\':
108 19
                            case ' ': // TODO: this should probably be removed - kept for compatibility
109 14
                                $processedName .= $nextChar;
110 14
                                $name = substr($name, 1);
111 14
                                ++$position;
112 14
                                break;
113
                                // TODO: process octal encoding (but it is also processed later)
114
                                // keep backslash in other cases
115
                            default:
116 19
                                $processedName .= $char;
117
                        }
118 33
                        break;
119
                    default:
120 68
                        $processedName .= $char;
121
                }
122 68
            } while (\strlen($name));
123
124 68
            $offset += strpos($content, '(') + 1 + $position;
125
126 68
            $name = $processedName;
127
128
            // Decode string.
129 68
            $name = Font::decodeOctal($name);
130 68
            $name = Font::decodeEntities($name);
131 68
            $name = Font::decodeHexadecimal($name, false);
132 68
            $name = Font::decodeUnicode($name);
133
134 68
            return new self($name);
135
        }
136
137 21
        return false;
138
    }
139
}
140