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.
Completed
Push — master ( 0a6f96...b0c756 )
by Ross
12s queued 10s
created

SimpleEnv   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B processLine() 0 35 5
A setEnv() 0 11 4
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException;
6
7
/**
8
 * Class SimpleEnv
9
 *
10
 * A simplistic take on reading .env files
11
 *
12
 * We only require parsing very simple key=value pairs
13
 *
14
 * For a more fully featured library, see https://github.com/vlucas/phpdotenv
15
 *
16
 * @package EdmondsCommerce\DoctrineStaticMeta
17
 * @SuppressWarnings(PHPMD.Superglobals)
18
 */
19
class SimpleEnv
20
{
21
    public static function setEnv(string $filePath, array &$server = null): void
22
    {
23
        if (null === $server) {
24
            $server =& $_SERVER;
25
        }
26
        if (!file_exists($filePath)) {
27
            throw new ConfigException('Env file path '.$filePath.' does not exist');
28
        }
29
        $lines = file($filePath);
30
        foreach ($lines as $line) {
31
            self::processLine($line, $server);
32
        }
33
    }
34
35
    private static function processLine(string $line, array &$server)
36
    {
37
        #skip comments
38
        if (preg_match('%^\s*#%', $line)) {
39
            return;
40
        }
41
        preg_match(
42
            #strip leading spaces
43
            '%^[[:space:]]*'
44
            #strip leading `export`
45
            .'(?:export[[:space:]]+|)'
46
            #parse out the key and assign to named match
47
            .'(?<key>[^=]+?)'
48
            #strip out `=`, possibly with space around it
49
            .'[[:space:]]*=[[:space:]]*'
50
            #strip out possible quotes
51
            ."(?:\"|'|)"
52
            #parse out the value and assign to named match
53
            ."(?<value>[^\"']+?)"
54
            #strip out possible quotes
55
            ."(?:\"|'|)"
56
            #string out trailing space to end of line
57
            .'[[:space:]]*$%',
58
            $line,
59
            $matches
60
        );
61
        if (5 !== count($matches)) {
62
            return;
63
        }
64
        list(, $key, $value) = $matches;
65
        if (empty($value)) {
66
            $value = '';
67
        }
68
        if (!isset($server[$key])) {
69
            $server[$key] = $value;
70
        }
71
    }
72
}
73