Conditions | 16 |
Paths | 94 |
Total Lines | 54 |
Code Lines | 39 |
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 | $deepest = $previous->getDeepestNode(); |
||
87 | if ($deepest->type & Y\PARTIAL) { |
||
88 | //TODO:verify this edge case |
||
89 | // if ($n->type === Y\KEY && $n->indent === $previous->indent) { |
||
90 | // throw new \ParseError(sprintf(self::INVALID_VALUE, $lineNb), 1); |
||
91 | // } |
||
92 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
||
93 | } else { |
||
94 | if (($n->type & $specialTypes) && $this->onSpecialType($n, $previous, $emptyLines)) { |
||
95 | continue; |
||
96 | } |
||
97 | if ($n->type & Y\SCALAR) { |
||
98 | foreach ($emptyLines as $blankNode) { |
||
99 | $blankNode->getParent()->add($blankNode); |
||
100 | } |
||
101 | } |
||
102 | $emptyLines = []; |
||
103 | switch ($n->indent <=> $previous->indent) { |
||
104 | case -1: $previous->getParent($n->indent)->add($n);break; |
||
1 ignored issue
–
show
|
|||
105 | case 0: $previous->getParent()->add($n);break; |
||
1 ignored issue
–
show
|
|||
106 | default: |
||
1 ignored issue
–
show
|
|||
107 | if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
||
1 ignored issue
–
show
|
|||
108 | $previous->add($n); |
||
109 | } |
||
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 (\Error|\Exception|\ParseError $e) { |
||
120 | $file = basename($this->filePath); |
||
121 | $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb+1)."\n"; |
||
122 | if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
||
123 | trigger_error($message, E_USER_WARNING); |
||
124 | $this->errors[] = $message; |
||
125 | var_dump($root); |
||
126 | } |
||
127 | throw new \Exception($message, 3); |
||
128 | } |
||
177 |