Completed
Push — master ( 7c199b...1d9709 )
by Rémi
02:44
created

Loader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parse() 0 6 1
A getParser() 0 4 1
A toEnv() 0 10 3
A putenv() 0 11 2
A getenv() 0 12 4
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 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...
20 21
    {
21 21
        $this->parser = new Parser;
22 7
23 14
        $this->parser->parse($this->file);
24
    }
25
26
    /**
27 21
     * Return the parser
28 21
     */
29
    public function getParser()
30
    {
31 21
        return $this->parser;
32 21
    }
33 21
34 14
    public function toEnv()
35 21
    {
36 14
        if (!isset($this->parser)) {
37 21
            throw new \LogicException("You must call parse() before", 1);
38
        }
39 18
40
        foreach ($this->parser->getContent() as $key => $value) {
41 12
            static::putenv($key, $value);
42 18
        }
43 9
    }
44 9
45 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...
46 4
    {
47 6
        $_ENV[$key]    = $value;
48 6
        $_SERVER[$key] = $value;
49 4
50
        /*
51
         * putenv only accept string value
52
         */
53
        $value = $value === null ? 'null' : $value;
54
        putenv("$key=$value");
55
    }
56
57
    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...
58
    {
59
        switch (true) {
60
            case array_key_exists($key, $_ENV):
61
                return $_ENV[$key];
62
            case array_key_exists($key, $_SERVER):
63
                return $_SERVER[$key];
64
            default:
65
                $value = getenv($key);
66
                return $value === false ? $default : $value;
67
        }
68
    }
69
}
70