-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsFixerConfigGenerator.php
More file actions
84 lines (66 loc) · 2.24 KB
/
CsFixerConfigGenerator.php
File metadata and controls
84 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\Configuration;
use KaririCode\Devkit\Contract\ConfigGenerator;
use KaririCode\Devkit\Core\ProjectContext;
/**
* Generates `.kcode/php-cs-fixer.php`.
*
* KaririCode coding style: PSR-12 + PHP 8.4 migration + strict types +
* compiler-optimized native function invocations.
*
* @since 1.0.0
*/
final class CsFixerConfigGenerator implements ConfigGenerator
{
#[\Override]
public function toolName(): string
{
return 'cs-fixer';
}
#[\Override]
public function outputPath(): string
{
return 'php-cs-fixer.php';
}
#[\Override]
public function generate(ProjectContext $context): string
{
$finderDirs = '';
foreach ($context->relativeSourceDirs() as $dir) {
$finderDirs .= " ->in(__DIR__ . '/../{$dir}')\n";
}
foreach ($context->relativeTestDirs() as $dir) {
$finderDirs .= " ->in(__DIR__ . '/../{$dir}')\n";
}
$rulesExport = $this->exportRules($context->csFixerRules);
return <<<PHP
<?php
declare(strict_types=1);
/**
* Generated by KaririCode Devkit — override rules via devkit.php (project root)
* key: cs_fixer_rules (merged with KaririCode defaults).
*/
\$finder = PhpCsFixer\\Finder::create()
{$finderDirs} ->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new PhpCsFixer\\Config())
->setRules({$rulesExport})
->setFinder(\$finder)
->setRiskyAllowed(true)
->setUsingCache(true)
->setCacheFile(__DIR__ . '/build/.php-cs-fixer.cache');
PHP;
}
/** @param array<string, mixed> $rules */
private function exportRules(array $rules): string
{
$export = var_export($rules, true);
// Normalize var_export output to short array syntax
$export = (string) preg_replace('/^array \($/m', '[', $export);
$export = (string) preg_replace('/^\)$/m', ']', $export);
$export = str_replace('array (', '[', $export);
return str_replace(')', ']', $export);
}
}