Completed
Push — master ( d52b96...e2f758 )
by Michiel
11:35
created

classes/phing/filters/util/IniFileTokenReader.php (2 issues)

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
 * Class that allows reading tokens from INI files.
22
 *
23
 * @author  Manuel Holtgewe
24
 * @package phing.filters.util
25
 */
26
class IniFileTokenReader extends TokenReader
27
{
28
29
    /**
30
     * Holds the path to the INI file that is to be read.
31
     *
32
     * @var object  Reference to a PhingFile Object representing
33
     *              the path to the INI file.
34
     */
35
    private $file = null;
36
37
    /**
38
     * @var string  Sets the section to load from the INI file.
39
     *              if omitted, all sections are loaded.
40
     */
41
    private $section = null;
42
43
    /**
44
     * @var array
45
     */
46
    private $tokens = null;
47
48
    /**
49
     * Reads the next token from the INI file
50
     *
51
     * @throws BuildException
52
     * @return Token
53
     */
54 1
    public function readToken()
55
    {
56 1
        if ($this->file === null) {
57
            throw new BuildException("No File set for IniFileTokenReader");
58
        }
59
60 1
        if ($this->tokens === null) {
61 1
            $this->processFile();
62
        }
63
64 1
        if (count($this->tokens) > 0) {
65 1
            return array_pop($this->tokens);
66
        } else {
67 1
            return null;
68
        }
69
    }
70
71
    /**
72
     * Parse & process the ini file
73
     */
74 1
    protected function processFile()
75
    {
76 1
        $arr = parse_ini_file($this->file->getAbsolutePath(), true);
77
78 1
        if ($this->section !== null) {
79
            if (isset($arr[$this->section])) {
80
                $this->processSection($arr[$this->section]);
81
            }
82
83
            return;
84
        }
85
86 1
        $values = array_values($arr);
0 ignored issues
show
It seems like $arr can also be of type false; however, parameter $input of array_values() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $values = array_values(/** @scrutinizer ignore-type */ $arr);
Loading history...
87
88 1
        if (!is_array($values[0])) {
89 1
            $this->processSection($arr);
0 ignored issues
show
It seems like $arr can also be of type false; however, parameter $section of IniFileTokenReader::processSection() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            $this->processSection(/** @scrutinizer ignore-type */ $arr);
Loading history...
90
91 1
            return;
92
        }
93
94
        foreach ($values as $subArr) {
95
            $this->processSection($subArr);
96
        }
97
    }
98
99
    /**
100
     * Process an individual section
101
     *
102
     * @param array $section
103
     */
104 1
    protected function processSection(array $section)
105
    {
106 1
        foreach ($section as $key => $value) {
107 1
            $tok = new Token();
108 1
            $tok->setKey($key);
109 1
            $tok->setValue($value);
110 1
            $this->tokens[] = $tok;
111
        }
112 1
    }
113
114
    /**
115
     * @param string|PhingFile $file
116
     * @throws BuildException
117
     */
118 1
    public function setFile($file)
119
    {
120 1
        if (is_string($file)) {
121 1
            $this->file = new PhingFile($file);
122
123 1
            return;
124
        }
125
126
        if (is_object($file) && $file instanceof PhingFile) {
127
            $this->file = $file;
128
129
            return;
130
        }
131
132
        throw new BuildException("Unsupported value " . (string) $file);
133
    }
134
135
    /**
136
     * @param $str
137
     */
138
    public function setSection($str)
139
    {
140
        $this->section = (string) $str;
141
    }
142
}
143