Completed
Push — master ( 7082f0...55a2b2 )
by Siad
15:25
created

StripLineBreaks::initialize()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 6
nop 0
dl 0
loc 15
ccs 0
cts 10
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
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
 * Filter to flatten the stream to a single line.
22
 *
23
 * Example:
24
 *
25
 * <pre><striplinebreaks/></pre>
26
 *
27
 * Or:
28
 *
29
 * <pre><filterreader classname="phing.filters.StripLineBreaks"/></pre>
30
 *
31
 * @author  <a href="mailto:[email protected]">Yannick Lecaillez</a>
32
 * @author  hans lellelid, [email protected]
33
 * @see     BaseParamFilterReader
34
 * @package phing.filters
35
 */
36
class StripLineBreaks extends BaseParamFilterReader implements ChainableReader
37
{
38
39
    /**
40
     * Default line-breaking characters.
41
     *
42
     * @var string
43
     */
44
    const DEFAULT_LINE_BREAKS = "\r\n";
45
46
    /**
47
     * Parameter name for the line-breaking characters parameter.
48
     *
49
     * @var string
50
     */
51
    const LINES_BREAKS_KEY = "linebreaks";
52
53
    /**
54
     * The characters that are recognized as line breaks.
55
     *
56
     * @var string
57
     */
58
    private $lineBreaks = "\r\n"; // self::DEFAULT_LINE_BREAKS;
59
60
    /**
61
     * Returns the filtered stream, only including
62
     * characters not in the set of line-breaking characters.
63
     *
64
     * @param  int $len
65
     * @return mixed the resulting stream, or -1
66
     *               if the end of the resulting stream has been reached.
67
     *
68
     * @throws IOException if the underlying stream throws an IOException
69
     *            during reading
70
     */
71 4
    public function read($len = null)
72
    {
73 4
        if (!$this->getInitialized()) {
74
            $this->initialize();
75
            $this->setInitialized(true);
76
        }
77
78 4
        $buffer = $this->in->read($len);
79 4
        if ($buffer === -1) {
80 4
            return -1;
81
        }
82
83 4
        $buffer = preg_replace("/[" . $this->lineBreaks . "]/", '', $buffer);
84
85 4
        return $buffer;
86
    }
87
88
    /**
89
     * Sets the line-breaking characters.
90
     *
91
     * @param string $lineBreaks A String containing all the characters to be
92
     *                           considered as line-breaking.
93
     */
94 4
    public function setLineBreaks($lineBreaks)
95
    {
96 4
        $this->lineBreaks = (string) $lineBreaks;
97 4
    }
98
99
    /**
100
     * Gets the line-breaking characters.
101
     *
102
     * @return string A String containing all the characters that are considered as line-breaking.
103
     */
104 4
    public function getLineBreaks()
105
    {
106 4
        return $this->lineBreaks;
107
    }
108
109
    /**
110
     * Creates a new StripLineBreaks using the passed in
111
     * Reader for instantiation.
112
     *
113
     * @param Reader $reader
114
     * @return StripLineBreaks A new filter based on this configuration, but filtering
115
     *                the specified reader
116
     * @internal param A $object Reader object providing the underlying stream.
117
     *               Must not be <code>null</code>.
118
     */
119 4
    public function chain(Reader $reader): Reader
120
    {
121 4
        $newFilter = new StripLineBreaks($reader);
122 4
        $newFilter->setLineBreaks($this->getLineBreaks());
123 4
        $newFilter->setInitialized(true);
124 4
        $newFilter->setProject($this->getProject());
125
126 4
        return $newFilter;
127
    }
128
129
    /**
130
     * Parses the parameters to set the line-breaking characters.
131
     */
132
    private function initialize()
133
    {
134
        $userDefinedLineBreaks = null;
135
        $params = $this->getParameters();
136
        if ($params !== null) {
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
137
            for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) {
138
                if (self::LINES_BREAKS_KEY === $params[$i]->getName()) {
139
                    $userDefinedLineBreaks = $params[$i]->getValue();
140
                    break;
141
                }
142
            }
143
        }
144
145
        if ($userDefinedLineBreaks !== null) {
146
            $this->lineBreaks = $userDefinedLineBreaks;
147
        }
148
    }
149
}
150