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
Pull Request — master (#51)
by joseph
102:38 queued 99:24
created

TypeHelper::getType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
crap 5
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
4
5
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
6
7
class TypeHelper
8
{
9
10
    /**
11
     * The standard gettype function output is not brilliantly up to date with modern PHP types
12
     *
13
     * @param mixed $var
14
     *
15
     * @return string
16
     */
17 8
    public function getType($var): string
18
    {
19 8
        $type = \gettype($var);
20 8
        if ('double' === $type) {
21 2
            return 'float';
22
        }
23 8
        if ('integer' === $type) {
24 2
            return 'int';
25
        }
26 8
        if ('NULL' === $type) {
27 7
            return 'null';
28
        }
29 2
        if ('boolean' === $type) {
30 2
            return 'bool';
31
        }
32
33 1
        return $type;
34
    }
35
36
    /**
37
     * When a default is passed in via CLI then the type can not be expected to be set correctly
38
     *
39
     * This function takes the string value and normalises it into the correct value based on the expected type
40
     *
41
     * @param mixed  $value
42
     * @param string $expectedType
43
     *
44
     * @return mixed
45
     */
46 8
    public function normaliseValueToType($value, string $expectedType)
47
    {
48 8
        if (null === $value) {
49 6
            return null;
50
        }
51 2
        switch ($expectedType) {
52 2
            case 'string':
53
                return $this->normaliseString($value);
54 2
            case 'bool':
55 2
                return $this->normaliseBool($value);
56 2
            case 'int':
57 2
                return $this->normaliseInt($value);
58 2
            case 'float':
59 2
                return $this->normaliseFloat($value);
60
            case '\DateTime':
61
                return $this->normaliseDateTime($value);
62
            default:
63
                throw new \RuntimeException('hit unexpected type '.$expectedType.' in '.__METHOD__);
64
        }
65
    }
66
67
    private function normaliseString($value): string
68
    {
69
        return trim((string)$value);
70
    }
71
72 2
    private function normaliseBool($value): bool
73
    {
74 2
        if (\is_bool($value)) {
75
            return $value;
76
        }
77 2
        $value = trim($value);
78 2
        if (0 === strcasecmp('true', $value)) {
79 2
            return true;
80
        }
81 2
        if (0 === strcasecmp('false', $value)) {
82 2
            return false;
83
        }
84
        throw new \RuntimeException('Invalid bool value: '.$value);
85
    }
86
87 2
    private function normaliseInt($value): int
88
    {
89 2
        $value = trim((string)$value);
90 2
        if (is_numeric($value)) {
91 2
            return (int)$value;
92
        }
93
        throw new \RuntimeException('Invalid int default value: '.$value);
94
    }
95
96 2
    private function normaliseFloat($value): float
97
    {
98 2
        $value = trim((string)$value);
99 2
        if (is_numeric($value)) {
100 2
            return (float)$value;
101
        }
102
        throw new \RuntimeException('Invalid float default value: '.$value);
103
    }
104
105
    /**
106
     * @param mixed $value
107
     *
108
     * @return int
109
     * @SuppressWarnings(PHPMD.StaticAccess)
110
     */
111
    private function normaliseDateTime($value): int
112
    {
113
        $value = trim($value);
114
        switch (true) {
115
            case (0 === strcasecmp('current', $value)):
116
            case (0 === strcasecmp('current_timestamp', $value)):
117
            case (0 === strcasecmp('now', $value)):
118
                return MappingHelper::DATETIME_DEFAULT_CURRENT_TIME_STAMP;
119
        }
120
        throw new \RuntimeException('Invalid DateTime default value: '.$value);
121
    }
122
}
123