| Conditions | 3 |
| Paths | 5 |
| Total Lines | 65 |
| Code Lines | 40 |
| 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 |
||
| 63 | public function run($files) |
||
| 64 | { |
||
| 65 | // config |
||
| 66 | ini_set('xdebug.max_nesting_level', 3000); |
||
| 67 | |||
| 68 | $metrics = new Metrics(); |
||
| 69 | |||
| 70 | // traverse all |
||
| 71 | $whenToStop = function() { |
||
| 72 | return true; |
||
| 73 | }; |
||
| 74 | |||
| 75 | // prepare parser |
||
| 76 | $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
||
| 77 | $traverser = new NodeTraverser(false, $whenToStop); |
||
| 78 | $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver()); |
||
| 79 | $traverser->addVisitor(new ClassEnumVisitor($metrics)); |
||
| 80 | $traverser->addVisitor(new CyclomaticComplexityVisitor($metrics)); |
||
| 81 | $traverser->addVisitor(new ExternalsVisitor($metrics)); |
||
| 82 | $traverser->addVisitor(new LcomVisitor($metrics)); |
||
| 83 | $traverser->addVisitor(new HalsteadVisitor($metrics)); |
||
| 84 | $traverser->addVisitor(new LengthVisitor($metrics)); |
||
| 85 | $traverser->addVisitor(new CyclomaticComplexityVisitor($metrics)); |
||
| 86 | $traverser->addVisitor(new MaintainabilityIndexVisitor($metrics)); |
||
| 87 | $traverser->addVisitor(new KanDefectVisitor($metrics)); |
||
| 88 | $traverser->addVisitor(new SystemComplexityVisitor($metrics)); |
||
| 89 | |||
| 90 | // create a new progress bar (50 units) |
||
| 91 | $progress = new ProgressBar($this->output, sizeof($files)); |
||
| 92 | $this->output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
||
| 93 | $progress->start(); |
||
| 94 | |||
| 95 | foreach ($files as $file) { |
||
| 96 | $progress->advance(); |
||
| 97 | $code = file_get_contents($file); |
||
| 98 | $this->issuer->set('filename', $file); |
||
| 99 | try { |
||
| 100 | $stmts = $parser->parse($code); |
||
| 101 | $this->issuer->set('statements', $stmts); |
||
| 102 | $traverser->traverse($stmts); |
||
|
|
|||
| 103 | } catch (Error $e) { |
||
| 104 | $this->output->writeln(sprintf('<error>Cannot parse %s</error>', $file)); |
||
| 105 | } |
||
| 106 | $this->issuer->clear('filename'); |
||
| 107 | $this->issuer->clear('statements'); |
||
| 108 | } |
||
| 109 | |||
| 110 | // |
||
| 111 | // System analyses |
||
| 112 | (new PageRank())->calculate($metrics); |
||
| 113 | (new Coupling())->calculate($metrics); |
||
| 114 | |||
| 115 | // |
||
| 116 | // File analyses |
||
| 117 | (new GitChanges($this->config, $files))->calculate($metrics); |
||
| 118 | |||
| 119 | // |
||
| 120 | // Unit test |
||
| 121 | (new UnitTesting($this->config, $files))->calculate($metrics); |
||
| 122 | |||
| 123 | $progress->finish(); |
||
| 124 | $progress->clear(); |
||
| 125 | |||
| 126 | return $metrics; |
||
| 127 | } |
||
| 128 | } |
||
| 129 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.