示例#1
0
 public function provideCustomTokenPrefixCases()
 {
     $transformers = Transformers::create();
     $items = AccessibleObject::create($transformers)->items;
     $cases = array();
     foreach ($items as $item) {
         foreach ($item->getCustomTokenNames() as $name) {
             $cases[] = array($name);
         }
     }
     return $cases;
 }
 /**
  * Set configuration.
  *
  * Valid configuration options are:
  * - 'break' remove blank lines after a line with a 'break' statement
  * - 'continue' remove blank lines after a line with a 'continue' statement
  * - 'extra' [default] consecutive blank lines are squashed into one
  * - 'return' remove blank lines after a line with a 'return' statement
  * - 'throw' remove blank lines after a line with a 'throw' statement
  * - 'use' remove blank lines between 'use' import statements
  * - 'useTrait' remove blank lines between 'use' trait statements
  * - 'curly_brace_open' remove blank lines after a curly opening brace ('{')
  *
  * @param string[]|null $configuration
  */
 public function configure(array $configuration = null)
 {
     if (null === $configuration) {
         return;
     }
     if (!defined('CT_USE_TRAIT')) {
         Transformers::create();
         // TODO could use a better fix
     }
     $this->tokenKindCallbackMap = array();
     $this->tokenEqualsMap = array();
     foreach ($configuration as $item) {
         switch ($item) {
             case 'break':
                 $this->tokenKindCallbackMap[T_BREAK] = 'fixAfterToken';
                 break;
             case 'continue':
                 $this->tokenKindCallbackMap[T_CONTINUE] = 'fixAfterToken';
                 break;
             case 'extra':
                 $this->tokenKindCallbackMap[T_WHITESPACE] = 'removeMultipleBlankLines';
                 break;
             case 'return':
                 $this->tokenKindCallbackMap[T_RETURN] = 'fixAfterToken';
                 break;
             case 'throw':
                 $this->tokenKindCallbackMap[T_THROW] = 'fixAfterToken';
                 break;
             case 'use':
                 $this->tokenKindCallbackMap[T_USE] = 'removeBetweenUse';
                 break;
             case 'useTrait':
                 $this->tokenKindCallbackMap[CT_USE_TRAIT] = 'removeBetweenUse';
                 break;
             case 'curly_brace_open':
                 $this->tokenEqualsMap['{'] = 'fixAfterToken';
                 break;
             default:
                 throw new InvalidFixerConfigurationException($this->getName(), sprintf('Unknown configuration item "%s" passed.', $item));
         }
     }
 }
示例#3
0
 /**
  * Get token name.
  *
  * @return null|string token name
  */
 public function getName()
 {
     if (!isset($this->id)) {
         return;
     }
     $transformers = Transformers::create();
     if ($transformers->hasCustomToken($this->id)) {
         return $transformers->getCustomToken($this->id);
     }
     return token_name($this->id);
 }
示例#4
0
 /**
  * Set code. Clear all current content and replace it by new Token items generated from code directly.
  *
  * @param string $code PHP code
  */
 public function setCode($code)
 {
     // No need to work when the code is the same.
     // That is how we avoid a lot of work and setting changed flag.
     if ($code === $this->generateCode()) {
         return;
     }
     // clear memory
     $this->setSize(0);
     $tokens = defined('TOKEN_PARSE') ? token_get_all($code, TOKEN_PARSE) : token_get_all($code);
     $this->setSize(count($tokens));
     foreach ($tokens as $index => $token) {
         $this[$index] = new Token($token);
     }
     $transformers = Transformers::create();
     $transformers->transform($this);
     $this->foundTokenKinds = array();
     foreach ($this as $index => $token) {
         $this->registerFoundToken($token);
     }
     $this->rewind();
     $this->changeCodeHash(self::calculateCodeHash($code));
     $this->changed = true;
 }
示例#5
0
 /**
  * @see Command
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     Transformers::create();
     // make sure all transformers have defined custom tokens since configuration might depend on it
     $verbosity = $output->getVerbosity();
     $reporterFactory = ReporterFactory::create();
     $reporterFactory->registerBuiltInReporters();
     $resolver = new ConfigurationResolver();
     $resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setOptions(array('allow-risky' => $input->getOption('allow-risky'), 'config' => $input->getOption('config'), 'dry-run' => $input->getOption('dry-run'), 'rules' => $input->getOption('rules'), 'path' => $input->getArgument('path'), 'path-mode' => $input->getOption('path-mode'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file'), 'format' => $input->getOption('format')))->setFormats($reporterFactory->getFormats())->resolve();
     $reporter = $reporterFactory->getReporter($resolver->getFormat());
     $stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : ('txt' === $reporter->getFormat() ? $output : null);
     if (null !== $stdErr && extension_loaded('xdebug')) {
         $stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', 'You are running php-cs-fixer with xdebug enabled. This has a major impact on runtime performance.'));
     }
     $config = $resolver->getConfig();
     $configFile = $resolver->getConfigFile();
     if (null !== $stdErr && $configFile) {
         $stdErr->writeln(sprintf('Loaded config from "%s".', $configFile));
     }
     $linter = new NullLinter();
     if ($config->usingLinter()) {
         try {
             $linter = new Linter($config->getPhpExecutable());
         } catch (UnavailableLinterException $e) {
             if (null !== $stdErr && $configFile) {
                 $stdErr->writeln('Unable to use linter, can not find PHP executable.');
             }
         }
     }
     if (null !== $stdErr && $config->usingCache()) {
         $cacheFile = $config->getCacheFile();
         if (is_file($cacheFile)) {
             $stdErr->writeln(sprintf('Using cache file "%s".', $cacheFile));
         }
     }
     $showProgress = $resolver->getProgress();
     $runner = new Runner($config, $input->getOption('diff') ? new SebastianBergmannDiffer() : new NullDiffer(), $showProgress ? $this->eventDispatcher : null, $this->errorsManager, $linter, $resolver->isDryRun());
     $progressOutput = $showProgress && $stdErr ? new ProcessOutput($stdErr, $this->eventDispatcher) : new NullOutput();
     $this->stopwatch->start('fixFiles');
     $changed = $runner->fix();
     $this->stopwatch->stop('fixFiles');
     $progressOutput->printLegend();
     $fixEvent = $this->stopwatch->getEvent('fixFiles');
     $reportSummary = ReportSummary::create()->setChanged($changed)->setAddAppliedFixers(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity())->setDecoratedOutput($output->isDecorated())->setDryRun($resolver->isDryRun())->setMemory($fixEvent->getMemory())->setTime($fixEvent->getDuration());
     if ($output->isDecorated()) {
         $output->write($reporter->generate($reportSummary));
     } else {
         $output->write($reporter->generate($reportSummary), false, OutputInterface::OUTPUT_RAW);
     }
     $invalidErrors = $this->errorsManager->getInvalidErrors();
     $exceptionErrors = $this->errorsManager->getExceptionErrors();
     $lintErrors = $this->errorsManager->getLintErrors();
     if (null !== $stdErr) {
         if (count($invalidErrors) > 0) {
             $this->listErrors($stdErr, 'linting before fixing', $invalidErrors);
         }
         if (count($exceptionErrors) > 0) {
             $this->listErrors($stdErr, 'fixing', $exceptionErrors);
         }
         if (count($lintErrors) > 0) {
             $this->listErrors($stdErr, 'linting after fixing', $lintErrors);
         }
     }
     return $this->calculateExitStatus($resolver->isDryRun(), count($changed) > 0, count($invalidErrors) > 0, count($exceptionErrors) > 0);
 }
 /**
  * Creates test data by parsing '.test' files.
  *
  * @return IntegrationCase[][]
  */
 public function getTests()
 {
     Transformers::create();
     $fixturesDir = realpath(static::getFixturesDir());
     if (!is_dir($fixturesDir)) {
         throw new \UnexpectedValueException(sprintf('Given fixture dir "%s" is not a directory.', $fixturesDir));
     }
     $factory = new IntegrationCaseFactory();
     $tests = array();
     foreach (Finder::create()->files()->in($fixturesDir) as $file) {
         if ('test' !== $file->getExtension()) {
             continue;
         }
         $tests[] = array($factory->create($file->getRelativePathname(), file_get_contents($file->getRealpath())));
     }
     return $tests;
 }