| Conditions | 16 |
| Paths | 238 |
| 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 |
||
| 74 | public function parse($strContent = null) |
||
| 75 | { |
||
| 76 | $source = $this->content; |
||
| 77 | if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
||
|
1 ignored issue
–
show
|
|||
| 78 | //TODO : be more permissive on $strContent values |
||
| 79 | if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
||
|
1 ignored issue
–
show
|
|||
| 80 | $previous = $root = new Node(); |
||
| 81 | $emptyLines = []; |
||
| 82 | $specialTypes = Y\LITTERALS | Y\BLANK; |
||
|
1 ignored issue
–
show
|
|||
| 83 | try { |
||
| 84 | foreach ($source as $lineNb => $lineString) { |
||
| 85 | $n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
||
| 86 | $parent = $previous; |
||
| 87 | $deepest = $previous->getDeepestNode(); |
||
| 88 | if ($deepest->type & Y\PARTIAL) { |
||
| 89 | //TODO:verify this edge case |
||
| 90 | // if ($n->type === Y\KEY && $n->indent === $previous->indent) { |
||
| 91 | // throw new \ParseError(sprintf(self::INVALID_VALUE, $lineNb), 1); |
||
| 92 | // } |
||
| 93 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
||
| 94 | } else { |
||
| 95 | if ($n->type & $specialTypes) { |
||
| 96 | if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) continue; |
||
|
1 ignored issue
–
show
|
|||
| 97 | } |
||
| 98 | foreach ($emptyLines as $key => $node) { |
||
| 99 | $node->getParent()->add($node); |
||
| 100 | } |
||
| 101 | $emptyLines = []; |
||
| 102 | if ($n->indent < $previous->indent) { |
||
| 103 | $parent = $previous->getParent($n->indent); |
||
| 104 | } elseif ($n->indent === $previous->indent) { |
||
| 105 | $parent = $previous->getParent(); |
||
| 106 | } elseif ($n->indent > $previous->indent) { |
||
| 107 | if ($this->onDeepestType($n, $parent, $previous, $lineString)) continue; |
||
|
1 ignored issue
–
show
|
|||
| 108 | } |
||
| 109 | $parent->add($n); |
||
| 110 | $previous = $n; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | if ($this->debug === 2) { |
||
| 114 | var_dump("\033[33mParsed Structure\033[0m\n", $root); |
||
| 115 | die("Debug of root structure requested (remove debug level to suppress these)"); |
||
| 116 | } |
||
| 117 | $out = Builder::buildContent($root, $this->debug);//var_dump($out);exit(); |
||
| 118 | return $out; |
||
| 119 | } catch (\ParseError $pe) { |
||
| 120 | $message = $pe->getMessage()." on line ".$pe->getLine()." for '$this->filePath'\n"; |
||
| 121 | if ($this->options & self::EXCEPTIONS_PARSING) { |
||
| 122 | var_dump($root); |
||
| 123 | throw new \Exception($message, 1); |
||
| 124 | } |
||
| 125 | $this->errors[] = $message; |
||
| 126 | } catch (\Error|\Exception $e) { |
||
| 127 | throw new \Exception(basename($e->getFile())."(".$e->getLine()."):".$e->getMessage()." for '$this->filePath'\n", 3); |
||
| 128 | } |
||
| 173 |