/** * @inheritdoc */ public function execute(ResultCollection $collection, ResultCollection $aggregatedResults) { $files = $this->finder->find($this->path); if (0 == sizeof($files, COUNT_NORMAL)) { throw new \LogicException('No file found'); } $progress = new ProgressBar($this->output); $progress->start(sizeof($files, COUNT_NORMAL)); // tools $classMap = new ClassMap(); $tokenizer = new Tokenizer(); $syntaxChecker = new SyntaxChecker(); $fileAnalyzer = new FileAnalyzer($this->output, $this->withOOP, new Extractor(), new \Hal\Metrics\Complexity\Text\Halstead\Halstead(new \Hal\Component\Token\TokenType()), new \Hal\Metrics\Complexity\Text\Length\Loc(), new \Hal\Metrics\Design\Component\MaintainabilityIndex\MaintainabilityIndex(), new \Hal\Metrics\Complexity\Component\McCabe\McCabe(), new \Hal\Metrics\Complexity\Component\Myer\Myer(), $classMap); foreach ($files as $k => $filename) { $progress->advance(); // Integrity if (!$this->ignoreErrors && !$syntaxChecker->isCorrect($filename)) { $this->output->writeln(sprintf('<error>file %s is not valid and has been skipped</error>', $filename)); unset($files[$k]); continue; } // Analyze try { $tokens = $tokenizer->tokenize($filename); $resultSet = $fileAnalyzer->execute($filename, $tokens); } catch (NoTokenizableException $e) { $this->output->writeln(sprintf("<error>file %s has been skipped: \n%s</error>", $filename, $e->getMessage())); unset($files[$k]); continue; } catch (\Exception $e) { throw new \Exception(sprintf("a '%s' exception occured analyzing file %s\nMessage: %s", get_class($e), $filename, $e->getMessage()) . sprintf("\n------------\nStack:\n%s", $e->getTraceAsString()) . sprintf("\n------------\nDo not hesitate to report a bug: https://github.com/PhpMetrics/PhpMetrics/issues"), 0, $e->getPrevious()); } $collection->push($resultSet); } $progress->clear(); $progress->finish(); if ($this->withOOP) { // COUPLING (should be done after parsing files) $this->output->write(str_pad("\rAnalyzing coupling. This will take few minutes...", 80, " ")); $couplingAnalyzer = new CouplingAnalyzer($classMap, $collection); $couplingAnalyzer->execute($files); // LCOM (should be done after parsing files) $this->output->write(str_pad("\rLack of cohesion of method (lcom). This will take few minutes...", 80, " ")); $lcomAnalyzer = new LcomAnalyzer($classMap, $collection); $lcomAnalyzer->execute($files); // Card and Agresti (should be done after parsing files) $this->output->write(str_pad("\rAnalyzing System complexity. This will take few minutes...", 80, " ")); $lcomAnalyzer = new CardAndAgrestiAnalyzer($classMap, $collection); $lcomAnalyzer->execute($files); } }
/** * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Design pattern detector by Jean-François Lépine <https://twitter.com/Halleck45>'); $output->writeln(''); $finder = new Finder($input->getOption('extensions'), $input->getOption('excluded-dirs')); $files = $finder->find($input->getArgument('path')); if (0 == sizeof($files, COUNT_NORMAL)) { throw new \LogicException('No file found'); } // // 1. Extracting classes $output->writeln('<info>Extracting classes</info>'); $progress = new ProgressBar($output); $progress->start(sizeof($files, COUNT_NORMAL)); $collection = array(); $tokenizer = new Tokenizer(new CacheMemory()); $extractor = new Extractor($tokenizer); $nbClasses = 0; foreach ($files as $k => $filename) { $progress->advance(); $collection[] = $classes = $extractor->extract($filename); $nbClasses += sizeof($classes->getClasses()); } $progress->clear(); $progress->finish(); // inform user $output->writeln(''); $output->writeln(''); $output->writeln(sprintf("<info>Found %d classes</info>", $nbClasses)); // // 2. Extracting patterns $output->writeln('<info>Extracting design patterns</info>'); $patterns = array(); foreach ($collection as $oop) { $classes = $oop->getClasses(); $resolver = new PatternResolver($classes); foreach ($classes as $class) { $resolvedClass = new ResolvedClass($class); $resolver->resolve($resolvedClass); if ($resolvedClass->getPatterns()) { $patterns = array_merge($patterns, $resolvedClass->getPatterns()); } } } // inform user $output->writeln(sprintf("<info>Found %d design patterns</info>", sizeof($patterns))); $output->writeln(); foreach ($patterns as $pattern) { $output->writeln(sprintf("\t<info>[%s]</info> %s", $pattern->getName(), $pattern->describe())); } }
/** * @inheritdoc */ public function execute(ResultCollection $collection, ResultCollection $aggregatedResults) { $files = $this->finder->find($this->path); if (0 == sizeof($files, COUNT_NORMAL)) { throw new \LogicException('No file found'); } $progress = new ProgressHelper(); $progress->start($this->output, sizeof($files, COUNT_NORMAL)); // tools $classMap = new ClassMap(); $tokenizer = new Tokenizer(); $syntaxChecker = new SyntaxChecker(); $fileAnalyzer = new FileAnalyzer($this->output, $this->withOOP, new Extractor($tokenizer), new \Hal\Metrics\Complexity\Text\Halstead\Halstead($tokenizer, new \Hal\Component\Token\TokenType()), new \Hal\Metrics\Complexity\Text\Length\Loc($tokenizer), new \Hal\Metrics\Design\Component\MaintenabilityIndex\MaintenabilityIndex($tokenizer), new \Hal\Metrics\Complexity\Component\McCabe\McCabe($tokenizer), new \Hal\Metrics\Complexity\Component\Myer\Myer($tokenizer), $classMap); foreach ($files as $k => $filename) { $progress->advance(); // Integrity if (!$syntaxChecker->isCorrect($filename)) { $this->output->writeln(sprintf('<error>file %s is not valid and has been skipped</error>', $filename)); unset($files[$k]); continue; } // Analyze $resultSet = $fileAnalyzer->execute($filename); $collection->push($resultSet); } $progress->clear(); $progress->finish(); if ($this->withOOP) { // COUPLING (should be done after parsing files) $this->output->write(str_pad("\rAnalyzing coupling. This will take few minutes...", 80, " ")); $couplingAnalyzer = new CouplingAnalyzer($classMap, $collection); $couplingAnalyzer->execute($files); // LCOM (should be done after parsing files) $this->output->write(str_pad("\rLack of cohesion of method (lcom). This will take few minutes...", 80, " ")); $lcomAnalyzer = new LcomAnalyzer($classMap, $collection); $lcomAnalyzer->execute($files); // Card and Agresti (should be done after parsing files) $this->output->write(str_pad("\rAnalyzing System complexity. This will take few minutes...", 80, " ")); $lcomAnalyzer = new CardAndAgrestiAnalyzer($classMap, $collection); $lcomAnalyzer->execute($files); } }
public function testIDontFindFilesFromExcludedDirs() { $finder = new Finder('php', 'toExclude'); $results = $finder->find($this->toExplore); $this->assertEquals(3, sizeof($results)); }