/**
  * @param array $expectedMessages The expected list of messages
  * @param string $input The input that is tokenized
  * @param \SplFileInfo|null $file
  * @param CheckerInterface|null $checker
  */
 protected function makeTest(array $expectedMessages, $input, \SplFileInfo $file = null, CheckerInterface $checker = null)
 {
     $checker = $checker ?: $this->getChecker();
     $file = $file ?: $this->getTestFile();
     $fileIsSupported = $checker->supports($file);
     Tokens::clearCache();
     $tokens = Tokens::fromCode($input);
     if ($fileIsSupported) {
         self::assertTrue($checker->isCandidate($tokens), 'Fixer must be a candidate for input code.');
         $checker->check($file, $tokens);
     }
     $iterator = new ReportedTokenIterator(new LineNumberIterator($tokens));
     $lineMessages = [];
     foreach ($iterator as $k => $messages) {
         $lineMessages[$k] = array_map(function (Message $message) {
             return $message->toArray();
         }, $messages);
     }
     self::assertEquals($expectedMessages, $lineMessages);
 }
Exemple #2
0
 /**
  * @param \SplFileInfo $file
  * @param \Symfony\CS\FixerInterface[] $fixers
  * @param CheckerInterface[] $checkers
  * @param bool $dryRun
  * @param bool $diff
  * @param FileCacheManager $fileCacheManager
  * @param FileMessageCacheManager $messageCacheManager
  *
  * @return array|null|void
  */
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager, array $checkers = array(), FileMessageCacheManager $messageCacheManager = null)
 {
     $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 false;
     }
     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 false;
     }
     $old = file_get_contents($file->getRealpath());
     // 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();
     $checkMessages = [];
     try {
         if ($dryRun) {
             $checkMessages = $this->runCheckers($file, $checkers, $tokens);
         }
         $appliedFixers = $this->runFixers($file, $fixers, $tokens);
         if (!$dryRun) {
             $checkMessages = $this->runCheckers($file, $checkers, $tokens);
         }
     } 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 false;
     }
     $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 false;
         }
         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, 'checkMessages' => $checkMessages);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     } elseif (!empty($checkMessages)) {
         $fixInfo = array('checkMessages' => $checkMessages);
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     if ($messageCacheManager !== null) {
         $messageCacheManager->setMessages($this->getFileRelativePathname($file), isset($fixInfo['checkMessages']) ? $fixInfo['checkMessages'] : []);
     }
     $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     return $fixInfo;
 }