| Conditions | 15 |
| Paths | 65 |
| Total Lines | 47 |
| Code Lines | 37 |
| 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 |
||
| 86 | public function parse($strContent = null) |
||
| 87 | { |
||
| 88 | $source = $this->content ?? preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
||
| 89 | //TODO : be more permissive on $strContent values |
||
| 90 | if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
||
| 91 | $previous = $root = new Node(); |
||
| 92 | $emptyLines = []; |
||
| 93 | try { |
||
| 94 | $gen = function() use($source) { |
||
| 95 | foreach ($source as $key => $value) { |
||
| 96 | yield ++$key => $value; |
||
| 97 | } |
||
| 98 | }; |
||
| 99 | foreach ($gen() as $lineNb => $lineString) { |
||
| 100 | $n = new Node($lineString, $lineNb); |
||
| 101 | if ($n->type & (Y::LITTERALS|Y::BLANK)) { |
||
| 102 | if ($this->onSpecialType($n, $previous, $emptyLines)) continue; |
||
| 103 | } else { |
||
| 104 | foreach ($emptyLines as $blankNode) { |
||
| 105 | $blankNode->getParent()->add($blankNode); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | $emptyLines = []; |
||
| 109 | switch ($n->indent <=> $previous->indent) { |
||
| 110 | case -1: $target = $previous->getParent($n->indent); |
||
|
1 ignored issue
–
show
|
|||
| 111 | break; |
||
| 112 | case 0: $target = $previous->getParent(); |
||
|
1 ignored issue
–
show
|
|||
| 113 | break; |
||
| 114 | default: $target = $previous; |
||
|
1 ignored issue
–
show
|
|||
| 115 | } |
||
| 116 | if ($this->onContextType($n, $target, $lineString)) continue; |
||
| 117 | $target->add($n); |
||
| 118 | $previous = $n; |
||
| 119 | } |
||
| 120 | if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
||
| 121 | $out = Builder::buildContent($root, $this->debug); |
||
| 122 | return $out; |
||
| 123 | } catch (\Error|\Exception|\ParseError $e) { |
||
| 124 | $file = basename($this->filePath); |
||
| 125 | $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb)."\n"; |
||
| 126 | if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
||
| 127 | trigger_error($message, E_USER_WARNING); |
||
| 128 | $this->error = $message; |
||
| 129 | return null; |
||
| 130 | } |
||
| 131 | var_dump($root); |
||
| 132 | throw new \Exception($message, 3); |
||
| 133 | } |
||
| 169 |