Exemple #1
0
function setHeader()
{
    HeaderCommentFixer::setHeader(<<<'EOF'
This file is part of the Kreta package.

(c) Beñat Espiña <*****@*****.**>
(c) Gorka Laucirica <*****@*****.**>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF
);
}
    public function testFixRemovePreviousHeader()
    {
        HeaderCommentFixer::setHeader('');
        $expected = <<<'EOH'
<?php

phpinfo();
EOH;
        $input = <<<'EOH'
<?php

/*
 * This file is part of the PHP CS utility.
 *
 * (c) Fabien Potencier <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

phpinfo();
EOH;
        $this->makeTest($expected, $input);
    }
 /**
  * Applies the given fixers on the input and checks the result.
  *
  * It will write the input to a temp file. The file will be fixed by a Fixer instance
  * configured with the given fixers. The result is compared with the expected output.
  * It checks if no errors were reported during the fixing.
  *
  * @param string           $testFileName Filename
  * @param string           $testTitle    Test title
  * @param FixerInterface[] $fixers       Fixers to use
  * @param string           $input        Code to fix
  * @param string|null      $expected     Expected result or null if the input is expected not to change
  */
 protected function doTestIntegration($testFileName, $testTitle, $fixers, $input, $expected = null)
 {
     $fixer = new Fixer();
     $tmpFile = static::getTempFile();
     if (false === @file_put_contents($tmpFile, $input)) {
         throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile));
     }
     $changed = $fixer->fixFile(new \SplFileInfo($tmpFile), $fixers, false, true, new FileCacheManager(false, null, $fixers, HeaderCommentFixer::getHeader()));
     $errorsManager = $fixer->getErrorsManager();
     if (!$errorsManager->isEmpty()) {
         $errors = $errorsManager->getExceptionErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during fixing: %s', $this->implodeErrors($errors)));
         $errors = $errorsManager->getInvalidErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during linting before fixing: %s.', $this->implodeErrors($errors)));
         $errors = $errorsManager->getLintErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during linting after fixing: %s.', $this->implodeErrors($errors)));
     }
     if (null === $expected) {
         $this->assertEmpty($changed, sprintf("Expected no changes made to test \"%s\" in \"%s\".\nFixers applied:\n\"%s\".\nDiff.:\n\"%s\".", $testTitle, $testFileName, $changed === null ? '[None]' : implode(',', $changed['appliedFixers']), $changed === null ? '[None]' : $changed['diff']));
         return;
     }
     $this->assertNotEmpty($changed, sprintf('Expected changes made to test "%s" in "%s".', $testTitle, $testFileName));
     $this->assertSame($expected, file_get_contents($tmpFile), sprintf('Expected changes do not match result, for "%s" in "%s".', $testTitle, $testFileName));
     // run the test again with the `expected` part, this should always stay the same
     $this->testIntegration($testFileName, $testTitle . ' "--EXPECT-- part run"', $fixers, $expected);
 }
Exemple #4
0
 /**
  * Fixes all files for the given finder.
  *
  * @param ConfigInterface $config A ConfigInterface instance
  * @param bool            $dryRun Whether to simulate the changes or not
  * @param bool            $diff   Whether to provide diff
  *
  * @return array
  */
 public function fix(ConfigInterface $config, $dryRun = false, $diff = false)
 {
     $fixers = $this->prepareFixers($config);
     $changed = array();
     $this->stopwatch->openSection();
     $fileCacheManager = new FileCacheManager($config->usingCache(), $config->getCacheFile(), $config->getFixers(), HeaderCommentFixer::getHeader());
     $processed = array();
     foreach ($config->getFinder() as $file) {
         $name = $this->getFileRelativePathname($file);
         if (in_array($name, $processed, true)) {
             continue;
         }
         $processed[] = $name;
         if ($file->isDir() || $file->isLink()) {
             continue;
         }
         $this->stopwatch->start($this->getFileRelativePathname($file));
         if ($fixInfo = $this->fixFile($file, $fixers, $dryRun, $diff, $fileCacheManager)) {
             $changed[$name] = $fixInfo;
         }
         $this->stopwatch->stop($this->getFileRelativePathname($file));
     }
     $this->stopwatch->stopSection('fixFile');
     return $changed;
 }
Exemple #5
0
<?php

/*
 * This file is part of the Sylius package.
 *
 * (c) Paweł Jędrzejewski
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
$header = <<<EOF
This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
HeaderCommentFixer::setHeader($header);
return ['header_comment', 'short_array_syntax', 'ordered_use', '-empty_return', '-phpdoc_no_empty_return', '-phpdoc_params', '-phpdoc_short_description'];
 /**
  * @return string[]
  */
 public function getFixers()
 {
     $fixers = array_merge(isset($this->styleCIConfig['enabled']) ? $this->styleCIConfig['enabled'] : array(), isset($this->styleCIConfig['disabled']) ? array_map(function ($disabledFixer) {
         return '-' . $disabledFixer;
     }, $this->styleCIConfig['disabled']) : array());
     if (HeaderCommentFixer::getHeader()) {
         array_push($fixers, 'header_comment');
     }
     return $fixers;
 }