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

Loader::putenv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
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 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