|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace whatwedo\PhpCodingStandard; |
| 4 | + |
| 5 | +use PhpCsFixer; |
| 6 | +use PhpCsFixer\ConfigInterface; |
| 7 | +use whatwedo\PhpCodingStandard\PhpCsFixerConfig\WwdPhpCsFixerConfigInterface; |
| 8 | + |
| 9 | +class PhpCsFixerConfigBuilder |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param class-string<WwdPhpCsFixerConfigInterface>[] $configs |
| 13 | + */ |
| 14 | + public static function build( |
| 15 | + string $projectDir, |
| 16 | + array $configs |
| 17 | + ): ConfigInterface |
| 18 | + { |
| 19 | + $excludes = self::buildExcludes($configs); |
| 20 | + |
| 21 | + $finder = new PhpCsFixer\Finder(); |
| 22 | + $finder->in($projectDir) |
| 23 | + ->exclude($excludes); |
| 24 | + |
| 25 | + $config = new PhpCsFixer\Config(); |
| 26 | + $config->setFinder($finder) |
| 27 | + ->setRiskyAllowed(true) |
| 28 | + ->setRules(self::buildRules($configs)); |
| 29 | + |
| 30 | + return $config; |
| 31 | + } |
| 32 | + |
| 33 | + private static function getNextOrder(array $rules, int $order) |
| 34 | + { |
| 35 | + while (array_key_exists($order, $rules)) { |
| 36 | + $order++; |
| 37 | + } |
| 38 | + return $order; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param class-string<WwdPhpCsFixerConfigInterface>[] $configs |
| 43 | + */ |
| 44 | + private static function buildRules(array $configs): array |
| 45 | + { |
| 46 | + /** |
| 47 | + * @var array <int, <array<string, mixed>> |
| 48 | + */ |
| 49 | + $rulesGroups = []; |
| 50 | + |
| 51 | + foreach ($configs as $config) { |
| 52 | + foreach ($config::getRules() as $order => $configRule) { |
| 53 | + $rulesGroups[self::getNextOrder($rulesGroups, $order)] = $configRule; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // order rules from Configs |
| 58 | + ksort($rulesGroups); |
| 59 | + |
| 60 | + $rules = []; |
| 61 | + foreach ($rulesGroups as $rulesGroup) { |
| 62 | + foreach ($rulesGroup as $rule => $ruleSetting) { |
| 63 | + $rules[$rule] = $ruleSetting; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return $rules; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param class-string<WwdPhpCsFixerConfigInterface>[] $configs |
| 72 | + */ |
| 73 | + public static function dumpRules(array $configs) |
| 74 | + { |
| 75 | + var_dump(self::buildRules($configs)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param class-string<WwdPhpCsFixerConfigInterface>[] $configs |
| 80 | + */ |
| 81 | + public static function dumpExcludes(array $configs) |
| 82 | + { |
| 83 | + var_dump(self::buildExcludes($configs)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param class-string<WwdPhpCsFixerConfigInterface>[] $configs |
| 88 | + */ |
| 89 | + private static function buildExcludes(array $configs): array |
| 90 | + { |
| 91 | + $excludes = []; |
| 92 | + foreach ($configs as $config) { |
| 93 | + foreach ($config::getExcludes() as $configExclude) { |
| 94 | + $excludes[] = $configExclude; |
| 95 | + } |
| 96 | + } |
| 97 | + return $excludes; |
| 98 | + } |
| 99 | +} |
0 commit comments