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

feature #4024 added a list-files command #5390

Merged
merged 28 commits into from Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
302733d
added a list-files command
clxmstaab Dec 23, 2020
149d757
removed un-used imports
clxmstaab Dec 23, 2020
2a25c8c
alphabetical order
clxmstaab Dec 28, 2020
8c87ac9
Update src/Console/Command/ListFilesCommand.php
clxmstaab Dec 28, 2020
22315d9
remove getHelp(), seems to be only used for the FixCommand
clxmstaab Jan 14, 2021
44bbe92
use relative paths instead of real-path to shorten output
clxmstaab Jan 14, 2021
1f6f32b
rm un-used imports
clxmstaab Jan 14, 2021
5d2104d
added ListFilesCommandTest
clxmstaab Jan 14, 2021
2f9cd68
fix test
clxmstaab Jan 14, 2021
7e92ca7
fix test-expectation
clxmstaab Jan 14, 2021
444500c
docs
clxmstaab Apr 15, 2021
725cc86
added separate path for SplFileInfo and sfSplFileInfo
clxmstaab Apr 15, 2021
4e93714
moved ListFilesTest project into test/Fixtures/
clxmstaab Apr 15, 2021
925f47c
added in/out test
clxmstaab Apr 15, 2021
ac8773d
.php_cs -> .php-cs-fixer.php
clxmstaab Apr 15, 2021
06beee5
fix test
clxmstaab Apr 15, 2021
d9dbec3
try to fix windows compat
clxmstaab Apr 15, 2021
3f6a255
fix CS
clxmstaab Apr 15, 2021
172d577
fix deprecation
clxmstaab Apr 15, 2021
47acf3a
Update doc/usage.rst
clxmstaab Apr 15, 2021
d107a1f
fix typo
clxmstaab Apr 15, 2021
d3f420d
fis CS
clxmstaab Apr 16, 2021
add9542
to provide multiple files to fix command, you need to specify config …
clxmstaab Apr 16, 2021
a32a8ff
fix typo
clxmstaab Apr 16, 2021
a3ea03a
fix test on windows
clxmstaab Apr 16, 2021
81753b4
try getRealPath()
clxmstaab Apr 16, 2021
c95d430
Update doc/usage.rst
keradus Apr 17, 2021
ed65a9e
Update doc/usage.rst
keradus Apr 17, 2021
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
32 changes: 32 additions & 0 deletions doc/usage.rst
Expand Up @@ -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:

Expand Down Expand Up @@ -121,6 +124,35 @@ 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
keradus marked this conversation as resolved.
Show resolved Hide resolved

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.

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

$ php php-cs-fixer.phar list-files --config=.php_cs.dist | xargs -n 10 -P 8 php-cs-fixer fix --config=.php_cs.dist
keradus marked this conversation as resolved.
Show resolved Hide resolved

* `-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
-----------------

Expand Down
3 changes: 3 additions & 0 deletions src/Console/Application.php
Expand Up @@ -15,6 +15,7 @@
use PhpCsFixer\Console\Command\DescribeCommand;
clxmstaab marked this conversation as resolved.
Show resolved Hide resolved
use PhpCsFixer\Console\Command\FixCommand;
use PhpCsFixer\Console\Command\HelpCommand;
use PhpCsFixer\Console\Command\ListFilesCommand;
clxmstaab marked this conversation as resolved.
Show resolved Hide resolved
use PhpCsFixer\Console\Command\SelfUpdateCommand;
use PhpCsFixer\Console\SelfUpdate\GithubClient;
use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
Expand Down Expand Up @@ -52,8 +53,10 @@ public function __construct()

$this->toolInfo = new 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,
Expand Down
96 changes: 96 additions & 0 deletions src/Console/Command/ListFilesCommand.php
@@ -0,0 +1,96 @@
<?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.
*/

namespace PhpCsFixer\Console\Command;

use PhpCsFixer\Config;
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;

/**
* @author Markus Staab <markus.staab@redaxo.org>
*
* @internal
*/
final class ListFilesCommand extends Command
clxmstaab marked this conversation as resolved.
Show resolved Hide resolved
{
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}
*/
protected function configure()
{
$this
->setDefinition(
clxmstaab marked this conversation as resolved.
Show resolved Hide resolved
[
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php-cs-fixer.php file.'),
]
)
->setDescription('List all files being fixed by the given config.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$passedConfig = $input->getOption('config');
$cwd = getcwd();

$resolver = new ConfigurationResolver(
$this->defaultConfig,
[
'config' => $passedConfig,
],
getcwd(),
$this->toolInfo
);

$finder = $resolver->getFinder();

/** @var SplFileInfo $file */
foreach ($finder as $file) {
if ($file->isFile()) {
$relativePath = str_replace($cwd, '.', $file->getRealPath());
// unify directory separators across operating system
$relativePath = str_replace('/', \DIRECTORY_SEPARATOR, $relativePath);

$output->writeln(escapeshellarg($relativePath));
}
}

return 0;
}
}
56 changes: 56 additions & 0 deletions tests/Console/Command/ListFilesCommandTest.php
@@ -0,0 +1,56 @@
<?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.
*/

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 testListWithConfig()
{
$commandTester = $this->doTestExecute([
'--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);

static::assertSame(escapeshellarg($expectedPath).PHP_EOL, $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($arguments);

return $commandTester;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/ListFilesTest/.php-cs-fixer.php
@@ -0,0 +1,20 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__.'/needs-fixing/',
])
->exclude([
__DIR__.'/excluded/',
])
;

$config = new PhpCsFixer\Config();
return $config
->setUsingCache(false)
->setRules([
'@Symfony' => true,
])
->setRiskyAllowed(true)
->setFinder($finder)
;
8 changes: 8 additions & 0 deletions tests/Fixtures/ListFilesTest/excluded/needs-fixing.php
@@ -0,0 +1,8 @@
<?php

function abc() {}

function def()
{

}
8 changes: 8 additions & 0 deletions tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php
@@ -0,0 +1,8 @@
<?php

function abc() {}

function def()
{

}