boolToStr()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlecRabbit;
4
5
use const AlecRabbit\Helpers\Strings\Constants\STR_DOUBLE;
6
use const AlecRabbit\Helpers\Strings\Constants\STR_DOUBLE_LENGTH;
7
use const AlecRabbit\Helpers\Strings\Constants\STR_EMPTY;
8
use const AlecRabbit\Helpers\Strings\Constants\STR_FALSE;
9
use const AlecRabbit\Helpers\Strings\Constants\STR_FLOAT;
10
use const AlecRabbit\Helpers\Strings\Constants\STR_NULL;
11
use const AlecRabbit\Helpers\Strings\Constants\STR_TRUE;
12
13
/**
14
 * Gets the value of an environment variable.
15
 *
16
 * @param string $key
17
 * @param mixed $default
18
 * @return mixed
19
 */
20
function env($key, $default = null)
21
{
22 42
    if (false === $value = \getenv($key)) {
23 18
        $value = value($default);
24
    }
25
26 42
    $value = \ltrim(\rtrim($value, ')"'), '("');
27
28 42
    switch (strtolower($value)) {
29 42
        case STR_TRUE:
30 4
            $value = true;
31 4
            break;
32 38
        case STR_FALSE:
33 4
            $value = false;
34 4
            break;
35 34
        case STR_EMPTY:
36 30
        case STR_NULL:
37 8
            $value = '';
38 8
            break;
39
    }
40
41 42
    return $value;
42
}
43
44
/**
45
 * Return the default value of the given value.
46
 *
47
 * @param mixed $value
48
 * @return mixed
49
 */
50
function value($value)
51
{
52
    return
53 18
        $value instanceof \Closure ?
54 18
            $value() : $value;
55
}
56
57
/**
58
 * Returns string representation of a bool value.
59
 *
60
 * @param null|bool $value
61
 * @return string
62
 */
63
function boolToStr(?bool $value): string
64
{
65 3
    if (null === $value) {
66 1
        return STR_NULL;
67
    }
68 2
    return $value ? STR_TRUE : STR_FALSE;
69
}
70
71
/**
72
 * Returns the type of a variable.
73
 *
74
 * @param mixed $var
75
 * @return string
76
 */
77
function typeOf($var): string
78
{
79 14
    $type = \is_object($var) ? \get_class($var) : \gettype($var);
80 14
    if (strlen($type) === STR_DOUBLE_LENGTH) {
81 2
        $type = str_replace(STR_DOUBLE, STR_FLOAT, $type);
82
    }
83 14
    return $type;
84
}
85