Completed
Push — master ( 07e922...1cff36 )
by Rémi
02:41
created

Parser::setScannerMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 24
    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 24
        if (defined('HHVM_VERSION')) {
29
            $this->setScannerMode(\INI_SCANNER_NORMAL);
30
        } else {
31 24
            $this->setScannerMode(\INI_SCANNER_TYPED);
32
        }
33 24
    }
34
35 24
    public function setScannerMode($scannerMode)
36
    {
37 24
        $this->scannerMode = $scannerMode;
38 24
    }
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 24
    public function parse($file)
48
    {
49
        try {
50 24
            $this->content = parse_ini_file(
51 16
                $file,
52 24
                false,
53 24
                $this->scannerMode
54 16
            );
55 18
        } catch (\Exception $e) {
56 6
            throw new Exception\ParseException($e);
57
        }
58
59 18
        return $this;
60
    }
61
62 2
    public function sanitizeKeys()
63
    {
64 2
        $sanitisedContent = [];
65
66 2
        foreach ($this->content as $key => $value) {
67
            $key = preg_replace('/ *export */', '', trim($key));
68
69
            $sanitisedContent[$key] = $value;
70
        }
71
72
        $this->content = $sanitisedContent;
73
74
        return $this;
75
    }
76
77
    public function sanitizeValues()
78
    {
79 3
        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...
80
            // sanitize boolean values
81 3
            if (in_array($value, ['true', 'on', 'yes', 'false', 'off', 'no'])) {
82 3
                $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
83 2
            }
84 3
        });
85
86 3
        return $this;
87
    }
88
89 18
    public function getContent()
90
    {
91 18
        return $this->content;
92
    }
93
}
94