Passed
Push — main ( d5ff59...b03a29 )
by Michiel
09:01
created

ExpatParser::getLocation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0123
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Parser;
22
23
use Exception;
24
use Phing\Io\IOException;
25
use Phing\Io\Reader;
26
use SplFileObject;
27
28
/**
29
 * This class is a wrapper for the PHP's internal expat parser.
30
 *
31
 * It takes an XML file represented by a abstract path name, and starts
32
 * parsing the file and calling the different "trap" methods inherited from
33
 * the AbstractParser class.
34
 *
35
 * Those methods then invoke the represenatative methods in the registered
36
 * handler classes.
37
 *
38
 * @author    Andreas Aderhold <[email protected]>
39
 * @copyright 2001,2002 THYRELL. All rights reserved
40
 */
41
class ExpatParser extends AbstractSAXParser
42
{
43
    /**
44
     * @var resource
45
     */
46
    private $parser;
47
48
    /**
49
     * @var Reader
50
     */
51
    private $reader;
52
53
    /**
54
     * @var SplFileObject
55
     */
56
    private $file;
57
58
    private $buffer = 4096;
0 ignored issues
show
introduced by
The private property $buffer is not used, and could be removed.
Loading history...
59
60
    /**
61
     * @var Location current cursor pos in XML file
62
     */
63
    private $location;
64
65
    /**
66
     * Constructs a new ExpatParser object.
67
     *
68
     * The constructor accepts a PhingFile object that represents the filename
69
     * for the file to be parsed. It sets up php's internal expat parser
70
     * and options.
71
     *
72
     * @param Reader $reader   the Reader Object that is to be read from
73
     * @param string $filename filename to read
74
     *
75
     * @throws Exception if the given argument is not a PhingFile object
76
     */
77 884
    public function __construct(Reader $reader, $filename = null)
78
    {
79 884
        $this->reader = $reader;
80 884
        if (null !== $filename) {
81
            $this->file = new SplFileObject($filename);
82
        }
83 884
        $this->parser = xml_parser_create();
0 ignored issues
show
Documentation Bug introduced by
It seems like xml_parser_create() can also be of type XmlParser. However, the property $parser is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84 884
        $this->location = new Location();
85 884
        xml_set_object($this->parser, $this);
86 884
        xml_set_element_handler($this->parser, [$this, 'startElement'], [$this, 'endElement']);
87 884
        xml_set_character_data_handler($this->parser, [$this, 'characters']);
88
    }
89
90
    /**
91
     * Override PHP's parser default settings, created in the constructor.
92
     *
93
     * @param $opt
94
     * @param $val
95
     *
96
     * @return bool true if the option could be set, otherwise false
97
     *
98
     * @internal param the $string option to set
99
     */
100 884
    public function parserSetOption($opt, $val)
101
    {
102 884
        return xml_parser_set_option($this->parser, $opt, $val);
103
    }
104
105
    /**
106
     * Returns the location object of the current parsed element. It describes
107
     * the location of the element within the XML file (line, char).
108
     *
109
     * @return Location the location of the current parser
110
     */
111 884
    public function getLocation()
112
    {
113 884
        if (null !== $this->file) {
114
            $path = false !== $this->file->getRealPath() ? $this->file->getRealPath() : null;
115
        } else {
116 884
            $path = $this->reader->getResource();
117
        }
118 884
        $this->location = new Location(
119
            $path,
120 884
            xml_get_current_line_number($this->parser),
121 884
            xml_get_current_column_number(
122 884
                $this->parser
123
            )
124
        );
125
126 884
        return $this->location;
127
    }
128
129
    /**
130
     * Starts the parsing process.
131
     *
132
     * @throws ExpatParseException if something gone wrong during parsing
133
     * @throws IOException         if XML file can not be accessed
134
     *
135
     * @return int 1 if the parsing succeeded
136
     */
137 884
    public function parse()
138
    {
139 884
        while (($data = $this->reader->read()) !== -1) {
140 884
            if (!xml_parse($this->parser, $data, $this->reader->eof())) {
0 ignored issues
show
Bug introduced by
The method eof() does not exist on Phing\Io\Reader. It seems like you code against a sub-type of said class. However, the method does not exist in Phing\Io\FilterReader or Phing\Io\StringReader. Are you sure you never get one of those? ( Ignorable by Annotation )

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

140
            if (!xml_parse($this->parser, $data, $this->reader->/** @scrutinizer ignore-call */ eof())) {
Loading history...
141
                $error = xml_error_string(xml_get_error_code($this->parser));
142
                $e = new ExpatParseException($error, $this->getLocation());
143
                xml_parser_free($this->parser);
144
145
                throw $e;
146
            }
147
        }
148 884
        xml_parser_free($this->parser);
149
150 884
        return 1;
151
    }
152
}
153