GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( c655a9...812c4e )
by Benjamin
03:29
created

EnvVarsSetter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 79
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
B loadEnv() 0 19 8
A doLoad() 0 9 4
B populate() 0 14 7
A __construct() 0 3 1
1
<?php
2
3
namespace Lib\Env;
4
5
use Lib\Env\Exception\EnvException;
6
use Lib\Env\Exception\PathException;
7
use Lib\Env\Parser\EnvParserInterface;
8
9
final class EnvVarsSetter
10
{
11
    private const DIST_EXT = '.dist';
12
13
    private const ENV_DEV = 'dev';
14
    private const ENV_TEST = 'test';
15
    private const ENV_PROD = 'prod';
16
17
    /** @var array */
18
    private $envVars;
19
20
    /** @var EnvParserInterface */
21
    private $parser;
22
23
    public function __construct(EnvParserInterface $parser)
24
    {
25
        $this->parser = $parser;
26
    }
27
28
    /**
29
     * @param string $path
30
     * @param string $envVarName
31
     * @param string $defaultEnv
32
     * @param array $testEnvs
33
     * @throws EnvException
34
     */
35
    public function loadEnv(string $path, string $envVarName = 'APP_ENV', string $defaultEnv = 'dev', $testEnvs = ['test']): void
36
    {
37
        if (null === $env = $_SERVER[$envVarName] ?? $_ENV[$envVarName] ?? null) {
38
            $this->envVars[$envVarName] = $env = $defaultEnv;
39
        }
40
        if (file_exists($path) && !file_exists($file = "$path.dist")) {
41
            $this->doLoad($path);
42
        } else {
43
            $this->doLoad($file);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $file does not seem to be defined for all execution paths leading up to this point.
Loading history...
44
        }
45
46
        if (!\in_array($env, $testEnvs, true) &&  file_exists($file = "$path.local")) {
47
            $this->doLoad($file);
48
        }
49
        if (file_exists($file = "$path.$env")) {
50
            $this->doLoad($file);
51
        }
52
        if (file_exists($file = "$path.$env.local")) {
53
            $this->doLoad($file);
54
        }
55
    }
56
57
    /**
58
     * @param string $path
59
     * @return string
60
     * @throws EnvException
61
     */
62
    public function doLoad(string $path): string
63
    {
64
        if (!is_readable($path) || is_dir($path)) {
65
            throw new PathException();
66
        }
67
        try {
68
            $this->populate($path);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
69
        } catch (EnvException $e) {
70
            throw $e;
71
        }
72
    }
73
74
    private function populate(string $path, bool $override = false): void
75
    {
76
        $vars = $this->parser->parse(file_get_contents($path));
77
        foreach ($vars as $varName => $value) {
78
            $httpVar = 0 !== strpos($varName, 'HTTP_');
79
80
            if (!$override && (isset($_ENV[$varName]) || ($httpVar && isset($_SERVER[$varName])))) {
81
                continue;
82
            }
83
84
            putenv("$varName=$value");
85
            $this->envVars[$varName] = $_ENV[$varName] = $value;
86
            if ($httpVar) {
87
                $_SERVER[$varName] = $value;
88
            }
89
        }
90
    }
91
}
92