Completed
Push — master ( 1d9709...0fe4a5 )
by Rémi
02:48 queued 43s
created

Loader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Rfussien\Dotenv;
4
5
class Loader
6
{
7 21
    protected $file;
8
9 21
    protected $parser;
10
11 21
    public function __construct($path, $filename = '.env')
12 21
    {
13 14
        $path = rtrim($path, DIRECTORY_SEPARATOR);
14
        $this->file = $path . DIRECTORY_SEPARATOR . $filename;
15 21
16
        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...
17
    }
18 21
19
    public function load()
20 21
    {
21 21
        $this->parse();
22 7
        $this->toEnv();
23 14
    }
24
25
    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...
26
    {
27 21
        $this->parser = new Parser;
28 21
29
        $this->parser->parse($this->file);
30
    }
31 21
32 21
    /**
33 21
     * Return the parser
34 14
     */
35 21
    public function getParser()
36 14
    {
37 21
        return $this->parser;
38
    }
39 18
40
    public function toEnv()
41 12
    {
42 18
        if (!isset($this->parser)) {
43 9
            throw new \LogicException("You must call parse() before", 1);
44 9
        }
45 3
46 4
        foreach ($this->parser->getContent() as $key => $value) {
47 6
            static::putenv($key, $value);
48 6
        }
49 4
    }
50
51
    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...
52
    {
53
        $_ENV[$key]    = $value;
54
        $_SERVER[$key] = $value;
55
56
        /*
57
         * putenv only accept string value
58
         */
59
        $value = $value === null ? 'null' : $value;
60
        putenv("$key=$value");
61
    }
62
63
    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...
64
    {
65
        switch (true) {
66
            case array_key_exists($key, $_ENV):
67
                return $_ENV[$key];
68
            case array_key_exists($key, $_SERVER):
69
                return $_SERVER[$key];
70
            default:
71
                $value = getenv($key);
72
                return $value === false ? $default : $value;
73
        }
74
    }
75
}
76