public function main() { if (NULL === $this->filesets) { throw new \BuildException('No fileset provided'); } $project = new Project(new Phing($this)); $project->addListener($listener = new PhingListener($this)); $analyzers = NULL !== $this->getConfigFile() ? require $this->getConfigFile() : Project::getDefaultConfig(); $project->addAnalyzers($analyzers); # Add files foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($this->project); /** @var \PhingFile $fromDir */ $fromDir = $fs->getDir($this->project); /** @var $files */ $files = $ds->getIncludedFiles(); foreach ($files as $file) { $fileName = $fromDir->getAbsolutePath() . DIRECTORY_SEPARATOR . $file; $this->log('Adding file ' . $fileName, \Project::MSG_VERBOSE); $project->addSplFileInfo(new \SplFileInfo($fileName)); } } $project->analyze(); $buildErrorMessage = $listener->getBuildErrorMessage(); if ($this->haltonerror && !empty($buildErrorMessage)) { throw new \BuildException($buildErrorMessage); } }
protected function execute(InputInterface $input, OutputInterface $output) { $logger = new SymfonyConsoleOutput($output); $project = new Project($logger); if ($input->getOption('quiet')) { $logger->setReportingLevel(Logger::ERROR); } else { if ($input->getOption('verbose')) { $logger->setReportingLevel(Logger::INFO); } } # Fall back to UTC if date.timezone is not set $dateTimezone = ini_get('date.timezone'); if (empty($dateTimezone)) { $logger->warning('date.timezone not set, falling back to UTC'); date_default_timezone_set('UTC'); } SourceHandler::addSourcesToProject($input, $project); $analyzers = NULL !== $input->getOption('config') ? require $input->getOption('config') : Project::getDefaultConfig(); # Configure listener $listener = NULL; if (NULL !== $input->getOption('report')) { $format = NULL === $input->getOption('format') ? 'plain' : $input->getOption('format'); if ('plain' === $format) { $listener = new Plain(fopen($input->getOption('report'), 'w')); } else { if (0 === strpos($format, 'json')) { $options = 0; if ('json-pretty' === $format) { $options = JSON_PRETTY_PRINT; } $json = new JsonFormatter($options); $listener = new Json(fopen($input->getOption('report'), 'w'), $json); } else { throw new \RuntimeException("Unsupported report format '{$format}'"); } } } else { if (!$input->getOption('quiet')) { $listener = new Plain(\STDOUT); } } if (NULL !== $listener) { $project->addListener($listener); } $project->addAnalyzers($analyzers); $project->analyze(); $analyzerReports = $project->getAnalyzerReports(); if (count($analyzerReports) > 0) { return 1; } else { $output->writeln('Nothing found to report.'); return 0; } }
/** * Although this test seems redundant I use it to ensure that as far as it's * possible the analyzers do not negatively affect each other. */ public function testAllAnalyzers() { $project = new Project(Null::getInstance()); foreach (Util::scanDir(self::getAnalyzerTestsDir(), '/\\.phptest$/') as $file) { $project->addSplFileInfo(new \SplFileInfo($file)); } $project->addAnalyzers(Project::getDefaultConfig()); $project->analyze(); $reports = $project->getAnalyzerReports(); $this->assertSame(10, count($reports)); $this->assertSame('Class Mfn\\PHP\\Analyzer\\Tests\\AbstractMethodMissing\\b misses the following abstract method: Mfn\\PHP\\Analyzer\\Tests\\AbstractMethodMissing\\a::b()', $reports[0]->getTimestampedReport()->getReport()->report()); $this->assertSame('Class Mfn\\PHP\\Analyzer\\Tests\\InterfaceMethodMissing\\d misses the following interface method: Mfn\\PHP\\Analyzer\\Tests\\InterfaceMethodMissing\\a::c()', $reports[1]->getTimestampedReport()->getReport()->report()); $this->assertSame('Declaration of Mfn\\PHP\\Analyzer\\Tests\\MethodDeclarationCompatibility\\b::c($a, $a) must be compatible with Mfn\\PHP\\Analyzer\\Tests\\MethodDeclarationCompatibility\\a::c(array $a = 1)', $reports[2]->getTimestampedReport()->getReport()->report()); # Empty exception catch block reports $this->assertSame(27, $reports[3]->getSourceFragment()->getLineSegment()->getHighlightLine()); $this->assertSame('Dynamic class instantiation with variable $foo in 003_dynamic_class_instantiation.phptest:26', $reports[4]->getTimestampedReport()->getReport()->report()); $this->assertSame('Variable used in constructing raw SQL, is it escaped?', $reports[5]->getTimestampedReport()->getReport()->report()); $this->assertSame(29, $reports[5]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine()); $this->assertSame(30, $reports[6]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine()); $this->assertSame(31, $reports[7]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine()); # Empty exception catch block reports $this->assertSame(27, $reports[8]->getSourceFragment()->getLineSegment()->getHighlightLine()); $this->assertSame(32, $reports[9]->getSourceFragment()->getLineSegment()->getHighlightLine()); }