| Total Lines | 83 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 22 | protected function readRaw(string $path) |
||
| 23 | { |
||
| 24 | $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
||
| 25 | $saves = []; |
||
| 26 | try { |
||
| 27 | $names = []; |
||
|
|
|||
| 28 | $ast = $parser->parse(file_get_contents($path)); |
||
| 29 | |||
| 30 | $traverser = new NodeTraverser; |
||
| 31 | $visitor = new class extends NodeVisitorAbstract { |
||
| 32 | public static $names; |
||
| 33 | private $stack; |
||
| 34 | |||
| 35 | public function beginTraverse(array $nodes) { |
||
| 36 | $this->stack = []; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function enterNode(Node $node) { |
||
| 40 | $this->stack[] = $node; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function leaveNode(Node $node) |
||
| 44 | { |
||
| 45 | $this->processNode($node); |
||
| 46 | array_pop($this->stack); |
||
| 47 | |||
| 48 | return $node; |
||
| 49 | } |
||
| 50 | |||
| 51 | private function processNode(Node $node) |
||
| 52 | { |
||
| 53 | if (! $node instanceof ArrayDimFetch) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | if (! $node->var instanceof Variable) { |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | if (! $node->var->name === '_ENV') { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | if (! $node->dim instanceof String_) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | if ($this->inDefine($node)) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | $name = $node->dim->value; |
||
| 69 | self::$names[$name] = $name; |
||
| 70 | } |
||
| 71 | |||
| 72 | private function inDefine(): bool |
||
| 73 | { |
||
| 74 | foreach ($this->stack as $node) { |
||
| 75 | if ($node instanceof FuncCall) { |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | return false; |
||
| 81 | } |
||
| 82 | }; |
||
| 83 | $traverser->addVisitor($visitor); |
||
| 84 | $ast = $traverser->traverse($ast); |
||
| 85 | foreach ($visitor::$names as $name) { |
||
| 86 | $saves[$name] = $_ENV[$name]; |
||
| 87 | $_ENV[$name] = Env::get($name); |
||
| 88 | } |
||
| 89 | } catch (\Exception $e) { |
||
| 90 | // do nothing |
||
| 91 | } |
||
| 92 | |||
| 93 | $params = $this->builder->getVars()['params'] ?? []; |
||
| 94 | |||
| 95 | $result = static function (array $params) { |
||
| 96 | return require func_get_arg(1); |
||
| 97 | }; |
||
| 98 | |||
| 99 | $res = $result($params, $path); |
||
| 100 | foreach ($saves as $key => $value) { |
||
| 101 | $_ENV[$key] = $value; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $res; |
||
| 105 | } |
||
| 107 |