| Conditions | 29 |
| Paths | 813 |
| Total Lines | 73 |
| Code Lines | 54 |
| 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 |
||
| 87 | public function parse($strContent = null) |
||
| 88 | { |
||
| 89 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
||
| 90 | //TODO : be more permissive on $strContent values |
||
| 91 | if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
||
| 92 | $previous = $root = new Node(); |
||
| 93 | $emptyLines = []; |
||
| 94 | try { //var_dump($source); |
||
| 95 | $gen = function () use($source) { |
||
| 96 | foreach ($source as $key => $value) { |
||
| 97 | yield ++$key => $value; |
||
| 98 | } |
||
| 99 | }; |
||
| 100 | foreach ($gen() as $lineNb => $lineString) { |
||
| 101 | $n = new Node($lineString, $lineNb); |
||
| 102 | $deepest = $previous->getDeepestNode(); |
||
| 103 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT|Y::TAG) || $deepest->type & Y::PARTIAL) { |
||
| 104 | if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
||
| 105 | } |
||
| 106 | //Note: 6.6 comments: Note that outside scalar content, a line containing only white space characters is taken to be a comment line. |
||
| 107 | foreach ($emptyLines as $blankNode) { |
||
| 108 | if ($blankNode !== $previous) { |
||
| 109 | $blankNode->getParent()->add($blankNode); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $emptyLines = []; |
||
| 113 | switch ($n->indent <=> $previous->indent) { |
||
| 114 | case -1: $target = $previous->getParent($n->indent); |
||
| 115 | if ($n->type & Y::ITEM) { |
||
| 116 | $target = $previous->getParent($n->indent, Y::KEY); |
||
| 117 | } |
||
| 118 | break; |
||
| 119 | case 0: |
||
| 120 | if ($n->type & Y::KEY && $n->indent === 0) { |
||
| 121 | $target = $root; |
||
| 122 | } elseif($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
||
| 123 | $target = $deepest; |
||
| 124 | } else { |
||
| 125 | $target = $previous->getParent(); |
||
| 126 | } |
||
| 127 | break; |
||
| 128 | default: |
||
| 129 | $target = $previous; |
||
| 130 | if ($previous->type & Y::ITEM) { |
||
| 131 | if (($deepest->type & Y::KEY && is_null($deepest->value)) || |
||
| 132 | ($deepest->type & Y::TAG && is_null($deepest->value)) |
||
| 133 | ) { |
||
| 134 | $target = $deepest; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | if ($this->onContextType($n, $target, $lineString)) continue; |
||
| 139 | $target->add($n); |
||
| 140 | $previous = $n; |
||
| 141 | } |
||
| 142 | if ($this->debug === 2) { |
||
| 143 | print_r((new \ReflectionClass(Y::class))->getConstants()); |
||
| 144 | echo "\033[33mParsed Structure\033[0m\n"; |
||
| 145 | print_r($root); |
||
| 146 | exit(); |
||
| 147 | } |
||
| 148 | $out = Builder::buildContent($root, $this->debug); |
||
| 149 | return $out; |
||
| 150 | } catch (\Error|\Exception|\ParseError $e) { |
||
| 151 | $file = $this->filePath ? basename($this->filePath) : 'YAML STRING'; |
||
| 152 | $message = basename($e->getFile())."@".$e->getLine().": ".$e->getMessage()." for '$file' @".($lineNb)."\n"; |
||
| 153 | if ($this->options & self::NO_PARSING_EXCEPTIONS) { |
||
| 154 | // trigger_error($message, E_USER_WARNING); |
||
| 155 | self::$error = $message; |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | $this->debug && print_r($root); |
||
| 159 | throw new \Exception($message, 3); |
||
| 160 | } |
||
| 216 |