Skip to content

Commit

Permalink
#111 - ignore marked files (#117)
Browse files Browse the repository at this point in the history
* #111 - added ignoring marked files for codestyle configuration

* #111 - readme update

* #111 - cr fix
  • Loading branch information
kamilpiech97 authored Apr 9, 2024
1 parent f39ca63 commit b92da75
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 15 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down
43 changes: 43 additions & 0 deletions tests/codestyle/IgnoreMarkedFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Tests;

use Exception;

class IgnoreMarkedFilesTest extends CodestyleTestCase
{
/**
* @throws Exception
*/
public function testIgnoreMarkedFiles(): void
{
$this->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.",
);
}
}
18 changes: 18 additions & 0 deletions tests/codestyle/config/config.ignoreMarkedFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\Paths;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;

$paths = new Paths("tests/codestyle/tmp");
$rules = new CommonRules();

$config = new Config(
paths: $paths,
rules: $rules->filter(PsrAutoloadingFixer::class),
);

return $config->ignoreMarkedFiles()->config();
16 changes: 16 additions & 0 deletions tests/fixtures/ignoreMarkedFiles/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// php-cs-fixer:ignore-file

class Filter
{
//example comment for ignored file
public static function even($array){
return array_filter($array,
fn (int $i) =>
$i
%
2 === 0
);
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/ignoreMarkedFiles/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// php-cs-fixer:ignore-file

class Filter
{
//example comment for ignored file
public static function even($array){
return array_filter($array,
fn (int $i) =>
$i
%
2 === 0
);
}
}

0 comments on commit b92da75

Please sign in to comment.