Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CS config by default too #5467

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion .php_cs.dist
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

$header = <<<'EOF'
This file is part of PHP CS Fixer.

Expand Down Expand Up @@ -39,7 +49,8 @@ if (false !== getenv('FABBOT_IO')) {
PhpCsFixer\FixerFactory::create()
->registerBuiltInFixers()
->registerCustomFixers($config->getCustomFixers())
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()));
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()))
;
} catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
$config->setRules([]);
} catch (UnexpectedValueException $e) {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Expand Up @@ -60,4 +60,8 @@ parameters:
message: '/^\$this\(PhpCsFixer\\Tokenizer\\Tokens\) does not accept PhpCsFixer\\Tokenizer\\Token\|null\.$/'
path: src/Tokenizer/Tokens.php

- # https://github.com/phpstan/phpstan/issues/4808
message: '/^Access to private property \$ignore of parent class Symfony\\Component\\Finder\\Finder\.$/'
path: src/Finder.php

tipsOfTheDay: false
20 changes: 20 additions & 0 deletions src/Finder.php
Expand Up @@ -30,4 +30,24 @@ public function __construct()
->exclude('vendor')
;
}

public function getIterator()
{
// add config files even if dot files are ignored
$configFilenameRegex = '\.php_cs(?:\..+)?';
$this->name('~^'.$configFilenameRegex.'$~is');

$fx = \Closure::bind(function () { // rebound function can be called without assigment as of PHP 7
return $this->ignore & static::IGNORE_DOT_FILES;
}, $this, parent::class);
$isDotFilesIgnored = $fx();
if ($isDotFilesIgnored) {
$this
->ignoreDotFiles(false)
->notPath('~(?:^|/)(?!'.$configFilenameRegex.'(?:/|$))\..*(?:/|$)~')
;
}

return parent::getIterator();
}
}
24 changes: 19 additions & 5 deletions tests/ConfigTest.php
Expand Up @@ -127,8 +127,13 @@ public function testThatFinderWorksWithDirSetOnConfig()
false
);

static::assertCount(1, $items);
static::assertSame('somefile.php', $items[0]->getFilename());
static::assertCount(3, $items);
usort($items, function (\SplFileInfo $a, \SplFileInfo $b) {
return strcmp($a->getFilename(), $b->getFilename());
});
static::assertSame('.php_cs', $items[0]->getFilename());
static::assertSame('.php_cs.dist', $items[1]->getFilename());
static::assertSame('somefile.php', $items[2]->getFilename());
}

public function testThatCustomFinderWorks()
Expand All @@ -143,8 +148,13 @@ public function testThatCustomFinderWorks()
false
);

static::assertCount(1, $items);
static::assertSame('somefile.php', $items[0]->getFilename());
static::assertCount(3, $items);
usort($items, function (\SplFileInfo $a, \SplFileInfo $b) {
return strcmp($a->getFilename(), $b->getFilename());
});
static::assertSame('.php_cs', $items[0]->getFilename());
static::assertSame('.php_cs.dist', $items[1]->getFilename());
static::assertSame('somefile.php', $items[2]->getFilename());
}

public function testThatCustomSymfonyFinderWorks()
Expand All @@ -159,8 +169,12 @@ public function testThatCustomSymfonyFinderWorks()
false
);

static::assertCount(1, $items);
static::assertCount(2, $items);
usort($items, function (\SplFileInfo $a, \SplFileInfo $b) {
return strcmp($a->getFilename(), $b->getFilename());
});
static::assertSame('somefile.php', $items[0]->getFilename());
static::assertSame('somefile2.php.txt', $items[1]->getFilename());
}

public function testThatCacheFileHasDefaultValue()
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/ConfigurationResolverTest.php
Expand Up @@ -565,14 +565,14 @@ static function ($item) use ($dir) {
],
'configured only by finder' => [
// don't override if the argument is empty
$cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
$cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/.php_cs', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
Finder::create()
->in($dir),
[],
'override',
],
'configured only by argument' => [
$cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
$cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/.php_cs', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
Finder::create(),
[$dir],
'override',
Expand Down
Empty file.
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions tests/Fixtures/ci-integration/.php_cs.dist
@@ -1,9 +1,9 @@
<?php

return (new PhpCsFixer\Config())
->setRules(array(
->setRules([
'@Symfony' => true,
))
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
Expand Down
6 changes: 3 additions & 3 deletions tests/Smoke/CiIntegrationTest.php
Expand Up @@ -219,7 +219,7 @@ public function provideIntegrationCases()
'',
'',
],
'...',
'....',
],
'changes-including-custom-config-file-creation' => [
'changes-including-custom-config-file-creation',
Expand All @@ -238,7 +238,7 @@ public function provideIntegrationCases()
'',
'',
],
'...',
'.....',
],
'changes-including-composer-lock' => [
'changes-including-composer-lock',
Expand All @@ -257,7 +257,7 @@ public function provideIntegrationCases()
'',
'',
],
'...',
'....',
],
];
}
Expand Down