From ce3b917229365660990f703d7890f3333ed83a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 26 Jan 2021 13:24:51 +0100 Subject: [PATCH 1/6] Fix CS config by default too --- .php_cs.dist | 13 +++++++++- src/Finder.php | 4 ++++ tests/ConfigTest.php | 24 +++++++++++++++---- tests/Console/ConfigurationResolverTest.php | 4 ++-- tests/Fixtures/FinderDirectory/.php_cs | 0 tests/Fixtures/FinderDirectory/.php_cs.dist | 0 .../FinderDirectory/somefile2.php.txt | 0 tests/Fixtures/ci-integration/.php_cs.dist | 4 ++-- tests/Smoke/CiIntegrationTest.php | 6 ++--- 9 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 tests/Fixtures/FinderDirectory/.php_cs create mode 100644 tests/Fixtures/FinderDirectory/.php_cs.dist create mode 100644 tests/Fixtures/FinderDirectory/somefile2.php.txt diff --git a/.php_cs.dist b/.php_cs.dist index 1c9ea9b79da..e33c2514d56 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -1,5 +1,15 @@ + * Dariusz RumiƄski + * + * 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. @@ -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) { diff --git a/src/Finder.php b/src/Finder.php index 807a633f4f5..68a6a3216a4 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -27,6 +27,10 @@ public function __construct() $this ->files() ->name('*.php') + ->ignoreDotFiles(false) + ->notPath('~(^|/)\.(?!php_cs(\.dist)?(/|$)).+(/|$)~') + ->name('.php_cs') + ->name('.php_cs.dist') ->exclude('vendor') ; } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 780e9a808d8..6ba0a9c6abd 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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() @@ -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() @@ -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() diff --git a/tests/Console/ConfigurationResolverTest.php b/tests/Console/ConfigurationResolverTest.php index bd04936ab9f..98f72b7c0ed 100644 --- a/tests/Console/ConfigurationResolverTest.php +++ b/tests/Console/ConfigurationResolverTest.php @@ -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', diff --git a/tests/Fixtures/FinderDirectory/.php_cs b/tests/Fixtures/FinderDirectory/.php_cs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/Fixtures/FinderDirectory/.php_cs.dist b/tests/Fixtures/FinderDirectory/.php_cs.dist new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/Fixtures/FinderDirectory/somefile2.php.txt b/tests/Fixtures/FinderDirectory/somefile2.php.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/Fixtures/ci-integration/.php_cs.dist b/tests/Fixtures/ci-integration/.php_cs.dist index 088317ac351..4faae3319cb 100644 --- a/tests/Fixtures/ci-integration/.php_cs.dist +++ b/tests/Fixtures/ci-integration/.php_cs.dist @@ -1,9 +1,9 @@ setRules(array( + ->setRules([ '@Symfony' => true, - )) + ]) ->setFinder( PhpCsFixer\Finder::create() ->in(__DIR__) diff --git a/tests/Smoke/CiIntegrationTest.php b/tests/Smoke/CiIntegrationTest.php index 79a94eee57c..b25327fd0f0 100644 --- a/tests/Smoke/CiIntegrationTest.php +++ b/tests/Smoke/CiIntegrationTest.php @@ -219,7 +219,7 @@ public function provideIntegrationCases() '', '', ], - '...', + '....', ], 'changes-including-custom-config-file-creation' => [ 'changes-including-custom-config-file-creation', @@ -238,7 +238,7 @@ public function provideIntegrationCases() '', '', ], - '...', + '.....', ], 'changes-including-composer-lock' => [ 'changes-including-composer-lock', @@ -257,7 +257,7 @@ public function provideIntegrationCases() '', '', ], - '...', + '....', ], ]; } From dba13cca0f6976ea7a997aaa63c124ce6dacc881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 5 Apr 2021 15:10:28 +0200 Subject: [PATCH 2/6] Allow to manage dot files as usual --- src/Finder.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index 68a6a3216a4..799c6de4b79 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -13,6 +13,7 @@ namespace PhpCsFixer; use Symfony\Component\Finder\Finder as BaseFinder; +use Symfony\Component\Finder\Iterator\LazyIterator; /** * @author Fabien Potencier @@ -27,11 +28,18 @@ public function __construct() $this ->files() ->name('*.php') - ->ignoreDotFiles(false) - ->notPath('~(^|/)\.(?!php_cs(\.dist)?(/|$)).+(/|$)~') - ->name('.php_cs') - ->name('.php_cs.dist') ->exclude('vendor') ; + + // add config files even if dot files are ignored + if (class_exists(LazyIterator::class)) { // LazyIterator class is available since Symfony 4.4 (which requires PHP 7+) + $this->append(new \IteratorIterator(new LazyIterator(function () { + $iterator = clone $this; + $iterator->ignoreDotFiles(false); + $iterator->name('~^\.php_cs(?:\..+)?$~is'); + + return $iterator; + }))); + } } } From fb0c85fd7bf30a769fd36f5354e6241221b2d887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 5 Apr 2021 15:59:41 +0200 Subject: [PATCH 3/6] fix unable to rewind --- src/Finder.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index 799c6de4b79..2c5dbbeefb7 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -13,7 +13,6 @@ namespace PhpCsFixer; use Symfony\Component\Finder\Finder as BaseFinder; -use Symfony\Component\Finder\Iterator\LazyIterator; /** * @author Fabien Potencier @@ -30,16 +29,25 @@ public function __construct() ->name('*.php') ->exclude('vendor') ; + } + public function getIterator() + { // add config files even if dot files are ignored - if (class_exists(LazyIterator::class)) { // LazyIterator class is available since Symfony 4.4 (which requires PHP 7+) - $this->append(new \IteratorIterator(new LazyIterator(function () { - $iterator = clone $this; - $iterator->ignoreDotFiles(false); - $iterator->name('~^\.php_cs(?:\..+)?$~is'); - - return $iterator; - }))); + $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(); } } From 342d09cc48ff213cb5d2423b4fdc2c5532ed9d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 5 Apr 2021 16:39:16 +0200 Subject: [PATCH 4/6] fix fabbot --- tests/Console/ConfigurationResolverTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Console/ConfigurationResolverTest.php b/tests/Console/ConfigurationResolverTest.php index 98f72b7c0ed..df1e9dc4771 100644 --- a/tests/Console/ConfigurationResolverTest.php +++ b/tests/Console/ConfigurationResolverTest.php @@ -205,7 +205,7 @@ public function testResolveConfigFileByPathOfFile() $resolver = $this->createConfigurationResolver(['path' => [$dir.\DIRECTORY_SEPARATOR.'foo.php']]); static::assertSame($dir.\DIRECTORY_SEPARATOR.'.php_cs.dist', $resolver->getConfigFile()); - static::assertInstanceOf('Test1Config', $resolver->getConfig()); + static::assertInstanceOf(\Test1Config::class, $resolver->getConfig()); } public function testResolveConfigFileSpecified() @@ -215,7 +215,7 @@ public function testResolveConfigFileSpecified() $resolver = $this->createConfigurationResolver(['config' => $file]); static::assertSame($file, $resolver->getConfigFile()); - static::assertInstanceOf('Test4Config', $resolver->getConfig()); + static::assertInstanceOf(\Test4Config::class, $resolver->getConfig()); } /** From bb7cfffee039e735c217afe541c8f6183550bf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 5 Apr 2021 16:39:16 +0200 Subject: [PATCH 5/6] Revert "fix fabbot" This reverts commit 342d09cc48ff213cb5d2423b4fdc2c5532ed9d2c. --- tests/Console/ConfigurationResolverTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Console/ConfigurationResolverTest.php b/tests/Console/ConfigurationResolverTest.php index df1e9dc4771..98f72b7c0ed 100644 --- a/tests/Console/ConfigurationResolverTest.php +++ b/tests/Console/ConfigurationResolverTest.php @@ -205,7 +205,7 @@ public function testResolveConfigFileByPathOfFile() $resolver = $this->createConfigurationResolver(['path' => [$dir.\DIRECTORY_SEPARATOR.'foo.php']]); static::assertSame($dir.\DIRECTORY_SEPARATOR.'.php_cs.dist', $resolver->getConfigFile()); - static::assertInstanceOf(\Test1Config::class, $resolver->getConfig()); + static::assertInstanceOf('Test1Config', $resolver->getConfig()); } public function testResolveConfigFileSpecified() @@ -215,7 +215,7 @@ public function testResolveConfigFileSpecified() $resolver = $this->createConfigurationResolver(['config' => $file]); static::assertSame($file, $resolver->getConfigFile()); - static::assertInstanceOf(\Test4Config::class, $resolver->getConfig()); + static::assertInstanceOf('Test4Config', $resolver->getConfig()); } /** From cc3312c35c1cd0b5ec9f05298e866f59e37fad03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 5 Apr 2021 16:53:47 +0200 Subject: [PATCH 6/6] fix phpstan issue --- phpstan.neon | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 18dac5ccec8..81cfe54474a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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