| Conditions | 14 |
| Paths | 90 |
| Total Lines | 99 |
| Code Lines | 56 |
| 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 |
||
| 40 | public function calculate(Metrics $metrics) |
||
| 41 | { |
||
| 42 | |||
| 43 | if (!$this->config->has('junit')) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | // parse junit file |
||
| 48 | $filename = $this->config->get('junit'); |
||
|
|
|||
| 49 | if (!file_exists($filename) || !is_readable($filename)) { |
||
| 50 | throw new ConfigException('JUnit report cannot be read'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $unitsTests = []; |
||
| 54 | $infoAboutTests = []; |
||
| 55 | $projectMetric = new ProjectMetric('unitTesting'); |
||
| 56 | |||
| 57 | // injects default value for each metric |
||
| 58 | foreach ($metrics->all() as $metric) { |
||
| 59 | $metric->set('numberOfUnitTests', 0); |
||
| 60 | } |
||
| 61 | |||
| 62 | // parsing of XML file without any dependency to DomDocument or simpleXML |
||
| 63 | // we want to be compatible with every platforms |
||
| 64 | $xml = file_get_contents($filename); |
||
| 65 | if (preg_match_all('!<testsuite name="(.*?)" file="(.*?)"!i', $xml, $matches, PREG_SET_ORDER)) { |
||
| 66 | foreach ($matches as $m) { |
||
| 67 | list(, $classname, $fileOfUnitTest) = $m; |
||
| 68 | $unitsTests[$fileOfUnitTest] = $classname; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // analyze each unit test |
||
| 73 | // This code is slow and can be optimized |
||
| 74 | foreach ($unitsTests as $filename => $classname) { |
||
| 75 | $metricsOfUnitTest = new Metrics(); |
||
| 76 | $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
||
| 77 | $traverser = new \PhpParser\NodeTraverser(); |
||
| 78 | $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver()); |
||
| 79 | $traverser->addVisitor(new ClassEnumVisitor($metricsOfUnitTest)); |
||
| 80 | $traverser->addVisitor(new ExternalsVisitor($metricsOfUnitTest)); |
||
| 81 | |||
| 82 | $code = file_get_contents($filename); |
||
| 83 | $stmts = $parser->parse($code); |
||
| 84 | $traverser->traverse($stmts); |
||
| 85 | |||
| 86 | if (!$metricsOfUnitTest->has($classname)) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | // list of externals sources of unit test |
||
| 91 | $metric = $metricsOfUnitTest->get($classname); |
||
| 92 | $externals = (array)$metric->get('externals'); |
||
| 93 | |||
| 94 | // global stats for each test |
||
| 95 | $infoAboutTests[$classname] = (object)[ |
||
| 96 | 'nbExternals' => sizeof(array_unique($externals)), |
||
| 97 | 'externals' => array_unique($externals), |
||
| 98 | 'filename' => $fileOfUnitTest, |
||
| 99 | 'classname' => $classname |
||
| 100 | ]; |
||
| 101 | |||
| 102 | foreach ($externals as $external) { |
||
| 103 | |||
| 104 | // search for this external in metrics |
||
| 105 | if (!$metrics->has($external)) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | // SUT (tested class) has unit test |
||
| 110 | $numberOfUnitTest = $metrics->get($external)->get('numberOfUnitTests'); |
||
| 111 | $numberOfUnitTest++; |
||
| 112 | $metrics->get($external)->set('numberOfUnitTests', $numberOfUnitTest); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // statistics |
||
| 117 | $sum = 0; |
||
| 118 | $nb = 0; |
||
| 119 | foreach ($metrics->all() as $metric) { |
||
| 120 | if (!$metric instanceof ClassMetric) { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $sum++; |
||
| 125 | if ($metric->get('numberOfUnitTests') > 0) { |
||
| 126 | $nb++; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $projectMetric->set('tests', $infoAboutTests); |
||
| 131 | $projectMetric->set('nbTests', sizeof($unitsTests)); |
||
| 132 | $projectMetric->set('nbCoveredClasses', $nb); |
||
| 133 | $projectMetric->set('percentCoveredClasses', round($nb / $sum * 100, 2)); |
||
| 134 | $projectMetric->set('nbUncoveredClasses', $sum - $nb); |
||
| 135 | $projectMetric->set('percentUncoveredClasses', round(($sum - $nb) / $sum * 100, 2)); |
||
| 136 | |||
| 137 | $metrics->attach($projectMetric); |
||
| 138 | } |
||
| 139 | } |
||
| 140 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.