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 is a Php comment and string stripper reader that filters
22
 * those lexical tokens out for purposes of simple Php parsing.
23
 * (if you have more complex Php parsing needs, use a real lexer).
24
 * Since this class heavily relies on the single char read function,
25
 * you are recommended to make it work on top of a buffered reader.
26
 *
27
 * @author  <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
28
 * @author  hans lellelid, hans@velum.net
29
 * @see     FilterReader
30
 * @package phing.filters
31
 */
32
class StripPhpComments extends BaseFilterReader implements ChainableReader
33
{
34
    /**
35
     * The read-ahead character, used for effectively pushing a single
36
     * character back. -1 indicates that no character is in the buffer.
37
     */
38
    private $readAheadCh = -1;
39

40
    /**
41
     * Whether or not the parser is currently in the middle of a string
42
     * literal.
43
     *
44
     * @var boolean
45
     */
46
    private $inString = false;
47

48
    /**
49
     * Returns the  stream without Php comments.
50
     *
51
     * @param  int $len
52
     * @return string the resulting stream, or -1
53
     *             if the end of the resulting stream has been reached
54
     */
55 1
    public function read($len = null)
56
    {
57 1
        $buffer = $this->in->read($len);
58 1
        if ($buffer === -1) {
59 1
            return -1;
60
        }
61 1
        $newStr = '';
62 1
        $tokens = token_get_all($buffer);
63

64 1
        foreach ($tokens as $token) {
65 1
            if (is_array($token)) {
66 1
                [$id, $text] = $token;
67

68 1
                switch ($id) {
69
                    case T_COMMENT:
70
                    case T_DOC_COMMENT:
71
                        // no action on comments
72 1
                        continue 2;
73

74
                    default:
75 1
                        $newStr .= $text;
76 1
                        continue 2;
77
                }
78
            }
79 1
            $newStr .= $token;
80
        }
81

82 1
        return $newStr;
83
    }
84

85
    /**
86
     * Creates a new StripPhpComments using the passed in
87
     * Reader for instantiation.
88
     *
89
     * @param A|Reader $reader
90
     * @return $this a new filter based on this configuration, but filtering
91
     *           the specified reader
92
     * @internal param A $reader Reader object providing the underlying stream.
93
     *               Must not be <code>null</code>.
94
     */
95 1
    public function chain(Reader $reader): Reader
96
    {
97 1
        $newFilter = new StripPhpComments($reader);
98 1
        $newFilter->setProject($this->getProject());
99

100 1
        return $newFilter;
101
    }
102
}

Read our documentation on viewing source code .

Loading