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;
 }
Example #2
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;
 }
Example #3
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;
 }