Completed
Push — master ( 1cff36...5446cd )
by Rémi
02:27
created

Parser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 74
ccs 24
cts 26
cp 0.9231
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A setScannerMode() 0 4 1
A parse() 0 14 2
A sanitizeValues() 0 11 2
A getContent() 0 4 1
1
<?php
2
3
namespace Rfussien\Dotenv;
4
5
class Parser
6
{
7
    /**
8
     * parsed content of the dotenv file
9
     */
10
    private $content = [];
11
12
    /**
13
     * Which parsing style is used
14
     *
15
     * @see http://php.net/parse_ini_file
16
     */
17
    private $scannerMode;
18
19
    /**
20
     * Set the default parsing style
21
     */
22 36
    public function __construct()
23
    {
24
        /**
25
         * INI_SCANNER_TYPED won't work on hhvm
26
         * @see https://github.com/facebook/hhvm/issues/5239
27
         */
28 36
        if (defined('HHVM_VERSION')) {
29
            $this->setScannerMode(\INI_SCANNER_NORMAL);
30
        } else {
31 36
            $this->setScannerMode(\INI_SCANNER_TYPED);
32
        }
33 36
    }
34
35 36
    public function setScannerMode($scannerMode)
36
    {
37 36
        $this->scannerMode = $scannerMode;
38 36
    }
39
40
    /**
41
     * parse the .env file
42
     *
43
     * @param string $file file to parse
44
     *
45
     * @return string Returns the phrase passed in
46
     */
47 36
    public function parse($file)
48
    {
49
        try {
50 36
            $this->content = parse_ini_file(
51 24
                $file,
52 36
                false,
53 36
                $this->scannerMode
54 24
            );
55 26
        } catch (\Exception $e) {
56 6
            throw new Exception\ParseException($e);
57
        }
58
59 30
        return $this;
60
    }
61
62
    public function sanitizeValues()
63
    {
64 5
        array_walk($this->content, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
            // sanitize boolean values
66 5
            if (in_array($value, ['true', 'on', 'yes', 'false', 'off', 'no'])) {
67 3
                $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
68 2
            }
69 3
        });
70
71 3
        return $this;
72
    }
73
74 30
    public function getContent()
75
    {
76 30
        return $this->content;
77
    }
78
}
79