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 (#4)
by Flemming
02:51
created

IniParser::castItemValueToProperType()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
ccs 23
cts 23
cp 1
rs 6.7272
cc 7
eloc 15
nc 7
nop 1
crap 7
1
<?php
2
3
4
namespace FlmBus\Ini;
5
6
use FlmBus\Ini\Exceptions\InvalidDataException;
7
8
class IniParser
9
{
10
11
    const SECTION_INHERITANCE_OPERATOR = ':';
12
13
    /**
14
     * @var IniParser
15
     */
16
    protected static $instance;
17
    /**
18
     * @var string
19
     */
20
    protected $invalidCharsForItemNames = '?{}|&~!()^"';
21
    /**
22
     * @var array
23
     */
24
    protected $invalidItemNames = ['null', 'yes', 'no', 'true', 'false', 'on', 'off', 'none'];
25
26
    /**
27
     * IniParser constructor.
28
     */
29 1
    protected function __construct()
30
    {
31 1
    }
32
33
    /**
34
     * @return IniParser
35
     */
36 44
    public static function i()
37
    {
38 44
        return static::getInstance();
39
    }
40
41
    /**
42
     * @return IniParser
43
     */
44 44
    public static function getInstance()
45
    {
46 44
        if (is_null(static::$instance))
47 44
        {
48 1
            static::$instance = new static();
49 1
        }
50
51 44
        return static::$instance;
52
    }
53
54
    private function __clone()
55
    {
56
    }
57
58
    /** @noinspection PhpUnusedPrivateMethodInspection */
59
    private function __wakeup()
60
    {
61
    }
62
63
    /**
64
     * @param $string
65
     *
66
     * @return IniSection[]
67
     * @throws InvalidDataException
68
     */
69 23
    public function parseIniString($string)
70
    {
71 23
        $parsedContents = [];
72
73 23
        if (strlen($string) > 0)
74 23
        {
75 16
            $rawContents = @parse_ini_string($string, true, INI_SCANNER_RAW);
76 16
            if (false === $rawContents)
77 16
            {
78 1
                throw new InvalidDataException('Error parsing ini string!');
79
            }
80
81 15
            $parsedContents = $this->parseArray($rawContents);
82 15
        }
83
84 22
        return $parsedContents;
85
    }
86
87
    /**
88
     * Cast item string value to proper type
89
     *
90
     * @param string $value
91
     *
92
     * @return array|string|bool|int|float|null
93
     */
94 19
    public function castItemValueToProperType($value)
95
    {
96 19
        $normalized = $value;
97
98 19
        if (in_array($value, ['true', 'on', 'yes']))
99 19
        {
100 2
            $normalized = true;
101 2
        }
102 19
        elseif (in_array($value, ['false', 'off', 'no', 'none']))
103
        {
104 1
            $normalized = false;
105 1
        }
106 18
        elseif ('null' == $value)
107
        {
108 1
            $normalized = null;
109 1
        }
110 18
        elseif (is_numeric($value))
111
        {
112 3
            $number = $value + 0;
113 3
            if (intval($number) == $number)
114 3
            {
115 2
                $normalized = (int)$number;
116 2
            }
117 2
            elseif (floatval($number) == $number)
118
            {
119 2
                $normalized = (float)$number;
120 2
            }
121 3
        }
122
123 19
        return $normalized;
124
    }
125
126
    /**
127
     * Get an string (or an array of strings) representation of $value
128
     *
129
     * @param bool|int|float|null|array $value
130
     *
131
     * @return array|string
132
     * @throws InvalidDataException
133
     */
134 30
    public function itemValuetoStringRepresentation($value)
135
    {
136 30
        if (is_bool($value))
137 30
        {
138 2
            $castedValue = (true === $value) ? 'true' : 'false';
139 2
        }
140 30
        elseif (is_null($value))
141
        {
142 2
            $castedValue = 'null';
143 2
        }
144 30
        elseif (is_array($value))
145
        {
146 24
            $castedValue = [];
147 24
            foreach ($value as $k => $v)
148
            {
149 24
                $castedValue[$k] = $this->itemValuetoStringRepresentation($v);
150 24
            }
151 24
        }
152 30
        elseif (is_numeric($value))
153
        {
154 12
            $castedValue = (string)$value;
155 12
        }
156 30
        elseif (is_string($value))
157
        {
158 30
            $castedValue = $value;
159 30
        }
160
        else
161
        {
162
            throw new InvalidDataException('Invalid item value type!');
163
        }
164
165 30
        return $castedValue;
166
    }
167
168
    /**
169
     * @param string $name
170
     *
171
     * @return array [validationResult, message]
172
     */
173 11
    public function validateItemName($name)
174
    {
175 11
        $valid = true;
176 11
        $message = 'Valid item name.';
177 11
        if (!is_string($name))
178 11
        {
179
            $valid = false;
180
            $message = 'Only string values are allowed for item names!';
181
        }
182
183 11
        if (in_array($name, $this->invalidItemNames))
184 11
        {
185 1
            $valid = false;
186 1
            $message = sprintf('Item name is not allowed! Not allowed item names: %s',
187 1
                               implode(', ', $this->invalidItemNames));
188 1
        }
189
190 11
        if (preg_match(sprintf('/[%s]/', $this->invalidCharsForItemNames), $name) > 0)
191 11
        {
192 2
            $valid = false;
193 2
            $message = sprintf('Invalid name for ini item! Provided item name contains not ' .
194 2
                               'allowed chars (%s).', $this->invalidCharsForItemNames);
195 2
        }
196
197 11
        return [$valid, $message];
198
    }
199
200
201
    /**
202
     * @param array $rawContents
203
     *
204
     * @return IniSection[]
205
     * @throws InvalidDataException
206
     */
207 20
    public function parseArray(array $rawContents)
208
    {
209 20
        $parsedContents = [];
210 20
        foreach ($rawContents as $sectionFullName => $sectionContents)
211
        {
212 18
            $pieces = explode(self::SECTION_INHERITANCE_OPERATOR, $sectionFullName, 2);
213 18
            $sectionName = trim($pieces[0]);
214 18
            if (!is_array($sectionContents))
215 18
            {
216 2
                throw new InvalidDataException(sprintf('Orphan fields are not allowed! ' .
217 2
                                                       'Please define a section for field "%s".',
218 2
                                                       $sectionName));
219
            }
220 16
            $parsedContents[$sectionName] = new IniSection($sectionName);
221 16
            $parsedContents[$sectionName]->setContents($sectionContents);
222 16
            $parentName = isset($pieces[1]) ? trim($pieces[1]) : null;
223 16
            if (!is_null($parentName))
224 16
            {
225 10
                if (!isset($parsedContents[$parentName]))
226 10
                {
227
                    throw new InvalidDataException(sprintf('Parent section not found! ' .
228
                                                           'Define "%s" section before "%s" section.',
229
                                                           $parentName, $sectionName));
230
                }
231 10
                $parsedContents[$sectionName]->setParent($parsedContents[$parentName]);
232 10
            }
233 18
        }
234
235 18
        return $parsedContents;
236
    }
237
238
}