| Conditions | 16 |
| Paths | 244 |
| Total Lines | 54 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 66 | public function parse($strContent = null) |
||
| 67 | { |
||
| 68 | $source = $this->content; |
||
| 69 | if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
||
|
1 ignored issue
–
show
|
|||
| 70 | //TODO : be more permissive on $strContent values |
||
| 71 | if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
||
|
1 ignored issue
–
show
|
|||
| 72 | $previous = $root = new Node(); |
||
| 73 | $emptyLines = []; |
||
| 74 | $specialTypes = [T::LITTERAL, T::LITTERAL_FOLDED, T::EMPTY]; |
||
| 75 | try { |
||
| 76 | foreach ($source as $lineNb => $lineString) { |
||
| 77 | $n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
||
| 78 | $parent = $previous; |
||
| 79 | $deepest = $previous->getDeepestNode(); |
||
| 80 | if ($deepest->type === T::PARTIAL) { |
||
| 81 | //TODO:verify this edge case |
||
| 82 | // if ($n->type === T::KEY && $n->indent === $previous->indent) { |
||
| 83 | // throw new \ParseError(sprintf(self::INVALID_VALUE, $lineNb), 1); |
||
| 84 | // } |
||
| 85 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
||
| 86 | } else { |
||
| 87 | if (in_array($n->type, $specialTypes)) { |
||
| 88 | if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) continue; |
||
|
1 ignored issue
–
show
|
|||
| 89 | } |
||
| 90 | foreach ($emptyLines as $key => $node) { |
||
| 91 | $node->getParent()->add($node); |
||
| 92 | } |
||
| 93 | $emptyLines = []; |
||
| 94 | if ($n->indent < $previous->indent) { |
||
| 95 | $parent = $previous->getParent($n->indent); |
||
| 96 | } elseif ($n->indent === $previous->indent) { |
||
| 97 | $parent = $previous->getParent(); |
||
| 98 | } elseif ($n->indent > $previous->indent) { |
||
| 99 | if ($this->onDeepestType($n, $parent, $previous, $lineString)) continue; |
||
|
1 ignored issue
–
show
|
|||
| 100 | } |
||
| 101 | $parent->add($n); |
||
| 102 | $previous = $n; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | if ($this->debug === 2) { |
||
| 106 | var_dump("\033[33mParsed Structure\033[0m\n", $root); |
||
| 107 | exit(0); |
||
| 108 | } |
||
| 109 | $out = Builder::buildContent($root, $this->debug); |
||
| 110 | return $out; |
||
| 111 | } catch (\ParseError $pe) { |
||
| 112 | $message = $pe->getMessage()." on line ".$pe->getLine(); |
||
| 113 | if ($this->options & self::EXCEPTIONS_PARSING) { |
||
| 114 | var_dump($root); |
||
| 115 | throw new \Exception($message, 1); |
||
| 116 | } |
||
| 117 | $this->errors[] = $message; |
||
| 118 | } catch (\Error|\Exception $e) { |
||
| 119 | throw new \Exception($e->getMessage()." for '$this->filePath'", 1); |
||
| 120 | } |
||
| 171 |