Conditions | 75 |
Paths | 3485 |
Total Lines | 257 |
Code Lines | 158 |
Lines | 8 |
Ratio | 3.11 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
53 | public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) |
||
54 | { |
||
55 | if (!preg_match('//u', $value)) { |
||
56 | throw new ParseException('The YAML value does not appear to be valid UTF-8.'); |
||
57 | } |
||
58 | $this->currentLineNb = -1; |
||
59 | $this->currentLine = ''; |
||
60 | $value = $this->cleanup($value); |
||
61 | $this->lines = explode("\n", $value); |
||
62 | |||
63 | if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) { |
||
64 | $mbEncoding = mb_internal_encoding(); |
||
65 | mb_internal_encoding('UTF-8'); |
||
66 | } |
||
67 | |||
68 | $data = array(); |
||
69 | $context = null; |
||
70 | $allowOverwrite = false; |
||
71 | while ($this->moveToNextLine()) { |
||
72 | if ($this->isCurrentLineEmpty()) { |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | // tab? |
||
77 | if ("\t" === $this->currentLine[0]) { |
||
78 | throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
79 | } |
||
80 | |||
81 | $isRef = $mergeNode = false; |
||
82 | if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) { |
||
83 | if ($context && 'mapping' == $context) { |
||
84 | throw new ParseException('You cannot define a sequence item when in a mapping'); |
||
85 | } |
||
86 | $context = 'sequence'; |
||
87 | |||
88 | if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) { |
||
89 | $isRef = $matches['ref']; |
||
90 | $values['value'] = $matches['value']; |
||
91 | } |
||
92 | |||
93 | // array |
||
94 | if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { |
||
95 | $c = $this->getRealCurrentLineNb() + 1; |
||
96 | $parser = new self($c); |
||
97 | $parser->refs = &$this->refs; |
||
98 | $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap); |
||
99 | } else { |
||
100 | if (isset($values['leadspaces']) |
||
101 | && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches) |
||
102 | ) { |
||
103 | // this is a compact notation element, add to next block and parse |
||
104 | $c = $this->getRealCurrentLineNb(); |
||
105 | $parser = new self($c); |
||
106 | $parser->refs = &$this->refs; |
||
107 | |||
108 | $block = $values['value']; |
||
109 | if ($this->isNextLineIndented()) { |
||
110 | $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1); |
||
111 | } |
||
112 | |||
113 | $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap); |
||
114 | } else { |
||
115 | $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context); |
||
116 | } |
||
117 | } |
||
118 | if ($isRef) { |
||
119 | $this->refs[$isRef] = end($data); |
||
120 | } |
||
121 | } elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) { |
||
122 | if ($context && 'sequence' == $context) { |
||
123 | throw new ParseException('You cannot define a mapping item when in a sequence'); |
||
124 | } |
||
125 | $context = 'mapping'; |
||
126 | |||
127 | // force correct settings |
||
128 | Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); |
||
129 | try { |
||
130 | $key = Inline::parseScalar($values['key']); |
||
131 | } catch (ParseException $e) { |
||
132 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
133 | $e->setSnippet($this->currentLine); |
||
134 | |||
135 | throw $e; |
||
136 | } |
||
137 | |||
138 | // Convert float keys to strings, to avoid being converted to integers by PHP |
||
139 | if (is_float($key)) { |
||
140 | $key = (string) $key; |
||
141 | } |
||
142 | |||
143 | if ('<<' === $key) { |
||
144 | $mergeNode = true; |
||
145 | $allowOverwrite = true; |
||
146 | if (isset($values['value']) && 0 === strpos($values['value'], '*')) { |
||
147 | $refName = substr($values['value'], 1); |
||
148 | if (!array_key_exists($refName, $this->refs)) { |
||
149 | throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
150 | } |
||
151 | |||
152 | $refValue = $this->refs[$refName]; |
||
153 | |||
154 | if (!is_array($refValue)) { |
||
155 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
156 | } |
||
157 | |||
158 | foreach ($refValue as $key => $value) { |
||
159 | if (!isset($data[$key])) { |
||
160 | $data[$key] = $value; |
||
161 | } |
||
162 | } |
||
163 | } else { |
||
164 | if (isset($values['value']) && $values['value'] !== '') { |
||
165 | $value = $values['value']; |
||
166 | } else { |
||
167 | $value = $this->getNextEmbedBlock(); |
||
168 | } |
||
169 | $c = $this->getRealCurrentLineNb() + 1; |
||
170 | $parser = new self($c); |
||
171 | $parser->refs = &$this->refs; |
||
172 | $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap); |
||
173 | |||
174 | if (!is_array($parsed)) { |
||
175 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
176 | } |
||
177 | |||
178 | if (isset($parsed[0])) { |
||
179 | // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes |
||
180 | // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier |
||
181 | // in the sequence override keys specified in later mapping nodes. |
||
182 | foreach ($parsed as $parsedItem) { |
||
183 | if (!is_array($parsedItem)) { |
||
184 | throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem); |
||
185 | } |
||
186 | |||
187 | foreach ($parsedItem as $key => $value) { |
||
188 | if (!isset($data[$key])) { |
||
189 | $data[$key] = $value; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } else { |
||
194 | // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the |
||
195 | // current mapping, unless the key already exists in it. |
||
196 | foreach ($parsed as $key => $value) { |
||
197 | if (!isset($data[$key])) { |
||
198 | $data[$key] = $value; |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) { |
||
204 | $isRef = $matches['ref']; |
||
205 | $values['value'] = $matches['value']; |
||
206 | } |
||
207 | |||
208 | if ($mergeNode) { |
||
209 | // Merge keys |
||
210 | } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { |
||
211 | // hash |
||
212 | // if next line is less indented or equal, then it means that the current value is null |
||
213 | if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { |
||
214 | // Spec: Keys MUST be unique; first one wins. |
||
215 | // But overwriting is allowed when a merge node is used in current block. |
||
216 | if ($allowOverwrite || !isset($data[$key])) { |
||
217 | $data[$key] = null; |
||
218 | } |
||
219 | } else { |
||
220 | $c = $this->getRealCurrentLineNb() + 1; |
||
221 | $parser = new self($c); |
||
222 | $parser->refs = &$this->refs; |
||
223 | $value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap); |
||
224 | // Spec: Keys MUST be unique; first one wins. |
||
225 | // But overwriting is allowed when a merge node is used in current block. |
||
226 | if ($allowOverwrite || !isset($data[$key])) { |
||
227 | $data[$key] = $value; |
||
228 | } |
||
229 | } |
||
230 | } else { |
||
231 | $value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context); |
||
232 | // Spec: Keys MUST be unique; first one wins. |
||
233 | // But overwriting is allowed when a merge node is used in current block. |
||
234 | if ($allowOverwrite || !isset($data[$key])) { |
||
235 | $data[$key] = $value; |
||
236 | } |
||
237 | } |
||
238 | if ($isRef) { |
||
239 | $this->refs[$isRef] = $data[$key]; |
||
240 | } |
||
241 | } else { |
||
242 | // multiple documents are not supported |
||
243 | if ('---' === $this->currentLine) { |
||
244 | throw new ParseException('Multiple documents are not supported.'); |
||
245 | } |
||
246 | |||
247 | // 1-liner optionally followed by newline(s) |
||
248 | if (is_string($value) && $this->lines[0] === trim($value)) { |
||
249 | try { |
||
250 | $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); |
||
251 | } catch (ParseException $e) { |
||
252 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
253 | $e->setSnippet($this->currentLine); |
||
254 | |||
255 | throw $e; |
||
256 | } |
||
257 | |||
258 | if (is_array($value)) { |
||
259 | $first = reset($value); |
||
260 | if (is_string($first) && 0 === strpos($first, '*')) { |
||
261 | $data = array(); |
||
262 | foreach ($value as $alias) { |
||
263 | $data[] = $this->refs[substr($alias, 1)]; |
||
264 | } |
||
265 | $value = $data; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | if (isset($mbEncoding)) { |
||
270 | mb_internal_encoding($mbEncoding); |
||
271 | } |
||
272 | |||
273 | return $value; |
||
274 | } |
||
275 | |||
276 | switch (preg_last_error()) { |
||
277 | case PREG_INTERNAL_ERROR: |
||
278 | $error = 'Internal PCRE error.'; |
||
279 | break; |
||
280 | case PREG_BACKTRACK_LIMIT_ERROR: |
||
281 | $error = 'pcre.backtrack_limit reached.'; |
||
282 | break; |
||
283 | case PREG_RECURSION_LIMIT_ERROR: |
||
284 | $error = 'pcre.recursion_limit reached.'; |
||
285 | break; |
||
286 | case PREG_BAD_UTF8_ERROR: |
||
287 | $error = 'Malformed UTF-8 data.'; |
||
288 | break; |
||
289 | case PREG_BAD_UTF8_OFFSET_ERROR: |
||
290 | $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; |
||
291 | break; |
||
292 | default: |
||
293 | $error = 'Unable to parse.'; |
||
294 | } |
||
295 | |||
296 | throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | if (isset($mbEncoding)) { |
||
301 | mb_internal_encoding($mbEncoding); |
||
302 | } |
||
303 | |||
304 | if ($objectForMap && !is_object($data)) { |
||
305 | $data = (object) $data; |
||
306 | } |
||
307 | |||
308 | return empty($data) ? null : $data; |
||
309 | } |
||
310 | |||
773 |