| Conditions | 58 |
| Paths | 653 |
| Total Lines | 250 |
| Code Lines | 171 |
| Lines | 10 |
| Ratio | 4 % |
| Changes | 2 | ||
| 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 |
||
| 82 | public function parse($value) |
||
| 83 | { |
||
| 84 | $this->currentLineNb = -1; |
||
| 85 | $this->currentLine = ""; |
||
| 86 | $value = $this->cleanup($value); |
||
| 87 | $this->lines = explode("\n", $value); |
||
| 88 | |||
| 89 | $data = []; |
||
| 90 | $context = null; |
||
| 91 | while ($this->moveToNextLine()) { |
||
| 92 | if ($this->isCurrentLineEmpty()) { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | // tab? |
||
| 97 | if ($this->currentLine[0] === "\t") { |
||
| 98 | throw new ParseException( |
||
| 99 | "A YAML file cannot contain tabs as indentation.", |
||
| 100 | $this->getRealCurrentLineNb() + 1, |
||
| 101 | $this->currentLine |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $isRef = false; |
||
| 106 | $isInPlace = false; |
||
| 107 | $isProcessed = false; |
||
| 108 | if (preg_match("#^\\-((?P<leadspaces>\\s+)(?P<value>.+?))?\\s*$#u", $this->currentLine, $values)) { |
||
| 109 | if ($context && $context === "mapping") { |
||
| 110 | throw new ParseException("You cannot define a sequence item when in a mapping"); |
||
| 111 | } |
||
| 112 | $context = "sequence"; |
||
| 113 | |||
| 114 | View Code Duplication | if (isset($values["value"]) && |
|
| 115 | preg_match("#^&(?P<ref>[^ ]+) *(?P<value>.*)#u", $values["value"], $matches)) { |
||
| 116 | $isRef = $matches["ref"]; |
||
| 117 | $values["value"] = $matches["value"]; |
||
| 118 | } |
||
| 119 | |||
| 120 | // array |
||
| 121 | if (!isset($values["value"]) || trim($values["value"], " ") === "" || |
||
| 122 | strpos(ltrim($values["value"], " "), "#") === 0) { |
||
| 123 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 124 | $parser = new static($c); |
||
| 125 | $parser->refs =& $this->refs; |
||
| 126 | $data[] = $parser->parse($this->getNextEmbedBlock(null, true)); |
||
| 127 | } else { |
||
| 128 | if (isset($values["leadspaces"]) |
||
| 129 | && preg_match( |
||
| 130 | "#^(?P<key>" . |
||
| 131 | Inline::REGEX_QUOTED_STRING . |
||
| 132 | "|[^ '\"\\{\\[].*?) *\\:(\\s+(?P<value>.+?))?\\s*$#u", |
||
| 133 | $values["value"], |
||
| 134 | $matches |
||
| 135 | ) |
||
| 136 | ) { |
||
| 137 | // this is a compact notation element, add to next block and parse |
||
| 138 | $c = $this->getRealCurrentLineNb(); |
||
| 139 | $parser = new static($c); |
||
| 140 | $parser->refs =& $this->refs; |
||
| 141 | |||
| 142 | $block = $values["value"]; |
||
| 143 | if ($this->isNextLineIndented()) { |
||
| 144 | $block .= "\n" . $this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values["leadspaces"]) + 1); |
||
| 145 | } |
||
| 146 | |||
| 147 | $data[] = $parser->parse($block); |
||
| 148 | } else { |
||
| 149 | $data[] = $this->parseValue($values["value"], $context); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($isRef) { |
||
| 154 | $this->refs[$isRef] = end($data); |
||
| 155 | } |
||
| 156 | } elseif (preg_match( |
||
| 157 | "#^(?P<key>" . |
||
| 158 | Inline::REGEX_QUOTED_STRING . |
||
| 159 | "|[^ '\"\\[\\{].*?) *\\:(\\s+(?P<value>.+?))?\\s*$#u", |
||
| 160 | $this->currentLine, |
||
| 161 | $values |
||
| 162 | ) && (strpos($values["key"], " #") === false || in_array($values["key"][0], ["\"", "'"]))) { |
||
| 163 | if ($context && $context === "sequence") { |
||
| 164 | throw new ParseException("You cannot define a mapping item when in a sequence"); |
||
| 165 | } |
||
| 166 | $context = "mapping"; |
||
| 167 | |||
| 168 | // force correct settings |
||
| 169 | Inline::parse(null, $this->refs); |
||
| 170 | try { |
||
| 171 | $key = Inline::parseScalar($values["key"]); |
||
| 172 | } catch (ParseException $e) { |
||
| 173 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 174 | $e->setSnippet($this->currentLine); |
||
| 175 | |||
| 176 | throw $e; |
||
| 177 | } |
||
| 178 | |||
| 179 | // Convert float keys to strings, to avoid being converted to integers by PHP |
||
| 180 | if (is_float($key)) { |
||
| 181 | $key = (string)$key; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($key === "<<") { |
||
| 185 | if (isset($values["value"]) && strpos($values["value"], "*") === 0) { |
||
| 186 | $isInPlace = substr($values["value"], 1); |
||
| 187 | if (!array_key_exists($isInPlace, $this->refs)) { |
||
| 188 | throw new ParseException( |
||
| 189 | sprintf( |
||
| 190 | "Reference \"%s\" does not exist.", |
||
| 191 | $isInPlace |
||
| 192 | ), |
||
| 193 | $this->getRealCurrentLineNb() + 1, |
||
| 194 | $this->currentLine |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | if (isset($values["value"]) && $values["value"] !== "") { |
||
| 199 | $value = $values["value"]; |
||
| 200 | } else { |
||
| 201 | $value = $this->getNextEmbedBlock(); |
||
| 202 | } |
||
| 203 | |||
| 204 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 205 | $parser = new static($c); |
||
| 206 | $parser->refs =& $this->refs; |
||
| 207 | $parsed = $parser->parse($value); |
||
| 208 | |||
| 209 | $merged = []; |
||
| 210 | if (!is_array($parsed)) { |
||
| 211 | throw new ParseException( |
||
| 212 | "YAML merge keys used with a scalar value instead of an array.", |
||
| 213 | $this->getRealCurrentLineNb() + 1, |
||
| 214 | $this->currentLine |
||
| 215 | ); |
||
| 216 | } elseif (isset($parsed[0])) { |
||
| 217 | // Numeric array, merge individual elements |
||
| 218 | foreach (array_reverse($parsed) as $parsedItem) { |
||
| 219 | if (!is_array($parsedItem)) { |
||
| 220 | throw new ParseException( |
||
| 221 | "Merge items must be arrays.", |
||
| 222 | $this->getRealCurrentLineNb() + 1, |
||
| 223 | $parsedItem |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | $merged = array_merge($parsedItem, $merged); |
||
| 227 | } |
||
| 228 | } else { |
||
| 229 | // Associative array, merge |
||
| 230 | $merged = array_merge($merged, $parsed); |
||
| 231 | } |
||
| 232 | |||
| 233 | $isProcessed = $merged; |
||
| 234 | } |
||
| 235 | View Code Duplication | } elseif (isset($values["value"]) && |
|
| 236 | preg_match("#^&(?P<ref>[^ ]+) *(?P<value>.*)#u", $values["value"], $matches)) { |
||
| 237 | $isRef = $matches["ref"]; |
||
| 238 | $values["value"] = $matches["value"]; |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($isProcessed) { |
||
| 242 | // Merge keys |
||
| 243 | $data = $isProcessed; |
||
| 244 | } elseif (!isset($values["value"]) || |
||
| 245 | trim($values["value"], " ") === "" || |
||
| 246 | strpos(ltrim($values["value"], " "), "#") === 0) { |
||
| 247 | // if next line is less indented or equal, then it means that the current value is null |
||
| 248 | if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { |
||
| 249 | $data[$key] = null; |
||
| 250 | } else { |
||
| 251 | $c = $this->getRealCurrentLineNb() + 1; |
||
| 252 | $parser = new static($c); |
||
| 253 | $parser->refs =& $this->refs; |
||
| 254 | $data[$key] = $parser->parse($this->getNextEmbedBlock()); |
||
| 255 | } |
||
| 256 | } else { |
||
| 257 | if ($isInPlace) { |
||
| 258 | $data = $this->refs[$isInPlace]; |
||
| 259 | } else { |
||
| 260 | $data[$key] = $this->parseValue($values["value"], $context); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($isRef) { |
||
| 265 | $this->refs[$isRef] = $data[$key]; |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | // multiple documents are not supported |
||
| 269 | if ($this->currentLine === "---") { |
||
| 270 | throw new ParseException("Multiple documents are not supported."); |
||
| 271 | } |
||
| 272 | |||
| 273 | // 1-liner optionally followed by newline(s) |
||
| 274 | if (is_string($value) && $this->lines[0] === trim($value)) { |
||
| 275 | try { |
||
| 276 | $value = Inline::parse($this->lines[0], $this->refs); |
||
| 277 | } catch (ParseException $e) { |
||
| 278 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 279 | $e->setSnippet($this->currentLine); |
||
| 280 | |||
| 281 | throw $e; |
||
| 282 | } |
||
| 283 | |||
| 284 | if (is_array($value)) { |
||
| 285 | $first = reset($value); |
||
| 286 | if (is_string($first) && strpos($first, "*") === 0) { |
||
| 287 | $data = []; |
||
| 288 | foreach ($value as $alias) { |
||
| 289 | $data[] = $this->refs[substr($alias, 1)]; |
||
| 290 | } |
||
| 291 | $value = $data; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return $value; |
||
| 296 | } |
||
| 297 | |||
| 298 | switch (preg_last_error()) { |
||
| 299 | case PREG_INTERNAL_ERROR: |
||
| 300 | $error = "Internal PCRE error."; |
||
| 301 | break; |
||
| 302 | case PREG_BACKTRACK_LIMIT_ERROR: |
||
| 303 | $error = "pcre.backtrack_limit reached."; |
||
| 304 | break; |
||
| 305 | case PREG_RECURSION_LIMIT_ERROR: |
||
| 306 | $error = "pcre.recursion_limit reached."; |
||
| 307 | break; |
||
| 308 | case PREG_BAD_UTF8_ERROR: |
||
| 309 | $error = "Malformed UTF-8 data."; |
||
| 310 | break; |
||
| 311 | case PREG_BAD_UTF8_OFFSET_ERROR: |
||
| 312 | $error = "Offset doesn't correspond to the begin of a valid UTF-8 code point."; |
||
| 313 | break; |
||
| 314 | default: |
||
| 315 | $error = "Unable to parse."; |
||
| 316 | } |
||
| 317 | |||
| 318 | throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 319 | } |
||
| 320 | |||
| 321 | if ($isRef) { |
||
| 322 | $this->refs[$isRef] = end($data); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | if (count($data) === 0) { |
||
| 327 | return null; |
||
| 328 | } |
||
| 329 | |||
| 330 | return $data; |
||
| 331 | } |
||
| 332 | |||
| 787 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.