Passed
Push — master ( 7fc0e6...4786a0 )
by Siad
06:48
created

classes/phing/parser/ExpatParser.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * This class is a wrapper for the PHP's internal expat parser.
22
 *
23
 * It takes an XML file represented by a abstract path name, and starts
24
 * parsing the file and calling the different "trap" methods inherited from
25
 * the AbstractParser class.
26
 *
27
 * Those methods then invoke the represenatative methods in the registered
28
 * handler classes.
29
 *
30
 * @author    Andreas Aderhold <[email protected]>
31
 * @copyright 2001,2002 THYRELL. All rights reserved
32
 * @package   phing.parser
33
 */
34
class ExpatParser extends AbstractSAXParser
35
{
36
37
    /**
38
     * @var resource
39
     */
40
    private $parser;
41
42
    /**
43
     * @var Reader
44
     */
45
    private $reader;
46
47
    /**
48
     * @var PhingFile
49
     */
50
    private $file;
51
52
    private $buffer = 4096;
53
54
    /**
55
     * @var Location Current cursor pos in XML file.
56
     */
57
    private $location;
58
59
    /**
60
     * Constructs a new ExpatParser object.
61
     *
62
     * The constructor accepts a PhingFile object that represents the filename
63
     * for the file to be parsed. It sets up php's internal expat parser
64
     * and options.
65
     *
66
     * @param  Reader $reader The Reader Object that is to be read from.
67
     * @param  string $filename Filename to read.
68
     * @throws Exception if the given argument is not a PhingFile object
69
     */
70 755
    public function __construct(Reader $reader, $filename = null)
71
    {
72 755
        $this->reader = $reader;
73 755
        if ($filename !== null) {
74
            $this->file = new PhingFile($filename);
75
        }
76 755
        $this->parser = xml_parser_create();
77 755
        $this->buffer = 4096;
78 755
        $this->location = new Location();
79 755
        xml_set_object($this->parser, $this);
80 755
        xml_set_element_handler($this->parser, [$this, "startElement"], [$this, "endElement"]);
81 755
        xml_set_character_data_handler($this->parser, [$this, "characters"]);
82 755
    }
83
84
    /**
85
     * Override PHP's parser default settings, created in the constructor.
86
     *
87
     * @param    $opt
88
     * @param    $val
89
     * @internal param the $string option to set
90
     * @return   boolean true if the option could be set, otherwise false
91
     */
92 755
    public function parserSetOption($opt, $val)
93
    {
94 755
        return xml_parser_set_option($this->parser, $opt, $val);
95
    }
96
97
    /**
98
     * Returns the location object of the current parsed element. It describes
99
     * the location of the element within the XML file (line, char)
100
     *
101
     * @return Location the location of the current parser
102
     */
103 750
    public function getLocation()
104
    {
105 750
        if ($this->file !== null) {
106
            $path = $this->file->getAbsolutePath();
107
        } else {
108 750
            $path = $this->reader->getResource();
109
        }
110 750
        $this->location = new Location(
111 750
            $path,
112 750
            xml_get_current_line_number($this->parser),
113 750
            xml_get_current_column_number(
114 750
                $this->parser
115
            )
116
        );
117
118 750
        return $this->location;
119
    }
120
121
    /**
122
     * Starts the parsing process.
123
     *
124
     * @return int                 1 if the parsing succeeded
125
     * @throws ExpatParseException if something gone wrong during parsing
126
     * @throws IOException         if XML file can not be accessed
127
     */
128 755
    public function parse()
129
    {
130 755
        while (($data = $this->reader->read()) !== -1) {
131 755
            if (!xml_parse($this->parser, $data, $this->reader->eof())) {
0 ignored issues
show
The method eof() does not exist on Reader. It seems like you code against a sub-type of said class. However, the method does not exist in FilterReader. 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

131
            if (!xml_parse($this->parser, $data, $this->reader->/** @scrutinizer ignore-call */ eof())) {
Loading history...
132
                $error = xml_error_string(xml_get_error_code($this->parser));
133
                $e = new ExpatParseException($error, $this->getLocation());
134
                xml_parser_free($this->parser);
135
                throw $e;
136
            }
137
        }
138 755
        xml_parser_free($this->parser);
139
140 755
        return 1;
141
    }
142
}
143