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

#111 - ignore marked files #117

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved

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);
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
$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
);
}
}
Loading