forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* 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\ToolInfoInterface; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
/** | ||
* @author Greg Korba <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
#[AsCommand(name: 'check', description: 'Checks if configured files/directories comply with configured rules.')] | ||
final class CheckCommand extends FixCommand | ||
{ | ||
protected static $defaultName = 'check'; | ||
protected static $defaultDescription = 'Checks if configured files/directories comply with configured rules.'; | ||
|
||
public function __construct(ToolInfoInterface $toolInfo) | ||
{ | ||
parent::__construct($toolInfo); | ||
} | ||
|
||
public function getHelp(): string | ||
{ | ||
$help = explode('<comment>--dry-run</comment>', parent::getHelp()); | ||
|
||
return substr($help[0], 0, strrpos($help[0], "\n") - 1) | ||
.substr($help[1], strpos($help[1], "\n")); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
$this->setDefinition([ | ||
...array_values($this->getDefinition()->getArguments()), | ||
...array_values(array_filter( | ||
$this->getDefinition()->getOptions(), | ||
static fn (InputOption $option): bool => 'dry-run' !== $option->getName() | ||
)), | ||
]); | ||
} | ||
|
||
protected function isDryRun(InputInterface $input): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,12 +43,15 @@ | |
* @author Fabien Potencier <[email protected]> | ||
* @author Dariusz Rumiński <[email protected]> | ||
* | ||
* @final | ||
* | ||
* @internal | ||
*/ | ||
#[AsCommand(name: 'fix')] | ||
final class FixCommand extends Command | ||
#[AsCommand(name: 'fix', description: 'Fixes a directory or a file.')] | ||
/* final */ class FixCommand extends Command | ||
{ | ||
protected static $defaultName = 'fix'; | ||
protected static $defaultDescription = 'Fixes a directory or a file.'; | ||
|
||
private EventDispatcherInterface $eventDispatcher; | ||
|
||
|
@@ -82,7 +85,7 @@ public function __construct(ToolInfoInterface $toolInfo) | |
public function getHelp(): string | ||
{ | ||
return <<<'EOF' | ||
The <info>%command.name%</info> command tries to fix as much coding standards | ||
The <info>%command.name%</info> command tries to %command.name% as much coding standards | ||
problems as possible on a given file or files in a given directory and its subdirectories: | ||
<info>$ php %command.full_name% /path/to/dir</info> | ||
|
@@ -159,7 +162,7 @@ public function getHelp(): string | |
<info>$ php %command.full_name% --verbose --show-progress=dots</info> | ||
By using <command>--using-cache</command> option with `yes` or `no` you can set if the caching | ||
By using <comment>--using-cache</comment> option with `yes` or `no` you can set if the caching | ||
mechanism should be used. | ||
The command can also read from standard input, in which case it won't | ||
|
@@ -175,7 +178,7 @@ public function getHelp(): string | |
Exit code | ||
--------- | ||
Exit code of the fix command is built using following bit flags: | ||
Exit code of the `%command.name%` command is built using following bit flags: | ||
* 0 - OK. | ||
* 1 - General error (or PHP minimal requirement not matched). | ||
|
@@ -190,25 +193,22 @@ public function getHelp(): string | |
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDefinition( | ||
[ | ||
new InputArgument('path', InputArgument::IS_ARRAY, 'The path.'), | ||
new InputOption('path-mode', '', InputOption::VALUE_REQUIRED, 'Specify path mode (can be override or intersection).', ConfigurationResolver::PATH_MODE_OVERRIDE), | ||
new InputOption('allow-risky', '', InputOption::VALUE_REQUIRED, 'Are risky fixers allowed (can be yes or no).'), | ||
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php-cs-fixer.php file.'), | ||
new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified.'), | ||
new InputOption('rules', '', InputOption::VALUE_REQUIRED, 'The rules.'), | ||
new InputOption('using-cache', '', InputOption::VALUE_REQUIRED, 'Does cache should be used (can be yes or no).'), | ||
new InputOption('cache-file', '', InputOption::VALUE_REQUIRED, 'The path to the cache file.'), | ||
new InputOption('diff', '', InputOption::VALUE_NONE, 'Also produce diff for each file.'), | ||
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.'), | ||
new InputOption('stop-on-violation', '', InputOption::VALUE_NONE, 'Stop execution on first violation.'), | ||
new InputOption('show-progress', '', InputOption::VALUE_REQUIRED, 'Type of progress indicator (none, dots).'), | ||
] | ||
) | ||
->setDescription('Fixes a directory or a file.') | ||
; | ||
$this->setDefinition( | ||
[ | ||
new InputArgument('path', InputArgument::IS_ARRAY, 'The path(s) that rules will be run against (each path can be a file or directory).'), | ||
new InputOption('path-mode', '', InputOption::VALUE_REQUIRED, 'Specify path mode (can be `override` or `intersection`).', ConfigurationResolver::PATH_MODE_OVERRIDE), | ||
new InputOption('allow-risky', '', InputOption::VALUE_REQUIRED, 'Are risky fixers allowed (can be `yes` or `no`).'), | ||
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a config file.'), | ||
new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified.'), | ||
new InputOption('rules', '', InputOption::VALUE_REQUIRED, 'List of rules that should be run against configured paths.'), | ||
new InputOption('using-cache', '', InputOption::VALUE_REQUIRED, 'Does cache should be used (can be `yes` or `no`).'), | ||
new InputOption('cache-file', '', InputOption::VALUE_REQUIRED, 'The path to the cache file.'), | ||
new InputOption('diff', '', InputOption::VALUE_NONE, 'Prints diff for each file.'), | ||
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.'), | ||
new InputOption('stop-on-violation', '', InputOption::VALUE_NONE, 'Stop execution on first violation.'), | ||
new InputOption('show-progress', '', InputOption::VALUE_REQUIRED, 'Type of progress indicator (none, dots).'), | ||
] | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
|
@@ -227,7 +227,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
[ | ||
'allow-risky' => $input->getOption('allow-risky'), | ||
'config' => $passedConfig, | ||
'dry-run' => $input->getOption('dry-run'), | ||
'dry-run' => $this->isDryRun($input), | ||
'rules' => $passedRules, | ||
'path' => $input->getArgument('path'), | ||
'path-mode' => $input->getOption('path-mode'), | ||
|
@@ -351,4 +351,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
\count($lintErrors) > 0 | ||
); | ||
} | ||
|
||
protected function isDryRun(InputInterface $input): bool | ||
{ | ||
return $input->getOption('dry-run'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* 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\CheckCommand; | ||
use PhpCsFixer\Tests\TestCase; | ||
use PhpCsFixer\ToolInfo; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \PhpCsFixer\Console\Command\CheckCommand | ||
*/ | ||
final class CheckCommandTest extends TestCase | ||
{ | ||
/** | ||
* This test ensures that `--dry-run` option is not available in `check` command, | ||
* because this command is a proxy for `fix` command which always set `--dry-run` during proxying, | ||
* so it does not make sense to provide this option again. | ||
*/ | ||
public function testDryRunModeIsUnavailable(): void | ||
{ | ||
$application = new Application(); | ||
$application->add(new CheckCommand(new ToolInfo())); | ||
|
||
$command = $application->find('check'); | ||
$commandTester = new CommandTester($command); | ||
|
||
$commandTester->execute( | ||
[ | ||
'command' => $command->getName(), | ||
'--help' => true, | ||
'path' => [__FILE__], // just to get rid of Finder error | ||
], | ||
[ | ||
'interactive' => false, | ||
'decorated' => false, | ||
'verbosity' => OutputInterface::VERBOSITY_DEBUG, | ||
] | ||
); | ||
|
||
self::assertStringNotContainsString( | ||
'--dry-run', | ||
$commandTester->getDisplay() | ||
); | ||
} | ||
} |