Passed
Push — main ( 8b647c...f5e4cf )
by Michiel
06:29
created

IniFileParser::inVal()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Io;
22
23
/**
24
 * Implements an IniFileParser. The logic is coming from th Properties.php, but I don't know who's the author.
25
 *
26
 * FIXME
27
 *  - Add support for arrays (separated by ',')
28
 *
29
 * @author  Mike Lohmann <[email protected]>
30
 */
31
class IniFileParser implements FileParserInterface
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36 893
    public function parseFile(File $file)
37
    {
38 893
        if (($lines = @file($file, FILE_IGNORE_NEW_LINES)) === false) {
39 1
            throw new IOException("Unable to parse contents of {$file}");
40
        }
41
42
        // concatenate lines ending with backslash
43 892
        $linesCount = count($lines);
44 892
        for ($i = 0; $i < $linesCount; ++$i) {
45 892
            if ('\\' === substr((string) $lines[$i], -1, 1)) {
46 2
                $lines[$i + 1] = substr((string) $lines[$i], 0, -1) . ltrim($lines[$i + 1]);
47 2
                $lines[$i] = '';
48
            }
49
        }
50
51 892
        $properties = [];
52 892
        foreach ($lines as $line) {
53
            // strip comments and leading/trailing spaces
54 892
            $line = trim(preg_replace('/\\s+[;#]\\s.+$/', '', $line));
55
56 892
            if (empty($line) || ';' == $line[0] || '#' == $line[0]) {
57 885
                continue;
58
            }
59
60 889
            $pos = strpos((string) $line, '=');
61 889
            if (false === $pos) {
62 1
                continue;
63
            }
64 889
            $property = trim(substr((string) $line, 0, $pos));
65 889
            $value = trim(substr((string) $line, $pos + 1));
66 889
            $properties[$property] = $value;
67
        } // for each line
68
69 892
        return $properties;
70
    }
71
}
72