diff --git a/readme.md b/readme.md index d163bdf..b3084bb 100644 --- a/readme.md +++ b/readme.md @@ -101,6 +101,18 @@ or following to fix found errors: composer csf ``` +#### Additional configuration +If you want to disable risky rules, you can add `withoutRiskyFixers` method to the config file: +```php +return $config->withoutRiskyFixers()->config(); +``` + +If you want to enable ignoring marked file, you can add `ignoreMarkedFiles` method to the config file: +```php +return $config->ignoreMarkedFiles()->config(); +``` +and then add `// php-cs-fixer:ignore-file` to the file which you want to ignore. + #### Upgrading guide Upgrading guide is available in [upgrading.md](./upgrading.md) file. diff --git a/src/Config.php b/src/Config.php index e6f3a00..639ad6f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -21,10 +21,13 @@ class Config { + protected const IGNORE_TAG = "php-cs-fixer:ignore-file"; + protected Paths $paths; protected Rules $rules; protected string $rootPath; protected bool $withRiskyFixers = true; + protected bool $ignoreMarkedFiles = false; public function __construct( ?Paths $paths = null, @@ -47,7 +50,11 @@ public function config(): PhpCsFixerConfig $this->getAllFiles($files, $directory); } - $finder = Finder::create()->directories()->append($files); + $filteredFiles = $this->ignoreMarkedFiles + ? array_filter($files, fn(string $file): bool => !str_contains(file_get_contents($file), self::IGNORE_TAG)) + : $files; + + $finder = Finder::create()->directories()->append($filteredFiles); $config = new PhpCsFixerConfig("Blumilk codestyle standard"); return $config->setFinder($finder) @@ -81,6 +88,13 @@ public function withoutRiskyFixers(): static return $this; } + public function ignoreMarkedFiles(): static + { + $this->ignoreMarkedFiles = true; + + return $this; + } + protected function getAllFiles(array &$paths, string $path): void { if (is_file($path) || !is_dir($path)) { diff --git a/tests/codestyle/IgnoreMarkedFilesTest.php b/tests/codestyle/IgnoreMarkedFilesTest.php new file mode 100644 index 0000000..f8cc662 --- /dev/null +++ b/tests/codestyle/IgnoreMarkedFilesTest.php @@ -0,0 +1,43 @@ +testFixture("ignoreMarkedFiles"); + } + + protected function getConfigPath(): string + { + return "./tests/codestyle/config/config.ignoreMarkedFiles.php"; + } + + /** + * @dataProvider providePhp80Fixtures + * @throws Exception + */ + protected function testFixture(string $name): void + { + copy(__DIR__ . "/../fixtures/{$name}/actual.php", __DIR__ . "/tmp/{$name}.php"); + + $this->assertTrue( + $this->runFixer(fix: true), + "Fixture fixtures/{$name} was not proceeded properly.", + ); + + $this->assertFileEquals( + __DIR__ . "/../fixtures/{$name}/expected.php", + __DIR__ . "/tmp/{$name}.php", + "Result of proceeded fixture fixtures/{$name} is not equal to expected.", + ); + } +} diff --git a/tests/codestyle/config/config.ignoreMarkedFiles.php b/tests/codestyle/config/config.ignoreMarkedFiles.php new file mode 100644 index 0000000..c259b64 --- /dev/null +++ b/tests/codestyle/config/config.ignoreMarkedFiles.php @@ -0,0 +1,18 @@ +filter(PsrAutoloadingFixer::class), +); + +return $config->ignoreMarkedFiles()->config(); diff --git a/tests/fixtures/ignoreMarkedFiles/actual.php b/tests/fixtures/ignoreMarkedFiles/actual.php new file mode 100644 index 0000000..b8ea320 --- /dev/null +++ b/tests/fixtures/ignoreMarkedFiles/actual.php @@ -0,0 +1,16 @@ + + $i + % + 2 === 0 + ); + } +} diff --git a/tests/fixtures/ignoreMarkedFiles/expected.php b/tests/fixtures/ignoreMarkedFiles/expected.php new file mode 100644 index 0000000..b8ea320 --- /dev/null +++ b/tests/fixtures/ignoreMarkedFiles/expected.php @@ -0,0 +1,16 @@ + + $i + % + 2 === 0 + ); + } +}