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

StripLineComments::_initialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 20
rs 10
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
 * This filter strips line comments.
22
 *
23
 * Example:
24
 *
25
 * <pre><striplinecomments>
26
 *   <comment value="#"/>
27
 *   <comment value="--"/>
28
 *   <comment value="REM "/>
29
 *   <comment value="rem "/>
30
 *   <comment value="//"/>
31
 * </striplinecomments></pre>
32
 *
33
 * Or:
34
 *
35
 * <pre><filterreader classname="phing.filters.StripLineComments">
36
 *   <param type="comment" value="#"/>
37
 *   <param type="comment" value="--"/>
38
 *   <param type="comment" value="REM "/>
39
 *   <param type="comment" value="rem "/>
40
 *   <param type="comment" value="//"/>
41
 * </filterreader></pre>
42
 *
43
 * @author  <a href="mailto:[email protected]">Yannick Lecaillez</a>
44
 * @author  hans lellelid, [email protected]
45
 * @see     BaseParamFilterReader
46
 * @package phing.filters
47
 */
48
class StripLineComments extends BaseParamFilterReader implements ChainableReader
49
{
50
51
    /**
52
     * Parameter name for the comment prefix.
53
     */
54
    const COMMENTS_KEY = "comment";
55
56
    /**
57
     * Array that holds the comment prefixes.
58
     */
59
    private $comments = [];
60
61
    /**
62
     * Returns stream only including
63
     * lines from the original stream which don't start with any of the
64
     * specified comment prefixes.
65
     *
66
     * @param  int $len
67
     * @return mixed the resulting stream, or -1
68
     *               if the end of the resulting stream has been reached.
69
     */
70
    public function read($len = null)
71
    {
72
        if (!$this->getInitialized()) {
73
            $this->initialize();
74
            $this->setInitialized(true);
75
        }
76
77
        $buffer = $this->in->read($len);
78
79
        if ($buffer === -1) {
80
            return -1;
81
        }
82
83
        $lines = explode("\n", $buffer);
84
        $filtered = [];
85
86
        $commentsSize = count($this->comments);
87
88
        foreach ($lines as $line) {
89
            for ($i = 0; $i < $commentsSize; $i++) {
90
                $comment = $this->comments[$i]->getValue();
91
                if (StringHelper::startsWith($comment, ltrim($line))) {
92
                    $line = null;
93
                    break;
94
                }
95
            }
96
            if ($line !== null) {
97
                $filtered[] = $line;
98
            }
99
        }
100
101
        $filtered_buffer = implode("\n", $filtered);
102
103
        return $filtered_buffer;
104
    }
105
106
    /*
107
     * Adds a <code>comment</code> element to the list of prefixes.
108
     *
109
     * @return comment The <code>comment</code> element added to the
110
     *                 list of comment prefixes to strip.
111
    */
112
    public function createComment()
113
    {
114
        $num = array_push($this->comments, new Comment());
115
116
        return $this->comments[$num - 1];
117
    }
118
119
    /*
120
     * Sets the list of comment prefixes to strip.
121
     *
122
     * @param comments A list of strings, each of which is a prefix
123
     *                 for a comment line. Must not be <code>null</code>.
124
    */
125
    /**
126
     * @param $lineBreaks
127
     * @throws Exception
128
     */
129
    public function setComments($lineBreaks)
130
    {
131
        if (!is_array($lineBreaks)) {
132
            throw new Exception("Expected 'array', got something else");
133
        }
134
        $this->comments = $lineBreaks;
135
    }
136
137
    /*
138
     * Returns the list of comment prefixes to strip.
139
     *
140
     * @return array The list of comment prefixes to strip.
141
    */
142
    /**
143
     * @return array
144
     */
145
    public function getComments()
146
    {
147
        return $this->comments;
148
    }
149
150
    /*
151
     * Creates a new StripLineComments using the passed in
152
     * Reader for instantiation.
153
     *
154
     * @param reader A Reader object providing the underlying stream.
155
     *               Must not be <code>null</code>.
156
     *
157
     * @return a new filter based on this configuration, but filtering
158
     *           the specified reader
159
     */
160
    /**
161
     * @param Reader $reader
162
     * @return StripLineComments
163
     * @throws Exception
164
     */
165
    public function chain(Reader $reader): Reader
166
    {
167
        $newFilter = new StripLineComments($reader);
168
        $newFilter->setComments($this->getComments());
169
        $newFilter->setInitialized(true);
170
        $newFilter->setProject($this->getProject());
171
172
        return $newFilter;
173
    }
174
175
    /*
176
     * Parses the parameters to set the comment prefixes.
177
    */
178
    private function initialize()
179
    {
180
        $params = $this->getParameters();
181
        if ($params !== null) {
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
182
            for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) {
183
                if (self::COMMENTS_KEY === $params[$i]->getType()) {
184
                    $comment = new Comment();
185
                    $comment->setValue($params[$i]->getValue());
186
                    $this->comments[] = $comment;
187
                }
188
            }
189
        }
190
    }
191
}
192