TypeCaster   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 61
ccs 41
cts 41
cp 1
rs 10
c 3
b 0
f 0
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
F toNum() 0 52 15
A toBool() 0 5 3
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
class TypeCaster
19
{
20 146
    public static function toNum(string $val): float|int
21
    {
22 146
        $f = (float) $val;
23 146
        $i = (int) $val;
24 146
        if ($i == $f) {
25 144
            $res = $i;
26
        } else {
27 5
            $res = $f;
28
        }
29
30 146
        if (preg_match('/[^0-9]*$/', strtolower($val), $matches)) {
31 146
            switch ($matches[0]) {
32 146
                case 'g':
33 1
                    $res *= 1000;
34
                    /* no break */
35 146
                case 'm':
36 1
                    $res *= 1000;
37
                    /* no break */
38 146
                case 'k':
39 4
                    $res *= 1000;
40 4
                    break;
41 146
                case 'gb':
42 1
                    $res *= 1024;
43
                    /* no break */
44 146
                case 'mb':
45 3
                    $res *= 1024;
46
                    /* no break */
47 146
                case 'kb':
48 3
                    $res *= 1024;
49 3
                    break;
50 146
                case 'y':
51 1
                    $res *= 60 * 60 * 24 * 365;
52 1
                    break;
53 146
                case 'w':
54 1
                    $res *= 7;
55
                    /* no break */
56 146
                case 'd':
57 1
                    $res *= 24;
58
                    /* no break */
59 146
                case 'h':
60 1
                    $res *= 60;
61
                    /* no break */
62 146
                case 'min':
63 1
                    $res *= 60;
64 1
                    break;
65 146
                case 'ms':
66 1
                    $res /= 1000;
67 1
                    break;
68
            }
69
        }
70
71 146
        return $res;
72
    }
73
74 77
    public static function toBool(string $val): bool
75
    {
76 77
        $val = strtolower($val);
77
78 77
        return 'true' === $val || 'yes' === $val || 'on' === $val;
79
    }
80
}
81