env()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 23
rs 7.6666
c 1
b 0
f 0
cc 10
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common;
5
6
use Laminas\Config\Factory;
7
use Laminas\Stdlib\Glob;
8
9
use function getenv;
10
use function json_decode as spl_json_decode;
11
use function json_last_error;
12
use function json_last_error_msg;
13
use function sprintf;
14
use function strtolower;
15
use function trim;
16
17
use const JSON_ERROR_NONE;
18
19
/**
20
 * Gets the value of an environment variable. Supports boolean, empty and null.
21
 * This is basically Laravel's env helper
22
 *
23
 * @param string $key
24
 * @param mixed $default
25
 * @return mixed
26
 * @link https://github.com/laravel/framework/blob/5.2/src/Illuminate/Foundation/helpers.php#L369
27
 */
28
function env($key, $default = null)
29
{
30
    $value = getenv($key);
31
    if ($value === false) {
32
        return $default;
33
    }
34
35
    switch (strtolower($value)) {
36
        case 'true':
37
        case '(true)':
38
            return true;
39
        case 'false':
40
        case '(false)':
41
            return false;
42
        case 'empty':
43
        case '(empty)':
44
            return '';
45
        case 'null':
46
        case '(null)':
47
            return null;
48
    }
49
50
    return trim($value);
51
}
52
53
/**
54
 * @throws Exception\InvalidArgumentException
55
 */
56
function json_decode(string $json, int $depth = 512, int $options = 0): array
57
{
58
    $data = spl_json_decode($json, true, $depth, $options);
59
    if (JSON_ERROR_NONE !== json_last_error()) {
60
        throw new Exception\InvalidArgumentException(sprintf('Error decoding JSON: %s', json_last_error_msg()));
61
    }
62
63
    return $data;
64
}
65