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
03:53
created

IniParser::validateItemName()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 18
cts 21
cp 0.8571
rs 8.5806
cc 4
eloc 15
nc 8
nop 1
crap 4.0466
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 42
    public static function i()
37
    {
38 42
        return static::getInstance();
39
    }
40
41
    /**
42
     * @return IniParser
43
     */
44 42
    public static function getInstance()
45
    {
46 42
        if (is_null(static::$instance))
47 42
        {
48 1
            static::$instance = new static();
49 1
        }
50
51 42
        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 22
    public function parseIniString($string)
70
    {
71 22
        $parsedContents = [];
72
73 22
        if (strlen($string) > 0)
74 22
        {
75 15
            $rawContents = @parse_ini_string($string, true, INI_SCANNER_RAW);
76 15
            if (false === $rawContents)
77 15
            {
78 1
                throw new InvalidDataException('Error parsing ini string!');
79
            }
80
81 14
            $parsedContents = $this->parseArray($rawContents);
82 14
        }
83
84 21
        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 17
    public function castItemValueToProperType($value)
95
    {
96 17
        $normalized = $value;
97
98 17
        if (in_array($value, ['true', 'on', 'yes']))
99 17
        {
100 2
            $normalized = true;
101 2
        }
102 17
        elseif (in_array($value, ['false', 'off', 'no', 'none']))
103
        {
104 1
            $normalized = false;
105 1
        }
106 16
        elseif ('null' == $value)
107
        {
108 1
            $normalized = null;
109 1
        }
110 16
        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 17
        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 28
    public function itemValuetoStringRepresentation($value)
135
    {
136 28
        if (is_bool($value))
137 28
        {
138 2
            $castedValue = (true === $value) ? 'true' : 'false';
139 2
        }
140 28
        elseif (is_null($value))
141
        {
142 2
            $castedValue = 'null';
143 2
        }
144 28
        elseif (is_array($value))
145
        {
146 23
            $castedValue = [];
147 23
            foreach ($value as $k => $v)
148
            {
149 23
                $castedValue[$k] = $this->itemValuetoStringRepresentation($v);
150 23
            }
151 23
        }
152 28
        elseif (is_numeric($value))
153
        {
154 12
            $castedValue = (string)$value;
155 12
        }
156 28
        elseif (is_string($value))
157
        {
158 28
            $castedValue = $value;
159 28
        }
160
        else
161
        {
162
            throw new InvalidDataException('Invalid item value type!');
163
        }
164
165 28
        return $castedValue;
166
    }
167
168
    /**
169
     * @param string $name
170
     *
171
     * @return array [validationResult, message]
172
     */
173 9
    public function validateItemName($name)
174
    {
175 9
        $valid = true;
176 9
        $message = 'Valid item name.';
177 9
        if (!is_string($name))
178 9
        {
179
            $valid = false;
180
            $message = 'Only string values are allowed for item names!';
181
        }
182
183 9
        if (in_array($name, $this->invalidItemNames))
184 9
        {
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 9
        if (preg_match(sprintf('/[%s]/', $this->invalidCharsForItemNames), $name) > 0)
191 9
        {
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 9
        return [$valid, $message];
198
    }
199
200
201
    /**
202
     * @param array $rawContents
203
     *
204
     * @return IniSection[]
205
     * @throws InvalidDataException
206
     */
207 19
    public function parseArray(array $rawContents)
208
    {
209 19
        $parsedContents = [];
210 19
        foreach ($rawContents as $sectionFullName => $sectionContents)
211
        {
212 17
            $pieces = explode(self::SECTION_INHERITANCE_OPERATOR, $sectionFullName, 2);
213 17
            $sectionName = trim($pieces[0]);
214 17
            if (!is_array($sectionContents))
215 17
            {
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 15
            $parsedContents[$sectionName] = new IniSection($sectionName);
221 15
            $parsedContents[$sectionName]->setContents($sectionContents);
222 15
            $parentName = isset($pieces[1]) ? trim($pieces[1]) : null;
223 15
            if (!is_null($parentName))
224 15
            {
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 17
        }
234
235 17
        return $parsedContents;
236
    }
237
238
}