Skip to content

Commit c755563

Browse files
committed
Support custom fixers
1 parent 444868a commit c755563

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

app/Factories/ConfigurationFactory.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ class ConfigurationFactory
4242
*/
4343
public static function preset($rules)
4444
{
45+
$configRepo = resolve(ConfigurationJsonRepository::class);
46+
4547
return (new Config)
4648
->setParallelConfig(ParallelConfigFactory::detect())
4749
->setFinder(self::finder())
48-
->setRules(array_merge($rules, resolve(ConfigurationJsonRepository::class)->rules()))
50+
->setRules(array_merge($rules, $configRepo->rules()))
4951
->setRiskyAllowed(true)
50-
->setUsingCache(true);
52+
->setUsingCache(true)
53+
->registerCustomFixers($configRepo->customFixers());
5154
}
5255

5356
/**

app/Repositories/ConfigurationJsonRepository.php

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Repositories;
44

5+
use App\Project;
6+
use PhpToken;
7+
58
class ConfigurationJsonRepository
69
{
710
/**
@@ -69,6 +72,36 @@ public function preset()
6972
return $this->preset ?: ($this->get()['preset'] ?? 'laravel');
7073
}
7174

75+
/**
76+
* Get the custom fixers.
77+
*
78+
* @return array<int, \PhpCsFixer\Fixer\FixerInterface>
79+
*/
80+
public function customFixers()
81+
{
82+
return collect($this->get()['custom-fixers'] ?? [])
83+
->map(function ($fixerPath) {
84+
$fixerProjectPath = Project::path().'/'.$fixerPath;
85+
86+
spl_autoload_register(fn () => require_once ($fixerProjectPath));
87+
88+
$tokens = PhpToken::tokenize(file_get_contents($fixerProjectPath));
89+
90+
$namespace = null;
91+
foreach ($tokens as $idx => $token) {
92+
if ($token->id == T_NAMESPACE) {
93+
$namespace = $tokens[$idx + 2]->text;
94+
break;
95+
}
96+
}
97+
98+
$fixerClasspath = $namespace.'\\'.basename($fixerPath, '.php');
99+
100+
return new $fixerClasspath;
101+
})
102+
->toArray();
103+
}
104+
72105
/**
73106
* Get the configuration from the "pint.json" file.
74107
*

tests/Fixtures/custom/CustomFixer.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\custom;
4+
5+
use PhpCsFixer\Fixer\FixerInterface;
6+
use PhpCsFixer\FixerDefinition\FixerDefinition;
7+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
8+
use PhpCsFixer\Tokenizer\Tokens;
9+
10+
class CustomFixer implements FixerInterface {
11+
12+
public function getName(): string {
13+
return 'My/CustomFixer';
14+
}
15+
16+
public function getDefinition(): FixerDefinitionInterface {
17+
return new FixerDefinition('A custom fixer', []);
18+
}
19+
20+
public function fix(\SplFileInfo $file, Tokens $tokens): void {
21+
//
22+
}
23+
24+
public function isCandidate(Tokens $tokens): bool {
25+
return true;
26+
}
27+
28+
public function supports(\SplFileInfo $file): bool {
29+
return true;
30+
}
31+
32+
public function isRisky(): bool {
33+
return false;
34+
}
35+
36+
public function getPriority(): int {
37+
return 0;
38+
}
39+
40+
}

tests/Fixtures/custom/pint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"custom-fixers": [
3+
"tests/Fixtures/custom/CustomFixer.php"
4+
]
5+
}

tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@
4646

4747
expect($repository->preset())->toBe('laravel');
4848
});
49+
50+
it('may have custom fixers options', function () {
51+
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/custom/pint.json', null);
52+
53+
expect($repository->customFixers())
54+
->ToBeArray()
55+
->toContainOnlyInstancesOf(\PhpCsFixer\Fixer\FixerInterface::class)
56+
->toMatchArray([
57+
new \Tests\Fixtures\custom\CustomFixer,
58+
]);
59+
});

0 commit comments

Comments
 (0)