Passed
Pull Request — master (#250)
by Alejandro
04:14
created

contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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