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

Loader::getenv()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Rfussien\Dotenv;
4
5
class Loader
6
{
7
    /**
8
     * Set the env file
9
     */
10
    protected $file;
11
12
    protected $parser;
13
14 3
    public function __construct($path, $filename = '.env')
15
    {
16 3
        $path = rtrim($path, DIRECTORY_SEPARATOR);
17 3
        $this->file = $path . DIRECTORY_SEPARATOR . $filename;
18
19 3
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
20
    }
21
22
    /**
23
     * parse and set the env
24
     */
25
    public function load()
26
    {
27
        $this->parse();
28
        $this->toEnv();
29
30
        return $this;
31
    }
32
33 3
    public function parse(array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
34
    {
35 3
        $this->parser = new Parser;
36
37 3
        $this->parser->parse($this->file);
38
39 3
        return $this;
40
    }
41
42
    /**
43
     * Return the parser
44
     */
45
    public function getParser()
46
    {
47
        return $this->parser;
48
    }
49
50 3
    public function toEnv()
51
    {
52 3
        if (!isset($this->parser)) {
53
            throw new \LogicException("You must call parse() before", 1);
54
        }
55
56 3
        foreach ($this->parser->getContent() as $key => $value) {
57 3
            static::putenv($key, $value);
58 2
        }
59
60 3
        return $this;
61
    }
62
63 3
    public static function putenv($key, $value)
2 ignored issues
show
Coding Style introduced by
putenv uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
putenv uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
64
    {
65 3
        $_ENV[$key]    = $value;
66 3
        $_SERVER[$key] = $value;
67
68
        /*
69
         * putenv only accept string value
70
         */
71 3
        $value = $value === null ? 'null' : $value;
72 3
        putenv("$key=$value");
73 3
    }
74
75
    public static function getenv($key, $default = null)
2 ignored issues
show
Coding Style introduced by
getenv uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getenv uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
    {
77
        switch (true) {
78
            case array_key_exists($key, $_ENV):
79
                return $_ENV[$key];
80
            case array_key_exists($key, $_SERVER):
81
                return $_SERVER[$key];
82
            default:
83
                $value = getenv($key);
84
                return $value === false ? $default : $value;
85
        }
86
    }
87
}
88