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

TabToSpaces::initialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 0
dl 0
loc 8
ccs 0
cts 7
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
 * 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 <[email protected]>
34
 * @author  Hans Lellelid <[email protected]>
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
    const DEFAULT_TAB_LENGTH = 8;
47
48
    /**
49
     * Parameter name for the length of a tab.
50
     *
51
     * @var string
52
     */
53
    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
    public function read($len = null)
73
    {
74
        if (!$this->getInitialized()) {
75
            $this->initialize();
76
            $this->setInitialized(true);
77
        }
78
79
        $buffer = $this->in->read($len);
80
81
        if ($buffer === -1) {
82
            return -1;
83
        }
84
85
        $buffer = str_replace("\t", str_repeat(' ', $this->tabLength), $buffer);
86
87
        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
    public function setTablength($tabLength)
96
    {
97
        $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
    public function getTablength()
106
    {
107
        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
    public function chain(Reader $reader): Reader
121
    {
122
        $newFilter = new TabToSpaces($reader);
123
        $newFilter->setTablength($this->getTablength());
124
        $newFilter->setInitialized(true);
125
        $newFilter->setProject($this->getProject());
126
127
        return $newFilter;
128
    }
129
130
    /**
131
     * Parses the parameters to set the tab length.
132
     */
133
    private function initialize()
134
    {
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::TAB_LENGTH_KEY === $params[$i]->getName()) {
139
                    $this->tabLength = $params[$i]->getValue();
140
                    break;
141
                }
142
            }
143
        }
144
    }
145
}
146