From 302733d68ca2751e1d9001b908e8bdc080e8c094 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Dec 2020 20:39:07 +0100 Subject: [PATCH 01/28] added a list-files command --- src/Console/Application.php | 2 + src/Console/Command/ListFilesCommand.php | 114 +++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/Console/Command/ListFilesCommand.php diff --git a/src/Console/Application.php b/src/Console/Application.php index c1154430b8b..714ccb40f5b 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -15,6 +15,7 @@ use PhpCsFixer\Console\Command\DescribeCommand; use PhpCsFixer\Console\Command\FixCommand; use PhpCsFixer\Console\Command\HelpCommand; +use PhpCsFixer\Console\Command\ListFilesCommand; use PhpCsFixer\Console\Command\SelfUpdateCommand; use PhpCsFixer\Console\SelfUpdate\GithubClient; use PhpCsFixer\Console\SelfUpdate\NewVersionChecker; @@ -52,6 +53,7 @@ public function __construct() $this->toolInfo = new ToolInfo(); + $this->add(new ListFilesCommand($this->toolInfo)); $this->add(new DescribeCommand()); $this->add(new FixCommand($this->toolInfo)); $this->add(new SelfUpdateCommand( diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php new file mode 100644 index 00000000000..23910631087 --- /dev/null +++ b/src/Console/Command/ListFilesCommand.php @@ -0,0 +1,114 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Console\Command; + +use PhpCsFixer\Config; +use PhpCsFixer\ConfigInterface; +use PhpCsFixer\Console\ConfigurationResolver; +use PhpCsFixer\Console\Output\ErrorOutput; +use PhpCsFixer\Console\Output\NullOutput; +use PhpCsFixer\Console\Output\ProcessOutput; +use PhpCsFixer\Error\ErrorsManager; +use PhpCsFixer\Report\ReportSummary; +use PhpCsFixer\Runner\Runner; +use PhpCsFixer\ToolInfoInterface; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\ConsoleOutputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Terminal; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Stopwatch\Stopwatch; + +/** + * @author Fabien Potencier + * @author Dariusz Rumiński + * + * @internal + */ +final class ListFilesCommand extends Command +{ + protected static $defaultName = 'list-files'; + + + /** + * @var ConfigInterface + */ + private $defaultConfig; + + /** + * @var ToolInfoInterface + */ + private $toolInfo; + + public function __construct(ToolInfoInterface $toolInfo) + { + parent::__construct(); + + $this->defaultConfig = new Config(); + $this->toolInfo = $toolInfo; + } + + /** + * {@inheritdoc} + * + * Override here to only generate the help copy when used. + */ + public function getHelp() + { + return HelpCommand::getHelpCopy(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setDefinition( + [ + new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php_cs file.'), + ] + ) + ->setDescription('List all files beeing fixed by the given config.') + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $passedConfig = $input->getOption('config'); + + $resolver = new ConfigurationResolver( + $this->defaultConfig, + [ + 'config' => $passedConfig, + ], + getcwd(), + $this->toolInfo + ); + + $finder = $resolver->getFinder(); + + foreach ($finder as $file) { + $output->writeln(escapeshellarg($file->getRealPath())); + } + + return 0; + } +} From 149d757fc062137a91ec84fb7a23566911f8a761 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Dec 2020 20:50:59 +0100 Subject: [PATCH 02/28] removed un-used imports --- src/Console/Command/ListFilesCommand.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 23910631087..a7d760dd1c7 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -15,12 +15,6 @@ use PhpCsFixer\Config; use PhpCsFixer\ConfigInterface; use PhpCsFixer\Console\ConfigurationResolver; -use PhpCsFixer\Console\Output\ErrorOutput; -use PhpCsFixer\Console\Output\NullOutput; -use PhpCsFixer\Console\Output\ProcessOutput; -use PhpCsFixer\Error\ErrorsManager; -use PhpCsFixer\Report\ReportSummary; -use PhpCsFixer\Runner\Runner; use PhpCsFixer\ToolInfoInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; From 2a25c8c0e617a35013e371744f7e8c789c7f8259 Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Mon, 28 Dec 2020 09:31:47 +0100 Subject: [PATCH 03/28] alphabetical order --- src/Console/Application.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Console/Application.php b/src/Console/Application.php index 714ccb40f5b..b5b7f8e56cd 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -53,9 +53,10 @@ public function __construct() $this->toolInfo = new ToolInfo(); - $this->add(new ListFilesCommand($this->toolInfo)); + // in alphabetical order $this->add(new DescribeCommand()); $this->add(new FixCommand($this->toolInfo)); + $this->add(new ListFilesCommand($this->toolInfo)); $this->add(new SelfUpdateCommand( new NewVersionChecker(new GithubClient()), $this->toolInfo, From 8c87ac9188de268fc377d3007df58bccc77372f3 Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Mon, 28 Dec 2020 09:33:45 +0100 Subject: [PATCH 04/28] Update src/Console/Command/ListFilesCommand.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andreas Möller --- src/Console/Command/ListFilesCommand.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index a7d760dd1c7..7d38181af6b 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -81,9 +81,6 @@ protected function configure() ; } - /** - * {@inheritdoc} - */ protected function execute(InputInterface $input, OutputInterface $output) { $passedConfig = $input->getOption('config'); From 22315d93c9ec3f37d86d2a26b137e3f505286bf4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Jan 2021 11:13:37 +0100 Subject: [PATCH 05/28] remove getHelp(), seems to be only used for the FixCommand --- src/Console/Command/ListFilesCommand.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 7d38181af6b..1dae4373577 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -56,16 +56,6 @@ public function __construct(ToolInfoInterface $toolInfo) $this->toolInfo = $toolInfo; } - /** - * {@inheritdoc} - * - * Override here to only generate the help copy when used. - */ - public function getHelp() - { - return HelpCommand::getHelpCopy(); - } - /** * {@inheritdoc} */ From 44bbe9284b9b5beeae5f8da0b7f148551aff1ed6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Jan 2021 11:22:18 +0100 Subject: [PATCH 06/28] use relative paths instead of real-path to shorten output --- src/Console/Command/ListFilesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 1dae4373577..6196662c0a8 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $resolver->getFinder(); foreach ($finder as $file) { - $output->writeln(escapeshellarg($file->getRealPath())); + $output->writeln(escapeshellarg($file->getPath())); } return 0; From 1f6f32b727c62d1496afdd0d8b514960de1c2631 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Jan 2021 11:49:40 +0100 Subject: [PATCH 07/28] rm un-used imports --- src/Console/Command/ListFilesCommand.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 6196662c0a8..cba38e788dc 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -17,19 +17,12 @@ use PhpCsFixer\Console\ConfigurationResolver; use PhpCsFixer\ToolInfoInterface; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Terminal; -use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Stopwatch\Stopwatch; /** - * @author Fabien Potencier - * @author Dariusz Rumiński + * @author Markus Staab * * @internal */ From 5d2104d661186b72aafc445b11758ff1a612827d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Jan 2021 12:44:33 +0100 Subject: [PATCH 08/28] added ListFilesCommandTest --- src/Console/Command/ListFilesCommand.php | 5 +- .../Console/Command/ListFilesCommandTest.php | 53 +++++++++++++++++++ tests/Console/Command/test-project/.php_cs | 18 +++++++ .../Command/test-project/needs-fixing.php | 8 +++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/Console/Command/ListFilesCommandTest.php create mode 100644 tests/Console/Command/test-project/.php_cs create mode 100644 tests/Console/Command/test-project/needs-fixing.php diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index cba38e788dc..cda21bddca3 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -79,8 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $resolver->getFinder(); + /** @var \SplFileInfo $file */ foreach ($finder as $file) { - $output->writeln(escapeshellarg($file->getPath())); + if ($file->isFile()) { + $output->writeln(escapeshellarg($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename())); + } } return 0; diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php new file mode 100644 index 00000000000..335050b3ca1 --- /dev/null +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -0,0 +1,53 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Console\Command; + +use PhpCsFixer\Console\Application; +use PhpCsFixer\Console\Command\ListFilesCommand; +use PhpCsFixer\Tests\TestCase; +use PhpCsFixer\ToolInfo; +use Symfony\Component\Console\Tester\CommandTester; + +/** + * @internal + * + * @covers \PhpCsFixer\Console\Command\ListFilesCommand + */ +final class ListFilesCommandTest extends TestCase +{ + public function testEmptyRulesValue() + { + $commandTester = $this->doTestExecute(); + // TODO adjust expectation + $this->assertSame('', $commandTester->getDisplay()); + } + + /** + * @return CommandTester + */ + private function doTestExecute(/*array $arguments*/) + { + $application = new Application(); + $application->add(new ListFilesCommand(new ToolInfo())); + + $command = $application->find('list-files'); + $commandTester = new CommandTester($command); + + $commandTester->execute([ + '--config' => __DIR__.'/test-project/.php_cs' + ] + ); + + return $commandTester; + } +} diff --git a/tests/Console/Command/test-project/.php_cs b/tests/Console/Command/test-project/.php_cs new file mode 100644 index 00000000000..01006b10cad --- /dev/null +++ b/tests/Console/Command/test-project/.php_cs @@ -0,0 +1,18 @@ +in([ + __DIR__ + ]) + ->exclude([ + ]) +; + +return PhpCsFixer\Config::create() + ->setUsingCache(false) + ->setRules([ + '@Symfony' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder($finder) + ; diff --git a/tests/Console/Command/test-project/needs-fixing.php b/tests/Console/Command/test-project/needs-fixing.php new file mode 100644 index 00000000000..b26f1e37b13 --- /dev/null +++ b/tests/Console/Command/test-project/needs-fixing.php @@ -0,0 +1,8 @@ + Date: Thu, 14 Jan 2021 12:52:06 +0100 Subject: [PATCH 09/28] fix test --- src/Console/Command/ListFilesCommand.php | 5 +++-- tests/Console/Command/ListFilesCommandTest.php | 16 +++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index cda21bddca3..78c258dfa72 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\SplFileInfo; /** * @author Markus Staab @@ -79,10 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $resolver->getFinder(); - /** @var \SplFileInfo $file */ + /** @var SplFileInfo $file */ foreach ($finder as $file) { if ($file->isFile()) { - $output->writeln(escapeshellarg($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename())); + $output->writeln(escapeshellarg($file->getRelativePathname())); } } diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index 335050b3ca1..88e419079e9 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -25,17 +25,18 @@ */ final class ListFilesCommandTest extends TestCase { - public function testEmptyRulesValue() + public function testListWithConfig() { - $commandTester = $this->doTestExecute(); - // TODO adjust expectation - $this->assertSame('', $commandTester->getDisplay()); + $commandTester = $this->doTestExecute([ + '--config' => __DIR__.'/test-project/.php_cs' + ]); + $this->assertSame('"needs-fixing.php"', $commandTester->getDisplay()); } /** * @return CommandTester */ - private function doTestExecute(/*array $arguments*/) + private function doTestExecute(array $arguments) { $application = new Application(); $application->add(new ListFilesCommand(new ToolInfo())); @@ -43,10 +44,7 @@ private function doTestExecute(/*array $arguments*/) $command = $application->find('list-files'); $commandTester = new CommandTester($command); - $commandTester->execute([ - '--config' => __DIR__.'/test-project/.php_cs' - ] - ); + $commandTester->execute($arguments); return $commandTester; } From 7e92ca7660a231ca1794d9fcd4586a03e4573627 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Jan 2021 12:55:44 +0100 Subject: [PATCH 10/28] fix test-expectation --- tests/Console/Command/ListFilesCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index 88e419079e9..44ceb5e67a7 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -30,7 +30,7 @@ public function testListWithConfig() $commandTester = $this->doTestExecute([ '--config' => __DIR__.'/test-project/.php_cs' ]); - $this->assertSame('"needs-fixing.php"', $commandTester->getDisplay()); + $this->assertSame(escapeshellarg('needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); } /** From 444500c61ff8c5c4b60b9724960d8eda5c4f72a5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 10:45:34 +0200 Subject: [PATCH 11/28] docs --- doc/usage.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/usage.rst b/doc/usage.rst index 7e9c00ea783..db6ac6e41cc 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -2,6 +2,9 @@ Usage ===== +The ``fix`` command +------------------- + The ``fix`` command tries to fix as much coding standards problems as possible on a given file or files in a given directory and its subdirectories: @@ -121,6 +124,33 @@ fixed but without actually modifying them: By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching mechanism should be used. +The ``list-files`` command +-------------------------- + +The ``list-files`` command will list all files which need fixing. + +.. code-block:: console + + $ php php-cs-fixer.phar list-files + +The ``--config`` option can be used, like in the ``fix`` command, to tell from which path a config file should be loaded + +.. code-block:: console + + $ php php-cs-fixer.phar list-files --config=.php_cs.dist + +The output is build in a form that its easy to use in combination with ``xargs`` command in a linux pipe. +This can be useful e.g. in situations where the caching might mechanism not available (CI, Docker) and distributing +fixing across several processes might speedup the process. + +.. code-block:: console + + $ php php-cs-fixer.phar list-files | xargs -n 10 -P 8 php-cs-fixer fix + +* `-n` defines how many files a single subprocess process +* `-P` defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has) + + Rule descriptions ----------------- From 725cc86de5ebb84189eeb58d835a6bf37a25091c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 11:03:50 +0200 Subject: [PATCH 12/28] added separate path for SplFileInfo and sfSplFileInfo --- src/Console/Command/ListFilesCommand.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 78c258dfa72..ae2a1c0f756 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -20,7 +20,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Finder\SplFileInfo; +use Symfony\Component\Finder\SplFileInfo as sfSplFileInfo; +use SplFileInfo; /** * @author Markus Staab @@ -31,7 +32,6 @@ final class ListFilesCommand extends Command { protected static $defaultName = 'list-files'; - /** * @var ConfigInterface */ @@ -58,7 +58,7 @@ protected function configure() $this ->setDefinition( [ - new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php_cs file.'), + new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php-cs-fixer.php file.'), ] ) ->setDescription('List all files beeing fixed by the given config.') @@ -80,10 +80,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $resolver->getFinder(); - /** @var SplFileInfo $file */ + /** @var SplFileInfo|sfSplFileInfo $file */ foreach ($finder as $file) { - if ($file->isFile()) { - $output->writeln(escapeshellarg($file->getRelativePathname())); + if ($file instanceof sfSplFileInfo) { + if ($file->isFile()) { + $output->writeln(escapeshellarg($file->getRelativePathname())); + } + } else if ($file instanceof SplFileInfo) { + if ($file->isFile()) { + $output->writeln(escapeshellarg($file->getPathname())); + } } } From 4e93714d09e8fbef565e1f88ca89df7850922c5f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 11:07:26 +0200 Subject: [PATCH 13/28] moved ListFilesTest project into test/Fixtures/ --- tests/Console/Command/ListFilesCommandTest.php | 2 +- .../Command/test-project => Fixtures/ListFilesTest}/.php_cs | 0 .../test-project => Fixtures/ListFilesTest}/needs-fixing.php | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename tests/{Console/Command/test-project => Fixtures/ListFilesTest}/.php_cs (100%) rename tests/{Console/Command/test-project => Fixtures/ListFilesTest}/needs-fixing.php (100%) diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index 44ceb5e67a7..b399050d42a 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -28,7 +28,7 @@ final class ListFilesCommandTest extends TestCase public function testListWithConfig() { $commandTester = $this->doTestExecute([ - '--config' => __DIR__.'/test-project/.php_cs' + '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php_cs' ]); $this->assertSame(escapeshellarg('needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); } diff --git a/tests/Console/Command/test-project/.php_cs b/tests/Fixtures/ListFilesTest/.php_cs similarity index 100% rename from tests/Console/Command/test-project/.php_cs rename to tests/Fixtures/ListFilesTest/.php_cs diff --git a/tests/Console/Command/test-project/needs-fixing.php b/tests/Fixtures/ListFilesTest/needs-fixing.php similarity index 100% rename from tests/Console/Command/test-project/needs-fixing.php rename to tests/Fixtures/ListFilesTest/needs-fixing.php From 925f47c50cbcafad5f38c47df4c2a71c83a95dcd Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 11:14:04 +0200 Subject: [PATCH 14/28] added in/out test --- tests/Console/Command/ListFilesCommandTest.php | 2 +- tests/Fixtures/ListFilesTest/.php_cs | 3 ++- .../ListFilesTest/{ => excluded}/needs-fixing.php | 0 .../Fixtures/ListFilesTest/needs-fixing/needs-fixing.php | 8 ++++++++ 4 files changed, 11 insertions(+), 2 deletions(-) rename tests/Fixtures/ListFilesTest/{ => excluded}/needs-fixing.php (100%) create mode 100644 tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index b399050d42a..c5ba787921c 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -30,7 +30,7 @@ public function testListWithConfig() $commandTester = $this->doTestExecute([ '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php_cs' ]); - $this->assertSame(escapeshellarg('needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); + $this->assertSame(escapeshellarg('needs-fixing/needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); } /** diff --git a/tests/Fixtures/ListFilesTest/.php_cs b/tests/Fixtures/ListFilesTest/.php_cs index 01006b10cad..6e2aab3ca65 100644 --- a/tests/Fixtures/ListFilesTest/.php_cs +++ b/tests/Fixtures/ListFilesTest/.php_cs @@ -2,9 +2,10 @@ $finder = PhpCsFixer\Finder::create() ->in([ - __DIR__ + __DIR__.'/needs-fixing/' ]) ->exclude([ + __DIR__.'/excluded/' ]) ; diff --git a/tests/Fixtures/ListFilesTest/needs-fixing.php b/tests/Fixtures/ListFilesTest/excluded/needs-fixing.php similarity index 100% rename from tests/Fixtures/ListFilesTest/needs-fixing.php rename to tests/Fixtures/ListFilesTest/excluded/needs-fixing.php diff --git a/tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php b/tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php new file mode 100644 index 00000000000..b26f1e37b13 --- /dev/null +++ b/tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php @@ -0,0 +1,8 @@ + Date: Thu, 15 Apr 2021 11:28:38 +0200 Subject: [PATCH 15/28] .php_cs -> .php-cs-fixer.php --- tests/Console/Command/ListFilesCommandTest.php | 2 +- tests/Fixtures/ListFilesTest/{.php_cs => .php-cs-fixer.php} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/Fixtures/ListFilesTest/{.php_cs => .php-cs-fixer.php} (100%) diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index c5ba787921c..9f7f9d5e0bc 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -28,7 +28,7 @@ final class ListFilesCommandTest extends TestCase public function testListWithConfig() { $commandTester = $this->doTestExecute([ - '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php_cs' + '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php-cs-fixer.php' ]); $this->assertSame(escapeshellarg('needs-fixing/needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); } diff --git a/tests/Fixtures/ListFilesTest/.php_cs b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php similarity index 100% rename from tests/Fixtures/ListFilesTest/.php_cs rename to tests/Fixtures/ListFilesTest/.php-cs-fixer.php From 06beee5411432b8af5f3b737753ca892078e906b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 11:34:48 +0200 Subject: [PATCH 16/28] fix test --- src/Console/Command/ListFilesCommand.php | 15 +++++---------- tests/Console/Command/ListFilesCommandTest.php | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index ae2a1c0f756..36fe03b2d96 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -20,7 +20,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Finder\SplFileInfo as sfSplFileInfo; use SplFileInfo; /** @@ -68,6 +67,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $passedConfig = $input->getOption('config'); + $cwd = getcwd(); $resolver = new ConfigurationResolver( $this->defaultConfig, @@ -80,16 +80,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $resolver->getFinder(); - /** @var SplFileInfo|sfSplFileInfo $file */ + /** @var SplFileInfo $file */ foreach ($finder as $file) { - if ($file instanceof sfSplFileInfo) { - if ($file->isFile()) { - $output->writeln(escapeshellarg($file->getRelativePathname())); - } - } else if ($file instanceof SplFileInfo) { - if ($file->isFile()) { - $output->writeln(escapeshellarg($file->getPathname())); - } + if ($file->isFile()) { + $relativePath = str_replace($cwd, '.', $file->getPathname()); + $output->writeln(escapeshellarg($relativePath)); } } diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index 9f7f9d5e0bc..76bdafc4335 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -30,7 +30,7 @@ public function testListWithConfig() $commandTester = $this->doTestExecute([ '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php-cs-fixer.php' ]); - $this->assertSame(escapeshellarg('needs-fixing/needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); + $this->assertSame(escapeshellarg('./tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); } /** From d9dbec3458344bb17cdd3099d1808b402dd3a76c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 12:06:43 +0200 Subject: [PATCH 17/28] try to fix windows compat --- tests/Console/Command/ListFilesCommandTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index 76bdafc4335..a2fbb61243f 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -30,7 +30,12 @@ public function testListWithConfig() $commandTester = $this->doTestExecute([ '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php-cs-fixer.php' ]); - $this->assertSame(escapeshellarg('./tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php').PHP_EOL, $commandTester->getDisplay()); + + $expectedPath = './tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php'; + // make the test also work on windows + $expectedPath = str_replace('/', DIRECTORY_SEPARATOR, $expectedPath); + + $this->assertSame(escapeshellarg($expectedPath).PHP_EOL, $commandTester->getDisplay()); } /** From 3f6a25571f92d6639487cc15067e9a4ab251b551 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 16:26:59 +0200 Subject: [PATCH 18/28] fix CS --- tests/Fixtures/ListFilesTest/.php-cs-fixer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php index 6e2aab3ca65..4f1964371b0 100644 --- a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php +++ b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php @@ -2,17 +2,17 @@ $finder = PhpCsFixer\Finder::create() ->in([ - __DIR__.'/needs-fixing/' + __DIR__.'/needs-fixing/', ]) ->exclude([ - __DIR__.'/excluded/' + __DIR__.'/excluded/', ]) ; return PhpCsFixer\Config::create() ->setUsingCache(false) ->setRules([ - '@Symfony' => true, + '@Symfony' => true, ]) ->setRiskyAllowed(true) ->setFinder($finder) From 172d577f2293f3c95a11b3b6e23477573c7148a2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 16:29:12 +0200 Subject: [PATCH 19/28] fix deprecation --- tests/Fixtures/ListFilesTest/.php-cs-fixer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php index 4f1964371b0..3eb3b135f43 100644 --- a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php +++ b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php @@ -9,7 +9,8 @@ ]) ; -return PhpCsFixer\Config::create() +$config = PhpCsFixer\Config(); +return $config ->setUsingCache(false) ->setRules([ '@Symfony' => true, From 47acf3a1cd112bc063613d859a535e19a55ffd69 Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Thu, 15 Apr 2021 16:27:48 +0200 Subject: [PATCH 20/28] Update doc/usage.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Rumiński --- doc/usage.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/usage.rst b/doc/usage.rst index db6ac6e41cc..045743c7005 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -133,7 +133,7 @@ The ``list-files`` command will list all files which need fixing. $ php php-cs-fixer.phar list-files -The ``--config`` option can be used, like in the ``fix`` command, to tell from which path a config file should be loaded +The ``--config`` option can be used, like in the ``fix`` command, to tell from which path a config file should be loaded. .. code-block:: console From d107a1f79ea37fe0d141532b74583619f5909c2a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 15 Apr 2021 17:40:30 +0200 Subject: [PATCH 21/28] fix typo --- tests/Fixtures/ListFilesTest/.php-cs-fixer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php index 3eb3b135f43..60058c0fb71 100644 --- a/tests/Fixtures/ListFilesTest/.php-cs-fixer.php +++ b/tests/Fixtures/ListFilesTest/.php-cs-fixer.php @@ -9,7 +9,7 @@ ]) ; -$config = PhpCsFixer\Config(); +$config = new PhpCsFixer\Config(); return $config ->setUsingCache(false) ->setRules([ From d3f420da2c387117f0fbf9135c72d396429eed91 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 16 Apr 2021 09:12:08 +0200 Subject: [PATCH 22/28] fis CS --- src/Console/Command/ListFilesCommand.php | 2 +- tests/Console/Command/ListFilesCommandTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 36fe03b2d96..6a80e880c72 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -16,11 +16,11 @@ use PhpCsFixer\ConfigInterface; use PhpCsFixer\Console\ConfigurationResolver; use PhpCsFixer\ToolInfoInterface; +use SplFileInfo; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use SplFileInfo; /** * @author Markus Staab diff --git a/tests/Console/Command/ListFilesCommandTest.php b/tests/Console/Command/ListFilesCommandTest.php index a2fbb61243f..3b7f713949f 100644 --- a/tests/Console/Command/ListFilesCommandTest.php +++ b/tests/Console/Command/ListFilesCommandTest.php @@ -28,14 +28,14 @@ final class ListFilesCommandTest extends TestCase public function testListWithConfig() { $commandTester = $this->doTestExecute([ - '--config' => __DIR__ . '/../../Fixtures/ListFilesTest/.php-cs-fixer.php' + '--config' => __DIR__.'/../../Fixtures/ListFilesTest/.php-cs-fixer.php', ]); $expectedPath = './tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php'; // make the test also work on windows - $expectedPath = str_replace('/', DIRECTORY_SEPARATOR, $expectedPath); + $expectedPath = str_replace('/', \DIRECTORY_SEPARATOR, $expectedPath); - $this->assertSame(escapeshellarg($expectedPath).PHP_EOL, $commandTester->getDisplay()); + static::assertSame(escapeshellarg($expectedPath).PHP_EOL, $commandTester->getDisplay()); } /** From add9542df5f9212f0812f4865673696919db0689 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 16 Apr 2021 09:14:41 +0200 Subject: [PATCH 23/28] to provide multiple files to fix command, you need to specify config file --- doc/usage.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/usage.rst b/doc/usage.rst index 045743c7005..32118beabe4 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -143,9 +143,11 @@ The output is build in a form that its easy to use in combination with ``xargs`` This can be useful e.g. in situations where the caching might mechanism not available (CI, Docker) and distributing fixing across several processes might speedup the process. +Note: You need to pass the config to the ``fix`` command, in order to make it work with several files beeing passed by ``list-files``. + .. code-block:: console - $ php php-cs-fixer.phar list-files | xargs -n 10 -P 8 php-cs-fixer fix + $ php php-cs-fixer.phar list-files --config=.php_cs.dist | xargs -n 10 -P 8 php-cs-fixer fix --config=.php_cs.dist * `-n` defines how many files a single subprocess process * `-P` defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has) From a32a8ff9b3b9c4894fd3f2d64653bdabed9f45f2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 16 Apr 2021 10:54:03 +0200 Subject: [PATCH 24/28] fix typo --- doc/usage.rst | 2 +- src/Console/Command/ListFilesCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/usage.rst b/doc/usage.rst index 32118beabe4..990644c5db4 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -143,7 +143,7 @@ The output is build in a form that its easy to use in combination with ``xargs`` This can be useful e.g. in situations where the caching might mechanism not available (CI, Docker) and distributing fixing across several processes might speedup the process. -Note: You need to pass the config to the ``fix`` command, in order to make it work with several files beeing passed by ``list-files``. +Note: You need to pass the config to the ``fix`` command, in order to make it work with several files being passed by ``list-files``. .. code-block:: console diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 6a80e880c72..4428fe6c7bd 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -60,7 +60,7 @@ protected function configure() new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php-cs-fixer.php file.'), ] ) - ->setDescription('List all files beeing fixed by the given config.') + ->setDescription('List all files being fixed by the given config.') ; } From a3ea03a2499638c038c9f9c7e957e9fb365bf162 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 16 Apr 2021 12:22:48 +0200 Subject: [PATCH 25/28] fix test on windows --- src/Console/Command/ListFilesCommand.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index 4428fe6c7bd..b0d8cd6a114 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -84,6 +84,9 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($finder as $file) { if ($file->isFile()) { $relativePath = str_replace($cwd, '.', $file->getPathname()); + // unify directory separators across operating system + $relativePath = str_replace('/', \DIRECTORY_SEPARATOR, $relativePath); + $output->writeln(escapeshellarg($relativePath)); } } From 81753b456fc73bc810d105c250bc59f37e07e2e1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 16 Apr 2021 13:40:29 +0200 Subject: [PATCH 26/28] try getRealPath() --- src/Console/Command/ListFilesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php index b0d8cd6a114..4913ed18864 100644 --- a/src/Console/Command/ListFilesCommand.php +++ b/src/Console/Command/ListFilesCommand.php @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var SplFileInfo $file */ foreach ($finder as $file) { if ($file->isFile()) { - $relativePath = str_replace($cwd, '.', $file->getPathname()); + $relativePath = str_replace($cwd, '.', $file->getRealPath()); // unify directory separators across operating system $relativePath = str_replace('/', \DIRECTORY_SEPARATOR, $relativePath); From c95d4301e002feb6203ae18d51ab1c72bb4add2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20Rumi=C5=84ski?= Date: Sat, 17 Apr 2021 14:05:28 +0200 Subject: [PATCH 27/28] Update doc/usage.rst --- doc/usage.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/usage.rst b/doc/usage.rst index 990644c5db4..8aabd15f555 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -137,7 +137,7 @@ The ``--config`` option can be used, like in the ``fix`` command, to tell from w .. code-block:: console - $ php php-cs-fixer.phar list-files --config=.php_cs.dist + $ php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php The output is build in a form that its easy to use in combination with ``xargs`` command in a linux pipe. This can be useful e.g. in situations where the caching might mechanism not available (CI, Docker) and distributing From ed65a9efe82666176cceec3d54ed427c02505896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20Rumi=C5=84ski?= Date: Sat, 17 Apr 2021 14:05:36 +0200 Subject: [PATCH 28/28] Update doc/usage.rst --- doc/usage.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/usage.rst b/doc/usage.rst index 8aabd15f555..37437b0d7a8 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -147,7 +147,7 @@ Note: You need to pass the config to the ``fix`` command, in order to make it wo .. code-block:: console - $ php php-cs-fixer.phar list-files --config=.php_cs.dist | xargs -n 10 -P 8 php-cs-fixer fix --config=.php_cs.dist + $ php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v * `-n` defines how many files a single subprocess process * `-P` defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has)