Passed
Push — master ( 30bcbc...eba4fe )
by Siad
06:53
created

ReplaceRegexp   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 96
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 24 4
A chain() 0 7 1
A setRegexps() 0 3 1
A createRegexp() 0 5 1
A getRegexps() 0 3 1
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
 * Performs a regexp find/replace on stream.
22
 * <p>
23
 * Example:<br>
24
 * <pre>
25
 * <replaceregexp>
26
 *    <regexp pattern="\r\n" replace="\n"/>
27
 *    <regexp pattern="(\w+)\.xml" replace="\1.php" ignoreCase="true"/>
28
 * </replaceregexp>
29
 * </pre>
30
 *
31
 * @author  Hans Lellelid <[email protected]>
32
 * @package phing.filters
33
 */
34
class ReplaceRegexp extends BaseFilterReader implements ChainableReader
35
{
36
37
    /**
38
     * @var array RegularExpression[]
39
     */
40
    private $regexps = [];
41
42
    /**
43
     * Creator method handles nested <regexp> tags.
44
     *
45
     * @return RegularExpression
46
     */
47 2
    public function createRegexp()
48
    {
49 2
        $num = array_push($this->regexps, new RegularExpression());
50
51 2
        return $this->regexps[$num - 1];
52
    }
53
54
    /**
55
     * Sets the current regexps.
56
     * (Used when, e.g., cloning/chaining the method.)
57
     *
58
     * @param array RegularExpression[]
59
     */
60 2
    public function setRegexps($regexps)
61
    {
62 2
        $this->regexps = $regexps;
63 2
    }
64
65
    /**
66
     * Gets the current regexps.
67
     * (Used when, e.g., cloning/chaining the method.)
68
     *
69
     * @return array RegularExpression[]
70
     */
71 2
    public function getRegexps()
72
    {
73 2
        return $this->regexps;
74
    }
75
76
    /**
77
     * Returns the filtered stream.
78
     * The original stream is first read in fully, and the regex replace is performed.
79
     *
80
     * @param int $len Required $len for Reader compliance.
81
     *
82
     * @return mixed The filtered stream, or -1 if the end of the resulting stream has been reached.
83
     *
84
     * @throws IOException if the underlying stream throws an IOException
85
     * during reading
86
     */
87 2
    public function read($len = null)
88
    {
89 2
        $buffer = $this->in->read($len);
90
91 2
        if ($buffer === -1) {
92 1
            return -1;
93
        }
94
95
        // perform regex replace here ...
96 2
        foreach ($this->regexps as $exptype) {
97 2
            $regexp = $exptype->getRegexp($this->project);
98
            try {
99 2
                $buffer = $regexp->replace($buffer);
100 2
                $this->log(
101 2
                    "Performing regexp replace: /" . $regexp->getPattern() . "/" . $regexp->getReplace() . "/g" . $regexp->getModifiers(),
102 2
                    Project::MSG_VERBOSE
103
                );
104
            } catch (Exception $e) {
105
                // perhaps mismatch in params (e.g. no replace or pattern specified)
106
                $this->log("Error performing regexp replace: " . $e->getMessage(), Project::MSG_WARN);
107
            }
108
        }
109
110 2
        return $buffer;
111
    }
112
113
    /**
114
     * Creates a new ReplaceRegExp filter using the passed in
115
     * Reader for instantiation.
116
     *
117
     * @param Reader $reader A Reader object providing the underlying stream.
118
     *                       Must not be <code>null</code>.
119
     *
120
     * @return ReplaceRegexp A new filter based on this configuration, but filtering
121
     *                       the specified reader
122
     */
123 2
    public function chain(Reader $reader): Reader
124
    {
125 2
        $newFilter = new ReplaceRegexp($reader);
126 2
        $newFilter->setProject($this->getProject());
127 2
        $newFilter->setRegexps($this->getRegexps());
128
129 2
        return $newFilter;
130
    }
131
}
132