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
 * Converts tabs to spaces.
22
 *
23
 * Example:
24
 *
25
 * <pre><tabtospaces tablength="8"></pre>
26
 *
27
 * Or:
28
 *
29
 * <pre><filterreader classname="phing.filters.TabsToSpaces">
30
 *   <param name="tablength" value="8">
31
 * </filterreader></pre>
32
 *
33
 * @author  Yannick Lecaillez <yl@seasonfive.com>
34
 * @author  Hans Lellelid <hans@xmpl.org>
35
 * @see     BaseParamFilterReader
36
 * @package phing.filters
37
 */
38
class TabToSpaces extends BaseParamFilterReader implements ChainableReader
39
{
40

41
    /**
42
     * The default tab length.
43
     *
44
     * @var int
45
     */
46
    public const DEFAULT_TAB_LENGTH = 8;
47

48
    /**
49
     * Parameter name for the length of a tab.
50
     *
51
     * @var string
52
     */
53
    public const TAB_LENGTH_KEY = "tablength";
54

55
    /**
56
     * Tab length in this filter.
57
     *
58
     * @var int
59
     */
60
    private $tabLength = 8; //self::DEFAULT_TAB_LENGTH;
61

62
    /**
63
     * Returns stream after converting tabs to the specified number of spaces.
64
     *
65
     * @param  int $len
66
     * @return int the resulting stream, or -1
67
     *             if the end of the resulting stream has been reached
68
     *
69
     * @throws IOException if the underlying stream throws an IOException
70
     *            during reading
71
     */
72 0
    public function read($len = null)
73
    {
74 0
        if (!$this->getInitialized()) {
75 0
            $this->initialize();
76 0
            $this->setInitialized(true);
77
        }
78

79 0
        $buffer = $this->in->read($len);
80

81 0
        if ($buffer === -1) {
82 0
            return -1;
83
        }
84

85 0
        $buffer = str_replace("\t", str_repeat(' ', $this->tabLength), $buffer);
86

87 0
        return $buffer;
88
    }
89

90
    /**
91
     * Sets the tab length.
92
     *
93
     * @param int $tabLength The number of spaces to be used when converting a tab.
94
     */
95 0
    public function setTablength($tabLength)
96
    {
97 0
        $this->tabLength = (int) $tabLength;
98
    }
99

100
    /**
101
     * Returns the tab length.
102
     *
103
     * @return int The number of spaces used when converting a tab
104
     */
105 0
    public function getTablength()
106
    {
107 0
        return $this->tabLength;
108
    }
109

110
    /**
111
     * Creates a new TabsToSpaces using the passed in
112
     * Reader for instantiation.
113
     *
114
     * @param Reader $reader A Reader object providing the underlying stream.
115
     *                       Must not be <code>null</code>.
116
     *
117
     * @return TabToSpaces A new filter based on this configuration, but filtering
118
     *                the specified reader
119
     */
120 0
    public function chain(Reader $reader): Reader
121
    {
122 0
        $newFilter = new TabToSpaces($reader);
123 0
        $newFilter->setTablength($this->getTablength());
124 0
        $newFilter->setInitialized(true);
125 0
        $newFilter->setProject($this->getProject());
126

127 0
        return $newFilter;
128
    }
129

130
    /**
131
     * Parses the parameters to set the tab length.
132
     */
133 0
    private function initialize()
134
    {
135 0
        $params = $this->getParameters();
136 0
        if ($params !== null) {
137 0
            for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) {
138 0
                if (self::TAB_LENGTH_KEY === $params[$i]->getName()) {
139 0
                    $this->tabLength = $params[$i]->getValue();
140 0
                    break;
141
                }
142
            }
143
        }
144
    }
145
}

Read our documentation on viewing source code .

Loading