| Conditions | 61 |
| Paths | 749 |
| Total Lines | 208 |
| Code Lines | 139 |
| Lines | 12 |
| Ratio | 5.77 % |
| 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 |
||
| 52 | public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false) |
||
| 53 | { |
||
| 54 | $this->currentLineNb = -1; |
||
| 55 | $this->currentLine = ''; |
||
| 56 | $this->lines = explode("\n", $this->cleanup($value)); |
||
| 57 | |||
| 58 | if (!preg_match('//u', $value)) { |
||
| 59 | throw new ParseException('The YAML value does not appear to be valid UTF-8.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | View Code Duplication | if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { |
|
| 63 | $mbEncoding = mb_internal_encoding(); |
||
| 64 | mb_internal_encoding('UTF-8'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $data = array(); |
||
| 68 | $context = null; |
||
| 69 | while ($this->moveToNextLine()) { |
||
| 70 | if ($this->isCurrentLineEmpty()) { |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | // tab? |
||
| 75 | if ("\t" === $this->currentLine[0]) { |
||
| 76 | throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 77 | } |
||
| 78 | |||
| 79 | $isRef = $isInPlace = $isProcessed = false; |
||
| 80 | if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) { |
||
| 81 | if ($context && 'mapping' == $context) { |
||
|
|
|||
| 82 | throw new ParseException('You cannot define a sequence item when in a mapping'); |
||
| 83 | } |
||
| 84 | $context = 'sequence'; |
||
| 85 | |||
| 86 | View Code Duplication | if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) { |
|
| 87 | $isRef = $matches['ref']; |
||
| 88 | $values['value'] = $matches['value']; |
||
| 89 | } |
||
| 90 | |||
| 91 | // array |
||
| 92 | if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { |
||
| 93 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 94 | $parser = new Parser($c); |
||
| 95 | $parser->refs = & $this->refs; |
||
| 96 | $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport); |
||
| 97 | } else { |
||
| 98 | if (isset($values['leadspaces']) |
||
| 99 | && ' ' == $values['leadspaces'] |
||
| 100 | && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches) |
||
| 101 | ) { |
||
| 102 | // this is a compact notation element, add to next block and parse |
||
| 103 | $c = $this->getRealCurrentLineNb(); |
||
| 104 | $parser = new Parser($c); |
||
| 105 | $parser->refs = & $this->refs; |
||
| 106 | |||
| 107 | $block = $values['value']; |
||
| 108 | if ($this->isNextLineIndented()) { |
||
| 109 | $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2); |
||
| 110 | } |
||
| 111 | |||
| 112 | $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport); |
||
| 113 | } else { |
||
| 114 | $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } 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('"', "'")))) { |
||
| 118 | if ($context && 'sequence' == $context) { |
||
| 119 | throw new ParseException('You cannot define a mapping item when in a sequence'); |
||
| 120 | } |
||
| 121 | $context = 'mapping'; |
||
| 122 | |||
| 123 | // force correct settings |
||
| 124 | Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $this->refs); |
||
| 125 | try { |
||
| 126 | $key = Inline::parseScalar($values['key']); |
||
| 127 | } catch (ParseException $e) { |
||
| 128 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 129 | $e->setSnippet($this->currentLine); |
||
| 130 | |||
| 131 | throw $e; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ('<<' === $key) { |
||
| 135 | if (isset($values['value']) && 0 === strpos($values['value'], '*')) { |
||
| 136 | $isInPlace = substr($values['value'], 1); |
||
| 137 | if (!array_key_exists($isInPlace, $this->refs)) { |
||
| 138 | throw new ParseException(sprintf('Reference "%s" does not exist.', $isInPlace), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | if (isset($values['value']) && $values['value'] !== '') { |
||
| 142 | $value = $values['value']; |
||
| 143 | } else { |
||
| 144 | $value = $this->getNextEmbedBlock(); |
||
| 145 | } |
||
| 146 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 147 | $parser = new Parser($c); |
||
| 148 | $parser->refs = & $this->refs; |
||
| 149 | $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport); |
||
| 150 | |||
| 151 | $merged = array(); |
||
| 152 | if (!is_array($parsed)) { |
||
| 153 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 154 | } elseif (isset($parsed[0])) { |
||
| 155 | // Numeric array, merge individual elements |
||
| 156 | foreach (array_reverse($parsed) as $parsedItem) { |
||
| 157 | if (!is_array($parsedItem)) { |
||
| 158 | throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem); |
||
| 159 | } |
||
| 160 | $merged = array_merge($parsedItem, $merged); |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | // Associative array, merge |
||
| 164 | $merged = array_merge($merged, $parsed); |
||
| 165 | } |
||
| 166 | |||
| 167 | $isProcessed = $merged; |
||
| 168 | } |
||
| 169 | View Code Duplication | } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) { |
|
| 170 | $isRef = $matches['ref']; |
||
| 171 | $values['value'] = $matches['value']; |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($isProcessed) { |
||
| 175 | // Merge keys |
||
| 176 | $data = $isProcessed; |
||
| 177 | // hash |
||
| 178 | } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { |
||
| 179 | // if next line is less indented or equal, then it means that the current value is null |
||
| 180 | if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { |
||
| 181 | $data[$key] = null; |
||
| 182 | } else { |
||
| 183 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 184 | $parser = new Parser($c); |
||
| 185 | $parser->refs = & $this->refs; |
||
| 186 | $data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | if ($isInPlace) { |
||
| 190 | $data = $this->refs[$isInPlace]; |
||
| 191 | } else { |
||
| 192 | $data[$key] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } else { |
||
| 196 | // 1-liner optionally followed by newline |
||
| 197 | $lineCount = count($this->lines); |
||
| 198 | if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) { |
||
| 199 | try { |
||
| 200 | $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $this->refs); |
||
| 201 | } catch (ParseException $e) { |
||
| 202 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 203 | $e->setSnippet($this->currentLine); |
||
| 204 | |||
| 205 | throw $e; |
||
| 206 | } |
||
| 207 | |||
| 208 | if (is_array($value)) { |
||
| 209 | $first = reset($value); |
||
| 210 | if (is_string($first) && 0 === strpos($first, '*')) { |
||
| 211 | $data = array(); |
||
| 212 | foreach ($value as $alias) { |
||
| 213 | $data[] = $this->refs[substr($alias, 1)]; |
||
| 214 | } |
||
| 215 | $value = $data; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | if (isset($mbEncoding)) { |
||
| 220 | mb_internal_encoding($mbEncoding); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $value; |
||
| 224 | } |
||
| 225 | |||
| 226 | switch (preg_last_error()) { |
||
| 227 | case PREG_INTERNAL_ERROR: |
||
| 228 | $error = 'Internal PCRE error.'; |
||
| 229 | break; |
||
| 230 | case PREG_BACKTRACK_LIMIT_ERROR: |
||
| 231 | $error = 'pcre.backtrack_limit reached.'; |
||
| 232 | break; |
||
| 233 | case PREG_RECURSION_LIMIT_ERROR: |
||
| 234 | $error = 'pcre.recursion_limit reached.'; |
||
| 235 | break; |
||
| 236 | case PREG_BAD_UTF8_ERROR: |
||
| 237 | $error = 'Malformed UTF-8 data.'; |
||
| 238 | break; |
||
| 239 | case PREG_BAD_UTF8_OFFSET_ERROR: |
||
| 240 | $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; |
||
| 241 | break; |
||
| 242 | default: |
||
| 243 | $error = 'Unable to parse.'; |
||
| 244 | } |
||
| 245 | |||
| 246 | throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($isRef) { |
||
| 250 | $this->refs[$isRef] = end($data); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | if (isset($mbEncoding)) { |
||
| 255 | mb_internal_encoding($mbEncoding); |
||
| 256 | } |
||
| 257 | |||
| 258 | return empty($data) ? null : $data; |
||
| 259 | } |
||
| 260 | |||
| 651 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: