Completed
Push — master ( d52b96...e2f758 )
by Michiel
11:35
created

classes/phing/filters/XincludeFilter.php (2 issues)

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
 * Applies Xinclude parsing to incoming text.
22
 *
23
 * Uses PHP DOM XML support
24
 *
25
 * @author  Bill Karwin <[email protected]>
26
 * @see     FilterReader
27
 * @package phing.filters
28
 */
29
class XincludeFilter extends BaseParamFilterReader implements ChainableReader
30
{
31
    private $basedir = null;
32
33
    /**
34
     * @var bool
35
     */
36
    private $processed = false;
37
38
    /**
39
     * Whether to resolve entities.
40
     *
41
     * @var bool
42
     *
43
     * @since 2.4
44
     */
45
    private $resolveExternals = false;
46
47
    /**
48
     * Whether to resolve entities.
49
     *
50
     * @param $resolveExternals
51
     *
52
     * @since 2.4
53
     */
54
    public function setResolveExternals(bool $resolveExternals)
55
    {
56
        $this->resolveExternals = $resolveExternals;
57
    }
58
59
    /**
60
     * @return bool
61
     *
62
     * @since 2.4
63
     */
64
    public function getResolveExternals()
65
    {
66
        return $this->resolveExternals;
67
    }
68
69
    /**
70
     * @param PhingFile $dir
71
     */
72
    public function setBasedir(PhingFile $dir)
73
    {
74
        $this->basedir = $dir;
75
    }
76
77
    /**
78
     * @return null
79
     */
80
    public function getBasedir()
81
    {
82
        return $this->basedir;
83
    }
84
85
    /**
86
     * Reads stream, applies XSLT and returns resulting stream.
87
     *
88
     * @param  int $len
89
     * @throws BuildException
90
     * @return string         transformed buffer.
91
     */
92
    public function read($len = null)
93
    {
94
        if (!class_exists('DomDocument')) {
95
            throw new BuildException("Could not find the DomDocument class. Make sure PHP has been compiled/configured to support DOM XML.");
96
        }
97
98
        if ($this->processed === true) {
99
            return -1; // EOF
100
        }
101
102
        // Read XML
103
        $_xml = null;
104
        while (($data = $this->in->read($len)) !== -1) {
105
            $_xml .= $data;
106
        }
107
108
        if ($_xml === null) { // EOF?
109
            return -1;
110
        }
111
112
        if (empty($_xml)) {
113
            $this->log("XML file is empty!", Project::MSG_WARN);
114
115
            return '';
116
        }
117
118
        $this->log("Transforming XML " . $this->in->getResource() . " using Xinclude ", Project::MSG_VERBOSE);
119
120
        $out = '';
121
        try {
122
            $out = $this->process($_xml);
123
            $this->processed = true;
124
        } catch (IOException $e) {
125
            throw new BuildException($e);
126
        }
127
128
        return $out;
129
    }
130
131
    /**
132
     * Try to process the Xinclude transformation
133
     *
134
     * @param string  XML to process.
135
     *
136
     * @return string
137
     */
138
    protected function process($xml)
139
    {
140
        if ($this->basedir) {
141
            $cwd = getcwd();
142
            chdir($this->basedir);
143
        }
144
145
        // Create and setup document.
146
        $xmlDom = new DomDocument();
147
        $xmlDom->resolveExternals = $this->resolveExternals;
148
149
        $xmlDom->loadXML($xml);
150
151
        $xmlDom->xinclude();
152
153
        if ($this->basedir) {
154
            chdir($cwd);
155
        }
156
157
        return $xmlDom->saveXML();
158
    }
159
160
    /**
161
     * Creates a new XincludeFilter using the passed in
162
     * Reader for instantiation.
163
     *
164
     * @param Reader A Reader object providing the underlying stream.
165
     *               Must not be <code>null</code>.
166
     *
167
     * @return XincludeFilter A new filter based on this configuration, but filtering
168
     *                the specified reader
169
     */
170
    public function chain(Reader $reader): Reader
171
    {
172
        $newFilter = new self($reader);
173
        $newFilter->setProject($this->getProject());
174
        $newFilter->setBasedir($this->getBasedir());
0 ignored issues
show
Are you sure the usage of $this->getBasedir() targeting XincludeFilter::getBasedir() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
$this->getBasedir() of type null is incompatible with the type PhingFile expected by parameter $dir of XincludeFilter::setBasedir(). ( Ignorable by Annotation )

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

174
        $newFilter->setBasedir(/** @scrutinizer ignore-type */ $this->getBasedir());
Loading history...
175
176
        return $newFilter;
177
    }
178
}
179