| Conditions | 36 |
| Paths | 460 |
| Total Lines | 256 |
| Code Lines | 127 |
| 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 |
||
| 20 | public static function parse (string $vlf): AST |
||
| 21 | { |
||
| 22 | $tree = new AST; |
||
| 23 | $objects = new Stack; |
||
| 24 | |||
| 25 | if (file_exists ($vlf)) |
||
| 26 | $vlf = file_get_contents ($vlf); |
||
| 27 | |||
| 28 | $lines = explode (self::$divider, $vlf); |
||
| 29 | $skip_at = -1; |
||
| 30 | |||
| 31 | foreach ($lines as $line_num => $line) |
||
| 32 | { |
||
| 33 | // \VoidEngine\pre ($line_num .', '. ($skip_at > $line_num ? 'skip' : 'not skip') .': '. $line); |
||
| 34 | |||
| 35 | if ($skip_at > $line_num || !self::filter ($line)) |
||
| 36 | continue; |
||
| 37 | |||
| 38 | $height = self::getHeight ($line); |
||
| 39 | $words = array_filter (explode (' ', $line), 'VLF\Parser::filter'); |
||
| 40 | $poped = false; |
||
| 41 | |||
| 42 | # Очищаем стек объектов |
||
| 43 | while ($objects->size () > 0) |
||
| 44 | if ($objects->current ()->height >= $height) |
||
| 45 | { |
||
| 46 | $objects->pop (); |
||
| 47 | |||
| 48 | $poped = true; |
||
| 49 | } |
||
| 50 | |||
| 51 | else break; |
||
| 52 | |||
| 53 | # Создаём новую ссылку на объект |
||
| 54 | if ($poped && $objects->size () > 0) |
||
| 55 | { |
||
| 56 | $object = $objects->pop (); |
||
| 57 | |||
| 58 | $objects->push (new Node (array_merge ($object->export (), ['nodes' => []]))); |
||
| 59 | $tree->push ($objects->current ()); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Импорт таблиц стилей |
||
| 64 | */ |
||
| 65 | if ($words[0] == 'import') |
||
| 66 | { |
||
| 67 | $imports = substr ($line, strlen ($words[0])); |
||
| 68 | $parsed = self::parseSubtext ($lines, $line_num, $height); |
||
| 69 | |||
| 70 | $imports .= $parsed[0]; |
||
| 71 | $skip_at = $parsed[1]; |
||
| 72 | |||
| 73 | $imports = self::filter ($imports) ? |
||
| 74 | array_map ('trim', self::parseArguments ($imports)) : []; |
||
| 75 | |||
| 76 | $tree->push (new Node ([ |
||
| 77 | 'type' => STYLES_IMPORTING, |
||
| 78 | 'line' => $line, |
||
| 79 | 'words' => $words, |
||
| 80 | 'height' => $height, |
||
| 81 | |||
| 82 | 'args' => [ |
||
| 83 | 'imports' => $imports |
||
| 84 | ] |
||
| 85 | ])); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Комментарии |
||
| 90 | */ |
||
| 91 | elseif ($words[0][0] == '#') |
||
| 92 | { |
||
| 93 | /** |
||
| 94 | * Обработка многострочных комментариев |
||
| 95 | */ |
||
| 96 | if (isset ($words[0][1])) |
||
| 97 | { |
||
| 98 | if ($words[0][1] == '^') |
||
| 99 | $skip_at = self::parseSubtext ($lines, $line_num, $height)[1]; |
||
| 100 | |||
| 101 | else throw new \Exception ('Unknown char founded after comment definition at line '. ($line_num + 1)); |
||
| 102 | } |
||
| 103 | |||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Выполнение PHP кода |
||
| 109 | */ |
||
| 110 | elseif ($words[0][0] == '%') |
||
| 111 | { |
||
| 112 | $code = substr ($line, strlen ($words[0])); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Обработка многострочного кода |
||
| 116 | */ |
||
| 117 | if (isset ($words[0][1])) |
||
| 118 | { |
||
| 119 | if ($words[0][1] == '^') |
||
| 120 | { |
||
| 121 | $parsed = self::parseSubtext ($lines, $line_num, $height); |
||
| 122 | |||
| 123 | $code .= $parsed[0]; |
||
| 124 | $skip_at = $parsed[1]; |
||
| 125 | } |
||
| 126 | |||
| 127 | else throw new \Exception ('Unknown char founded after runtime execution definition at line '. ($line_num + 1)); |
||
| 128 | } |
||
| 129 | |||
| 130 | $tree->push (new Node ([ |
||
| 131 | 'type' => RUNTIME_EXECUTION, |
||
| 132 | 'line' => $line, |
||
| 133 | 'words' => $words, |
||
| 134 | 'height' => $height, |
||
| 135 | |||
| 136 | 'args' => [ |
||
| 137 | 'code' => $code |
||
| 138 | ] |
||
| 139 | ])); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Установка свойства |
||
| 144 | */ |
||
| 145 | elseif (($pos = strpos ($line, ':')) !== false) |
||
| 146 | { |
||
| 147 | if ($objects->size () == 0) |
||
| 148 | throw new \Exception ('Trying to set property to unknown object at line '. ($line_num + 1)); |
||
| 149 | |||
| 150 | if (!isset ($words[1])) |
||
| 151 | throw new \Exception ('Trying to set void property value at line '. ($line_num + 1)); |
||
| 152 | |||
| 153 | $propertyName = substr ($line, 0, $pos); |
||
| 154 | $propertyValue = substr ($line, $pos + 1); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Обработка многострочных свойств |
||
| 158 | */ |
||
| 159 | if ($line[$pos + 1] == '^') |
||
| 160 | { |
||
| 161 | $parsed = self::parseSubtext ($lines, $line_num, $height); |
||
| 162 | |||
| 163 | $propertyValue = substr ($propertyValue, 1) . $parsed[0]; |
||
| 164 | $skip_at = $parsed[1]; |
||
| 165 | } |
||
| 166 | |||
| 167 | $objects->current ()->push (new Node ([ |
||
| 168 | 'type' => PROPERTY_SET, |
||
| 169 | 'line' => $line, |
||
| 170 | 'words' => $words, |
||
| 171 | 'height' => $height, |
||
| 172 | |||
| 173 | 'args' => [ |
||
| 174 | 'name' => $propertyName, |
||
| 175 | 'value' => $propertyValue |
||
| 176 | ] |
||
| 177 | ])); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Вызов метода |
||
| 182 | */ |
||
| 183 | elseif (isset ($words[0][1]) && $words[0][0] == '-' && $words[0][1] == '>') |
||
| 184 | { |
||
| 185 | if ($objects->size () == 0) |
||
| 186 | throw new \Exception ('Trying to call method from unknown object at line '. ($line_num + 1)); |
||
| 187 | |||
| 188 | $methodArgs = []; |
||
| 189 | |||
| 190 | if (($pos = strpos ($line, '(')) !== false) |
||
| 191 | { |
||
| 192 | if (($end = strrpos ($line, ')', $pos)) === false) |
||
| 193 | throw new \Exception ('Incorrect method arguments syntax at line '. ($line_num + 1)); |
||
| 194 | |||
| 195 | $methodArgs = substr ($line, $pos + 1, $end - $pos - 1); |
||
| 196 | |||
| 197 | $methodName = trim (substr ($line, 2, $pos - 2)); |
||
| 198 | $methodArgs = self::filter ($methodArgs) ? |
||
| 199 | self::parseArguments ($methodArgs) : []; |
||
| 200 | } |
||
| 201 | |||
| 202 | else $methodName = trim (substr ($line, 2)); |
||
| 203 | |||
| 204 | $objects->current ()->push (new Node ([ |
||
| 205 | 'type' => METHOD_CALL, |
||
| 206 | 'line' => $line, |
||
| 207 | 'words' => $words, |
||
| 208 | 'height' => $height, |
||
| 209 | |||
| 210 | 'args' => [ |
||
| 211 | 'name' => $methodName, |
||
| 212 | 'args' => $methodArgs |
||
| 213 | ] |
||
| 214 | ])); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Объявление объекта |
||
| 219 | */ |
||
| 220 | elseif (sizeof ($words) > 1) |
||
| 221 | { |
||
| 222 | $class = $words[0]; |
||
| 223 | $name = $words[1]; |
||
| 224 | $args = []; |
||
| 225 | $styles = []; |
||
| 226 | |||
| 227 | if ($objects->size () > 0 && $objects->current ()->height < $height) |
||
| 228 | $args[] = $objects->current ()->args['name']; |
||
| 229 | |||
| 230 | if (($pos = strpos ($line, '(')) !== false) |
||
| 231 | { |
||
| 232 | if (($end = strrpos ($line, ')', $pos)) === false) |
||
| 233 | throw new \Exception ('Incorrect class constructor arguments syntax at line '. ($line_num + 1)); |
||
| 234 | |||
| 235 | $args = substr ($line, $pos + 1, $end - $pos - 1); |
||
| 236 | |||
| 237 | $name = substr ($line, $len = strlen ($class), $pos - $len); |
||
| 238 | $args = self::filter ($args) ? |
||
| 239 | self::parseArguments ($args) : []; |
||
| 240 | } |
||
| 241 | |||
| 242 | if (($end = strpos ($line, '>')) !== false) |
||
| 243 | { |
||
| 244 | $styles = trim (substr ($line, $end + 1)); |
||
| 245 | |||
| 246 | if (strlen ($styles) == 0) |
||
| 247 | throw new \Exception ('Trying to set empty style to object'); |
||
| 248 | |||
| 249 | $styles = array_map ('trim', explode (',', $styles)); |
||
| 250 | } |
||
| 251 | |||
| 252 | $objects->push (new Node ([ |
||
| 253 | 'type' => OBJECT_DEFINITION, |
||
| 254 | 'line' => $line, |
||
| 255 | 'words' => $words, |
||
| 256 | 'height' => $height, |
||
| 257 | |||
| 258 | 'args' => [ |
||
| 259 | 'class' => $class, |
||
| 260 | 'name' => trim ($name), |
||
| 261 | 'args' => $args, |
||
| 262 | 'styles' => $styles |
||
| 263 | ] |
||
| 264 | ])); |
||
| 265 | |||
| 266 | $tree->push ($objects->current ()); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Неопознанная структура |
||
| 271 | */ |
||
| 272 | else throw new \Exception ('Unsupported structure founded at line '. ($line_num + 1)); |
||
| 273 | } |
||
| 274 | |||
| 275 | return $tree; |
||
| 276 | } |
||
| 397 |