Ejemplo n.º 1
0
 protected function makeTest($expected, $input = null, \SplFileInfo $file = null, FixerInterface $fixer = null)
 {
     if ($expected === $input) {
         throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
     }
     $fixer = $fixer ?: $this->getFixer();
     $file = $file ?: $this->getTestFile();
     $fileIsSupported = $fixer->supports($file);
     if (null !== $input) {
         Tokens::clearCache();
         $tokens = Tokens::fromCode($input);
         if ($fileIsSupported) {
             $this->assertTrue($fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
             $fixResult = $fixer->fix($file, $tokens);
             $this->assertNull($fixResult, '->fix method must return null.');
         }
         $this->assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
         $this->assertSame($expected, $tokens->generateCode(), 'Code build on input code must match expected code.');
         Tokens::clearCache();
         $expectedTokens = Tokens::fromCode($expected);
         $tokens->clearEmptyTokens();
         $this->assertTokens($expectedTokens, $tokens);
     }
     Tokens::clearCache();
     $tokens = Tokens::fromCode($expected);
     if ($fileIsSupported) {
         $fixResult = $fixer->fix($file, $tokens);
         $this->assertNull($fixResult, '->fix method must return null.');
     }
     $this->assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
     $this->assertSame($expected, $tokens->generateCode(), 'Code build on expected code must not change.');
 }
Ejemplo n.º 2
0
 private function fixFile(\SplFileInfo $file, $dryRun)
 {
     $new = $old = file_get_contents($file->getRealpath());
     $appliedFixers = [];
     Tokens::clearCache();
     try {
         foreach ($this->fixers as $fixer) {
             if (!$fixer->supports($file)) {
                 continue;
             }
             $newest = $fixer->fix($file, $new);
             if ($newest !== $new) {
                 $appliedFixers[] = $fixer->getName();
             }
             $new = $newest;
         }
     } catch (\Exception $e) {
         if ($this->errorsManager) {
             $this->errorsManager->report(ErrorsManager::ERROR_TYPE_EXCEPTION, $this->getFileRelativePathname($file), $e->__toString());
         }
         return;
     }
     if ($new !== $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
     }
     return $appliedFixers;
 }
 /**
  * @param int    $expected
  * @param string $code
  * @param int    $index
  *
  * @dataProvider provideCommentBlockStartDetectionCases
  */
 public function testCommentBlockStartDetection($expected, $code, $index)
 {
     Tokens::clearCache();
     $tokens = Tokens::fromCode($code);
     $fixer = $this->getFixer();
     $method = new \ReflectionMethod($fixer, 'findCommentBlockStart');
     $method->setAccessible(true);
     if ($expected !== ($result = $method->invoke($fixer, $tokens, $index))) {
         $this->fail(sprintf('Expected index %d (%s) got index %d (%s).', $expected, $tokens[$expected]->toJson(), $result, $tokens[$result]->toJson()));
     }
 }
Ejemplo n.º 4
0
 protected function makeTest($expected, $input = null, \SplFileInfo $file = null)
 {
     if ($expected === $input) {
         throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
     }
     $fixer = $this->getFixer();
     $file = $file ?: $this->getTestFile();
     $fileIsSupported = $fixer->supports($file);
     if (null !== $input) {
         $fixedCode = $fileIsSupported ? $fixer->fix($file, $input) : $input;
         $this->assertSame($expected, $fixedCode);
         $tokens = Tokens::fromCode($fixedCode);
         // Load cached collection (used by the fixer)
         Tokens::clearCache();
         $expectedTokens = Tokens::fromCode($fixedCode);
         // Load the expected collection based on PHP parsing
         $this->assertTokens($expectedTokens, $tokens);
     }
     $this->assertSame($expected, $fileIsSupported ? $fixer->fix($file, $expected) : $expected);
 }
Ejemplo n.º 5
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if ('' === $old || !$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old) || PHP_VERSION_ID >= 50306 && PHP_VERSION_ID < 50400 && false !== stripos($old, '__halt_compiler()')) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         return;
     }
     try {
         $this->linter->lintFile($file->getRealpath());
     } catch (LintingException $e) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_INVALID));
         $this->errorsManager->report(new Error(Error::TYPE_INVALID, $this->getFileRelativePathname($file)));
         return;
     }
     $old = file_get_contents($file->getRealpath());
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     $tokens = Tokens::fromCode($old);
     $newHash = $oldHash = $tokens->getCodeHash();
     try {
         foreach ($fixers as $fixer) {
             if (!$fixer->supports($file) || !$fixer->isCandidate($tokens)) {
                 continue;
             }
             $fixer->fix($file, $tokens);
             if ($tokens->isChanged()) {
                 $tokens->clearEmptyTokens();
                 $tokens->clearChanged();
                 $appliedFixers[] = $fixer->getName();
             }
         }
     } catch (\Exception $e) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_EXCEPTION));
         $this->errorsManager->report(new Error(Error::TYPE_EXCEPTION, $this->getFileRelativePathname($file)));
         return;
     }
     $fixInfo = null;
     if (!empty($appliedFixers)) {
         $new = $tokens->generateCode();
         $newHash = $tokens->getCodeHash();
     }
     // We need to check if content was changed and then applied changes.
     // But we can't simple check $appliedFixers, because one fixer may revert
     // work of other and both of them will mark collection as changed.
     // Therefore we need to check if code hashes changed.
     if ($oldHash !== $newHash) {
         try {
             $this->linter->lintSource($new);
         } catch (LintingException $e) {
             $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_LINT));
             $this->errorsManager->report(new Error(Error::TYPE_LINT, $this->getFileRelativePathname($file)));
             return;
         }
         if (!$dryRun && false === @file_put_contents($file->getRealpath(), $new)) {
             $error = error_get_last();
             if ($error) {
                 throw new IOException(sprintf('Failed to write file "%s", "%s".', $file->getRealpath(), $error['message']), 0, null, $file->getRealpath());
             }
             throw new IOException(sprintf('Failed to write file "%s".', $file->getRealpath()), 0, null, $file->getRealpath());
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     return $fixInfo;
 }
Ejemplo n.º 6
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if (!$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old)) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         }
         return;
     }
     if ($this->lintManager && !$this->lintManager->createProcessForFile($file->getRealpath())->isSuccessful()) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_INVALID));
         }
         return;
     }
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     try {
         foreach ($fixers as $fixer) {
             if (!$fixer->supports($file)) {
                 continue;
             }
             $newest = $fixer->fix($file, $new);
             if ($newest !== $new) {
                 $appliedFixers[] = $fixer->getName();
             }
             $new = $newest;
         }
     } catch (\Exception $e) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_EXCEPTION));
         }
         if ($this->errorsManager) {
             $this->errorsManager->report(ErrorsManager::ERROR_TYPE_EXCEPTION, $this->getFileRelativePathname($file), $e->__toString());
         }
         return;
     }
     $fixInfo = null;
     if ($new !== $old) {
         if ($this->lintManager) {
             $lintProcess = $this->lintManager->createProcessForSource($new);
             if (!$lintProcess->isSuccessful()) {
                 if ($this->eventDispatcher) {
                     $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_LINT));
                 }
                 if ($this->errorsManager) {
                     $this->errorsManager->report(ErrorsManager::ERROR_TYPE_LINT, $this->getFileRelativePathname($file), $lintProcess->getOutput());
                 }
                 return;
             }
         }
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     if ($this->eventDispatcher) {
         $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     }
     return $fixInfo;
 }
Ejemplo n.º 7
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if (!$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old)) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         }
         return;
     }
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     foreach ($fixers as $fixer) {
         if (!$fixer->supports($file)) {
             continue;
         }
         $newest = $fixer->fix($file, $new);
         if ($newest !== $new) {
             $appliedFixers[] = $fixer->getName();
         }
         $new = $newest;
     }
     $fixInfo = null;
     if ($new !== $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     if ($this->eventDispatcher) {
         $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     }
     return $fixInfo;
 }