public static function addSourcesToProject(InputInterface $input, Project $project) { $inputSources = []; if (NULL !== $input->getOption('load-from')) { $inputSources = array_merge($inputSources, array_map('trim', file($input->getOption('load-from')))); } if (NULL !== $input->getArgument('sources')) { $inputSources = array_merge($inputSources, $input->getArgument('sources')); } $inputSources = array_unique($inputSources); if (empty($inputSources)) { throw new \RuntimeException('No input sources/directories for scanning provided.'); } /** @var \SplFileInfo[] $files */ $files = []; foreach ($inputSources as $inputSource) { if (is_dir($inputSource)) { $files = array_merge($files, Util::scanDir($inputSource)); } else { if (is_file($inputSource)) { $files[] = new \SplFileInfo($inputSource); } else { throw new \RuntimeException("File {$inputSource} is not a file nor a directory."); } } } if (count($files) === 0) { throw new \RuntimeException('No source files to scan found'); } foreach ($files as $file) { $project->addSplFileInfo($file); } }
/** * 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()); }
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); } }