diff --git a/app/Factories/ConfigurationFactory.php b/app/Factories/ConfigurationFactory.php index b42ab909..d2388abd 100644 --- a/app/Factories/ConfigurationFactory.php +++ b/app/Factories/ConfigurationFactory.php @@ -42,12 +42,15 @@ class ConfigurationFactory */ public static function preset($rules) { + $configRepo = resolve(ConfigurationJsonRepository::class); + return (new Config) ->setParallelConfig(ParallelConfigFactory::detect()) ->setFinder(self::finder()) - ->setRules(array_merge($rules, resolve(ConfigurationJsonRepository::class)->rules())) + ->setRules(array_merge($rules, $configRepo->rules())) ->setRiskyAllowed(true) - ->setUsingCache(true); + ->setUsingCache(true) + ->registerCustomFixers($configRepo->customFixers()); } /** diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index caca7d1a..52f89804 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -2,6 +2,9 @@ namespace App\Repositories; +use App\Project; +use PhpToken; + class ConfigurationJsonRepository { /** @@ -69,6 +72,36 @@ public function preset() return $this->preset ?: ($this->get()['preset'] ?? 'laravel'); } + /** + * Get the custom fixers. + * + * @return array + */ + public function customFixers() + { + return collect($this->get()['custom-fixers'] ?? []) + ->map(function ($fixerPath) { + $fixerProjectPath = Project::path().'/'.$fixerPath; + + spl_autoload_register(fn () => require_once ($fixerProjectPath)); + + $tokens = PhpToken::tokenize(file_get_contents($fixerProjectPath)); + + $namespace = null; + foreach ($tokens as $idx => $token) { + if ($token->id == T_NAMESPACE) { + $namespace = $tokens[$idx + 2]->text; + break; + } + } + + $fixerClasspath = $namespace.'\\'.basename($fixerPath, '.php'); + + return new $fixerClasspath; + }) + ->toArray(); + } + /** * Get the configuration from the "pint.json" file. * diff --git a/tests/Fixtures/custom/CustomFixer.php b/tests/Fixtures/custom/CustomFixer.php new file mode 100644 index 00000000..57b2ac7a --- /dev/null +++ b/tests/Fixtures/custom/CustomFixer.php @@ -0,0 +1,40 @@ +preset())->toBe('laravel'); }); + +it('may have custom fixers options', function () { + $repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/custom/pint.json', null); + + expect($repository->customFixers()) + ->ToBeArray() + ->toContainOnlyInstancesOf(\PhpCsFixer\Fixer\FixerInterface::class) + ->toMatchArray([ + new \Tests\Fixtures\custom\CustomFixer, + ]); +});