예제 #1
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();
     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 (!$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;
 }
예제 #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()')) {
         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 (\ParseError $e) {
         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), sprintf('Linting error at line %d: "%s".', $e->getLine(), $e->getMessage()));
         }
         return;
     } catch (\Error $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;
     } 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;
 }