コード例 #1
0
 public static function fromException(\Exception $ex, File $file)
 {
     $message = 'An error occurred while analyzing "' . $file->getName() . '": ' . $ex->getMessage();
     $ex = new self($message, (int) $ex->getCode(), $ex);
     $ex->setAnalyzedFile($file);
     return $ex;
 }
コード例 #2
0
 public function analyze(File $file)
 {
     if (!$file instanceof PhpFile) {
         return;
     }
     NodeTraversal::traverseWithCallback($file->getAst(), $this);
 }
コード例 #3
0
 public final function analyze(File $file)
 {
     if (!$file instanceof PhpFile) {
         return;
     }
     $this->phpFile = $file;
     $this->stream->setInput($file->getContent(), $file->getAst());
     $this->analyzeStream();
 }
コード例 #4
0
 public function analyze(File $file)
 {
     if (!$file instanceof PhpFile || !$this->getSetting('enabled')) {
         return;
     }
     $this->phpFile = $file;
     $this->stream->setCode($file->getContent());
     $this->analyzeStream();
 }
コード例 #5
0
 public function enterScope(NodeTraversal $traversal)
 {
     $typeInference = new TypeInference($traversal->getControlFlowGraph(), $this->reverseInterpreter, $this->functionInterpreter, $this->methodInterpreter, $this->commentParser, $traversal->getScope(), $this->registry, $this->logger);
     try {
         $typeInference->analyze();
     } catch (MaxIterationsExceededException $ex) {
         $scopeRoot = $traversal->getScopeRoot();
         $this->logger->warning($ex->getMessage() . ' - Scope-Root: ' . get_class($scopeRoot) . ' on line ' . $scopeRoot->getLine() . ' in ' . $this->file->getName());
     }
 }
コード例 #6
0
 public function analyze(File $file)
 {
     if (!$file instanceof PhpFile) {
         return;
     }
     if (!$this->getSetting('enabled')) {
         return;
     }
     $this->file = $file;
     NodeTraversal::traverseWithCallback($file->getAst(), $this);
 }
コード例 #7
0
 public function analyze(File $file)
 {
     if (!$this->isEnabled()) {
         return;
     }
     if (!$file instanceof PhpFile) {
         return;
     }
     $this->phpFile = $file;
     NodeTraversal::traverseWithCallback($file->getAst(), $this, $this->getScopeCreator());
 }
コード例 #8
0
 public static function create($name, $content, $originalContent = null)
 {
     if (null === self::$phpParser) {
         self::initStatic();
     }
     if ('.php' === substr($name, -4)) {
         $file = self::createPhpFile($name, $content);
     } else {
         $file = new File($name, $content);
     }
     if (null !== $originalContent) {
         $file->setDiff(DiffUtils::generate($originalContent, $content));
     }
     return $file;
 }
コード例 #9
0
 public function analyze(File $file)
 {
     if (!$file instanceof PhpFile) {
         return;
     }
     $this->codingStyle = $this->analyzer->getPhpCodingStyle();
     $this->tokens = token_get_all($content = $file->getContent());
     $this->i = -1;
     $this->lines = explode("\n", $content);
     $this->curLine = 1;
     parent::analyze($file);
     while ($this->next()) {
         $this->checkWhitespace();
         if (T_OPEN_TAG === $this->token[0] && '<?' === $this->token[1] && !$this->codingStyle->allowShortPhpOpenTag) {
             $this->addComment(1, 'Please do not use the short-opening tag, but "<?php" instead.');
             continue;
         }
     }
     // Check that file ends with a linefeed character
     if ("\n" !== substr($content, -1)) {
         $this->addComment(count($this->lines), 'Please add a linefeed (\\n) at the end of the file.');
     }
     $foundTabs = $foundLinefeed = $foundTrailingWhitespace = false;
     foreach ($this->lines as $i => $line) {
         // Check that there are no tabs inside the file.
         if (!$foundTabs && preg_match('/^(?: )*\\t/', $line)) {
             $this->addComment($i + 1, 'Please do not use tabs for indentation, but 4 spaces for each tab.');
             // Assume that the users fixes all further occurrences of tabs.
             $foundTabs = true;
             continue;
         }
         // Check for correct line-ending character
         if (!$foundLinefeed && "\r" === substr($line, -1)) {
             $this->addComment($i + 1, 'Please do not use the line-ending (\\r\\n), but only \\n (0x0A).');
             // Assume that the user fixed all further occurrences.
             $foundLinefeed = true;
             continue;
         }
         // Check for trailing white-space
         if (!$foundTrailingWhitespace && preg_match('/\\s+$/', $line)) {
             $this->addComment($i + 1, 'Please do not add trailing whitespace.');
             $foundTrailingWhitespace = true;
             continue;
         }
     }
 }
コード例 #10
0
 public function testCreateConvertsParserErrorToComment()
 {
     $file = File::create('missing_semicolon.php', '<?php echo "foo"');
     $this->assertInstanceOf('PHPParser_Node', $file->getAst());
     $comments = $file->getComments(1);
     $this->assertSame(1, count($comments));
     $this->assertContains('This code did not parse for me. Apparently, there is an error somewhere around this line', (string) $comments[0]);
 }
コード例 #11
0
 /**
  * @dataProvider getTests
  */
 public function testFixingFiles($testFile)
 {
     $testData = $this->parseTestCase(file_get_contents($testFile));
     $this->file = \Scrutinizer\PhpAnalyzer\Model\File::create('test.php', $testData['before']);
     $this->analyzer->setConfigurationValues($testData['config']);
     $this->analyzer->analyze(new \Scrutinizer\PhpAnalyzer\Model\FileCollection(array($this->file)));
     $this->assertTrue($this->file->hasFixedFile(), 'File has no fixed file.');
     $this->assertEquals($testData['after'], $this->file->getFixedFile()->getContent());
 }
コード例 #12
0
 public final function analyze(\Scrutinizer\PhpAnalyzer\Model\File $file)
 {
     if (!$file instanceof \Scrutinizer\PhpAnalyzer\Model\PhpFile) {
         return;
     }
     if (!$this->isEnabled()) {
         return;
     }
     $this->fixedFile = $fixedFile = $file->getOrCreateFixedFile();
     if (!$fixedFile->hasAst()) {
         return;
     }
     $this->stream->setInput($originalContent = $fixedFile->getContent(), $fixedFile->getAst());
     $this->analyzeStream();
     $newContent = $this->getNewContent();
     if ($newContent !== $originalContent) {
         $fixedFile->setContent($newContent);
     }
 }
コード例 #13
0
 /**
  * @dataProvider getTests
  */
 public function testAnalyze($filename)
 {
     $test = $this->parseTestCase($filename);
     $testName = basename($filename);
     $phpFile = File::create($test['filename'], $test['code']);
     if (!empty($test['diff'])) {
         $phpFile->setDiff($test['diff']);
     }
     $expectedComments = $test['comments'];
     $config = $test['config'];
     $analyzer = $this->getAnalyzer();
     $analyzer->setConfigurationValues($config);
     $analyzer->analyze(new FileCollection(array($phpFile)));
     $actualComments = $phpFile->getComments();
     foreach ($expectedComments as $line => $comments) {
         $this->assertTrue(isset($actualComments[$line]), sprintf('Expected comments for line "%d", but got none in test "%s". Comments: %s', $line, $testName, var_export($actualComments, true)));
         $this->assertSame(count($comments), count($actualComments[$line]), sprintf('Expected "%d" comments for line "%d", but got "%d" comments. Comments: %s', count($comments), $line, count($actualComments[$line]), var_export($actualComments, true)));
         foreach ($comments as $comment) {
             $found = false;
             foreach ($actualComments[$line] as $actualComment) {
                 if (false !== strpos((string) $actualComment, $comment)) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $availableComments = "\n";
                 foreach ($actualComments[$line] as $aComment) {
                     $availableComments .= $aComment . "\n";
                 }
                 $this->fail(sprintf('Expected comment "%s" on line "%d", but did not find it. Available Comments: %s', $comment, $line, $availableComments));
             }
         }
     }
     foreach ($actualComments as $line => $actualComment) {
         $this->assertTrue(isset($expectedComments[$line]), sprintf('Found comment "%s" on line "%d", but did not expect it.', (string) $actualComment[0], $line));
     }
     if (null !== $test['after']) {
         $this->assertTrue($phpFile->hasFixedFile());
         $this->assertEquals($test['after'], $phpFile->getFixedFile()->getContent());
     }
 }
コード例 #14
0
 public function getCodingStyleTests()
 {
     $testDir = realpath($this->getTestDir());
     try {
         $tests = array();
         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testDir)) as $file) {
             $test = $this->parseTestCase(file_get_contents($file->getRealpath()));
             $phpFile = File::create($file->getFilename() . '.php', $test['code']);
             if (!empty($test['diff'])) {
                 $phpFile->setDiff($test['diff']);
             }
             $tests[] = array($file->getFilename(), $phpFile, $test['comments']);
         }
     } catch (\Exception $ex) {
         echo "Could not load test cases\n";
         echo $ex->getMessage();
         exit;
     }
     return $tests;
 }
コード例 #15
0
 public function add(File $file)
 {
     if (isset($this->files[$name = $file->getName()])) {
         throw new \InvalidArgumentException(sprintf('There already exists a file named "%s".', $name));
     }
     $this->files[$name] = $file;
     $this->size += strlen($file->getContent());
 }