| Conditions | 35 |
| Paths | 89 |
| Total Lines | 155 |
| Code Lines | 87 |
| 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 |
||
| 23 | public static function run (AST $tree, Node $parent = null): array |
||
| 24 | { |
||
| 25 | foreach ($tree->getNodes () as $id => $node) |
||
| 26 | { |
||
| 27 | switch ($node->type) |
||
| 28 | { |
||
| 29 | case OBJECT_DEFINITION: |
||
| 30 | $class = $node->args['class']; |
||
| 31 | $name = $node->args['name']; |
||
| 32 | $args = []; |
||
| 33 | |||
| 34 | if (isset (self::$objects[$name])) |
||
| 35 | break; |
||
| 36 | |||
| 37 | if (isset ($node->args['args'])) |
||
| 38 | { |
||
| 39 | $args = $node->args['args']; |
||
| 40 | |||
| 41 | foreach ($args as $arg_id => $arg) |
||
| 42 | $args[$arg_id] = self::formatLine ($arg, self::$objects); |
||
| 43 | } |
||
| 44 | |||
| 45 | try |
||
| 46 | { |
||
| 47 | self::$objects[$name] = eval ("namespace VoidEngine; return new $class (". implode (', ', $args) .");"); |
||
|
1 ignored issue
–
show
|
|||
| 48 | |||
| 49 | try |
||
| 50 | { |
||
| 51 | self::$objects[$name]->name = $name; |
||
| 52 | } |
||
| 53 | |||
| 54 | catch (\Throwable $e) {} |
||
|
1 ignored issue
–
show
|
|||
| 55 | } |
||
| 56 | |||
| 57 | catch (\Throwable $e) |
||
| 58 | { |
||
| 59 | if (self::$throw_errors) |
||
| 60 | throw new \Exception ('Interpeter couldn\'t create object "'. $class .'" with name "'. $name .'" at line "'. $node->line .'". Exception info:'. "\n\n". (string) $e, 0, $e); |
||
| 61 | } |
||
| 62 | break; |
||
| 63 | |||
| 64 | case PROPERTY_SET: |
||
| 65 | if ($parent !== null) |
||
| 66 | { |
||
| 67 | $name = $parent->args['name']; |
||
| 68 | |||
| 69 | $propertyName = $node->args['name']; |
||
| 70 | $propertyValue = $node->args['value']; |
||
| 71 | $preset = ''; |
||
| 72 | |||
| 73 | if (preg_match ('/function \((.*)\) use \((.*)\)/', $propertyValue)) |
||
| 74 | { |
||
| 75 | $use = substr ($propertyValue, strpos ($propertyValue, 'use')); |
||
| 76 | $use = $ouse = substr ($use, ($pos = strpos ($use, '(') + 1), strpos ($use, ')') - $pos); |
||
| 77 | $use = explode (' ', $use); |
||
| 78 | |||
| 79 | foreach ($use as $id => $useParam) |
||
| 80 | if (isset (self::$objects[$useParam]) && $use[$id + 1][0] == '$') |
||
| 81 | { |
||
| 82 | $fname = $use[$id + 1]; |
||
| 83 | |||
| 84 | if (substr ($fname, strlen ($fname) - 1) == ',') |
||
| 85 | $fname = substr ($fname, 0, -1); |
||
| 86 | |||
| 87 | $preset .= "$fname = $useParam; "; |
||
| 88 | |||
| 89 | unset ($use[$id]); |
||
| 90 | } |
||
| 91 | |||
| 92 | $preset = self::formatLine ($preset, self::$objects); |
||
| 93 | $propertyValue = self::formatLine (str_replace ($ouse, implode (' ', $use), $propertyValue), self::$objects); |
||
| 94 | } |
||
| 95 | |||
| 96 | else $propertyValue = self::formatLine ($propertyValue, self::$objects); |
||
| 97 | |||
| 98 | try |
||
| 99 | { |
||
| 100 | self::$objects[$name]->$propertyName = eval ("namespace VoidEngine; $preset return $propertyValue;"); |
||
|
1 ignored issue
–
show
|
|||
| 101 | } |
||
| 102 | |||
| 103 | catch (\Throwable $e) |
||
| 104 | { |
||
| 105 | if (self::$throw_errors) |
||
| 106 | throw new \Exception ('Interpeter couldn\'t set property "'. $propertyName .'" with value "'. $propertyValue .'" at line "'. $node->line .'". Exception info:'. "\n\n". (string) $e, 0, $e); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | elseif (self::$throw_errors) |
||
| 111 | throw new \Exception ('Setting property to an non-object at line "'. $node->line); |
||
| 112 | break; |
||
| 113 | |||
| 114 | case METHOD_CALL: |
||
| 115 | if ($parent !== null) |
||
| 116 | { |
||
| 117 | $name = $parent->args['name']; |
||
| 118 | |||
| 119 | $methodName = $node->args['name']; |
||
| 120 | $methodArgs = $node->args['args']; |
||
| 121 | |||
| 122 | foreach ($methodArgs as $arg_id => $arg) |
||
| 123 | $methodArgs[$arg_id] = self::formatLine ($arg, self::$objects); |
||
| 124 | |||
| 125 | try |
||
| 126 | { |
||
| 127 | if (strpos ($methodName, '->') !== false && self::$allow_multimethods_calls) |
||
| 128 | eval ('namespace VoidEngine; _c('. self::$objects[$name]->selector .')->'. $methodName .' ('. implode (', ', $methodArgs) .');'); |
||
|
1 ignored issue
–
show
|
|||
| 129 | |||
| 130 | elseif (sizeof ($methodArgs) > 0) |
||
| 131 | self::$objects[$name]->$methodName (...eval ('namespace VoidEngine; return ['. implode (', ', $methodArgs) .'];')); |
||
|
1 ignored issue
–
show
|
|||
| 132 | |||
| 133 | else self::$objects[$name]->$methodName (); |
||
| 134 | } |
||
| 135 | |||
| 136 | catch (\Throwable $e) |
||
| 137 | { |
||
| 138 | if (self::$throw_errors) |
||
| 139 | throw new \Exception ('Interpeter couldn\'t call method "'. $methodName .'" with arguments '. json_encode ($methodArgs) .' at line "'. $node->line .'". Exception info:'. "\n\n". (string) $e, 0, $e); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | elseif (self::$throw_errors) |
||
| 144 | throw new \Exception ('Calling method to an non-object at line "'. $node->line .'"'); |
||
| 145 | break; |
||
| 146 | |||
| 147 | case STYLES_IMPORTING: |
||
| 148 | foreach ($node->args['imports'] as $style) |
||
| 149 | { |
||
| 150 | $path = eval ('namespace VoidEngine; return '. self::formatLine ($style, self::$objects) .';'); |
||
|
1 ignored issue
–
show
|
|||
| 151 | |||
| 152 | if (!file_exists ($path)) |
||
| 153 | throw new \Exception ('Trying to import nonexistent style at line "'. $node->line .'"'); |
||
| 154 | |||
| 155 | \VLF\VST\Interpreter::run (\VLF\VST\Parser::parse (file_get_contents ($path))); |
||
| 156 | } |
||
| 157 | break; |
||
| 158 | |||
| 159 | case RUNTIME_EXECUTION: |
||
| 160 | eval (self::formatLine ($node->args['code'], self::$objects)); |
||
|
1 ignored issue
–
show
|
|||
| 161 | break; |
||
| 162 | } |
||
| 163 | |||
| 164 | $nodes = $node->getNodes (); |
||
| 165 | |||
| 166 | if (isset ($node->args['styles'])) |
||
| 167 | foreach ($node->args['styles'] as $style) |
||
| 168 | if (isset (\VLF\VST\Interpreter::$styles[$style])) |
||
| 169 | $nodes = array_merge ($nodes, \VLF\VST\Interpreter::$styles[$style]); |
||
| 170 | |||
| 171 | else throw new \Exception ('Trying to set undefined style to object at line "'. $node->line .'"'); |
||
| 172 | |||
| 173 | self::$objects = self::run (new AST (array_map ( |
||
| 174 | fn ($node) => $node->export (), $nodes)), $node); |
||
| 175 | } |
||
| 176 | |||
| 177 | return self::$objects; |
||
| 178 | } |
||
| 242 |