Skip to content
Closed
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
8 changes: 2 additions & 6 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php declare(strict_types=1);

use IxDFCodingStandard\PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
->in(__DIR__)
// ->exclude(['vendor', '.cache']) // gitignored paths are already excluded, as well as other typiucal dits like vendor, node_modules, storage, cache, etc.
// Sniff test fixtures are intentionally malformed; reformatting them would shift line numbers and break the sniff tests.
->notPath('#^tests/Sniffs/#');
// Sniff test fixtures are intentionally malformed; reformatting them would shift line numbers and break the sniff tests.
$finder = Config::finder(__DIR__)->notPath('#^tests/Sniffs/#');

// mb_str_functions targets user-facing application strings. This package only processes ASCII PHP tokens,
// so keep the plain byte-string functions and avoid pulling in an ext-mbstring runtime dependency.
Expand Down
9 changes: 6 additions & 3 deletions IxDFCodingStandard/PhpCsFixer/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Config
/** @param array<string, array<string, mixed>|bool> $ruleOverrides Rules to merge on top of the shared ruleset */
public static function create(string $projectDir, array $ruleOverrides = [], ?Finder $finder = null): BaseConfig
{
$finder ??= self::defaultFinder($projectDir);
$finder ??= self::finder($projectDir);

$config = new BaseConfig();
$config
Expand All @@ -32,7 +32,11 @@ public static function create(string $projectDir, array $ruleOverrides = [], ?Fi
return $config;
}

private static function defaultFinder(string $projectDir): Finder
/**
* Default Finder; extend it rather than building one from scratch, so the excludes below still apply.
* PhpCsFixer\Finder lints gitignored files, and only exclude() prunes the walk (ignoreVCSIgnored() filters after descending).
*/
public static function finder(string $projectDir): Finder
{
return Finder::create()
->in($projectDir)
Expand All @@ -41,7 +45,6 @@ private static function defaultFinder(string $projectDir): Finder
'.docker',
'bootstrap/cache',
'node_modules',
'public',
'storage',
'vendor',
])
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ return Config::create(__DIR__, ruleOverrides: []);
<details>
<summary>Customization (optional)</summary>
<code>Config::create()</code> ships a sensible default Finder and enables parallel runs, risky rules, and caching.
You can customize it by using your Finder instance and by overriding individual rules:
To customize the Finder, extend <code>Config::finder()</code> rather than building one from scratch:
it skips gitignored files and keeps heavy directories (<code>.cache</code>, <code>node_modules</code>, <code>storage</code>, <code>vendor</code>) out of the walk.

```php
use PhpCsFixer\Finder;

$finder = Finder::create()->in(__DIR__)->name('*.php');
$finder = Config::finder(__DIR__)
->exclude(['tools'])
->notName('server.php');

// Override individual rules, your own Finder and use your cache path
return Config::create(__DIR__, finder: $finder, ruleOverrides: [
Expand Down
44 changes: 44 additions & 0 deletions tests/PhpCsFixer/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace IxDFCodingStandard\PhpCsFixer;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

#[CoversClass(Config::class)]
final class ConfigTest extends TestCase
{
private string $projectDir;

protected function setUp(): void
{
$this->projectDir = sys_get_temp_dir().'/ixdf-cs-finder-'.bin2hex(random_bytes(4));

$filesystem = new Filesystem();
$filesystem->mkdir($this->projectDir.'/.git');
$filesystem->dumpFile($this->projectDir.'/.gitignore', "/generated/\n");
foreach (['app/Model.php', 'public/index.php', 'generated/Proxy.php', 'storage/framework/Cached.php', 'vendor/Lib.php'] as $file) {
$filesystem->dumpFile($this->projectDir.'/'.$file, "<?php\n");
}
}

protected function tearDown(): void
{
new Filesystem()->remove($this->projectDir);
}

#[Test]
public function default_finder_skips_gitignored_and_excluded_files_but_keeps_public(): void
{
$found = [];
foreach (Config::finder($this->projectDir) as $file) {
$found[] = $file->getRelativePathname();
}

sort($found);

self::assertSame(['app/Model.php', 'public/index.php'], $found);
}
}