Test Failed
Push — master ( e387ef...0464de )
by Lexey
03:14
created

Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 51
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 31 5
A isCommentOrEmpty() 0 4 2
1
<?php
2
3
namespace LF\EnvDiff\Env;
4
5
use InvalidArgumentException;
6
7
class Parser
8
{
9
    /**
10
     * @param string $path
11
     *
12
     * @return array
13
     *
14
     * @throws InvalidArgumentException
15
     */
16 13
    public function parse($path)
17
    {
18 13
        if (!is_file($path)) {
19 1
            throw new InvalidArgumentException(sprintf('The file "%s" does not exist', $path));
20
        }
21
22 12
        $file = fopen($path, 'rb');
23
24 12
        $number = 0;
25 12
        $env    = [];
26 12
        while (false === feof($file)) {
27
            $line = trim(fgets($file));
28
            $number ++;
29 12
30 10
            if ($this->isCommentOrEmpty($line)) {
31
                continue;
32 12
            }
33
            if (false === strpos($line, '=')) {
34
                throw new InvalidArgumentException(
35
                    sprintf('Parse error at %d line: `%s` is not valid env value', $number, $line)
36 12
                );
37 12
            }
38 12
39
            list($name, $value) = explode('=', $line, 2);
40 12
            $env[trim($name)] = trim($value);
41
        }
42
43
        fclose($file);
44
45
        return $env;
46
    }
47
48
    /**
49
     * @param string $line
50
     *
51
     * @return bool
52
     */
53
    private function isCommentOrEmpty($line)
54
    {
55
        return (mb_strlen($line) === 0 || $line[0] === '#');
56
    }
57
}
58