/** * Resolve GodObjects * * 8 public methods or more (excluding getters and setters) * lack of cohesion of methods * instanciate 6 or more different classes * * @param ResolvedClass $resolved */ public function resolve(ResolvedClass $resolved) { $class = $resolved->getClass(); $pattern = new GodObject($class->getFullname()); // we don't look private methods $collection = (new Collection($class->getMethods()))->where('method => method.getVisibility() == "public"'); // at least 8 public methods if (sizeof($collection) < 8) { return; } // lack of cohesion of methods $lcom = new LackOfCohesionOfMethods(); $result = $lcom->calculate($class); if ($result->getLcom() < 3) { return; } // know everything (instanciate more than 8 different classes) $nb = 0; foreach ($collection as $method) { $instancied = array_diff($method->getInstanciedClasses(), array($class->getFullname())); $nb += sizeof($instancied); } if ($nb < 6) { return; } $resolved->pushPattern($pattern); }
/** * @dataProvider providesFilesForLcom */ public function testICanCalculateLackOfCohesionOfClass($file, $expected) { $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); $result = $extractor->extract($file); $classes = $result->getClasses(); $class = $classes[0]; $lcom = new LackOfCohesionOfMethods(); $result = $lcom->calculate($class); $this->assertEquals($expected, $result->getLCOM()); }
/** * Calculates lcom for file (and not for class) * * @param string $filename * @return Result */ public function calculate($filename) { $rOOP = $this->classMap->getClassesIn($filename); $result = new Result($filename); $n = 0; $lcom = new LackOfCohesionOfMethods(); foreach ($rOOP->getClasses() as $class) { $r = $lcom->calculate($class); $n += $r->getLcom(); } $result->setLcom($n); return $result; }