Skip to content

Commit

Permalink
Support custom fixers
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley19280 committed Aug 1, 2024
1 parent 444868a commit 787bdf1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/Factories/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
33 changes: 33 additions & 0 deletions app/Repositories/ConfigurationJsonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Repositories;

use App\Project;
use PhpToken;

class ConfigurationJsonRepository
{
/**
Expand Down Expand Up @@ -69,6 +72,36 @@ public function preset()
return $this->preset ?: ($this->get()['preset'] ?? 'laravel');
}

/**
* Get the custom fixers.
*
* @return array<int, \PhpCsFixer\Fixer\FixerInterface>
*/
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.
*
Expand Down
Binary file modified builds/pint
Binary file not shown.
40 changes: 40 additions & 0 deletions tests/Fixtures/custom/CustomFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Fixtures\custom;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

class CustomFixer implements FixerInterface {

public function getName(): string {
return 'My/CustomFixer';
}

public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition('A custom fixer', []);
}

public function fix(\SplFileInfo $file, Tokens $tokens): void {
//
}

public function isCandidate(Tokens $tokens): bool {
return true;
}

public function supports(\SplFileInfo $file): bool {
return true;
}

public function isRisky(): bool {
return false;
}

public function getPriority(): int {
return 0;
}

}
5 changes: 5 additions & 0 deletions tests/Fixtures/custom/pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"custom-fixers": [
"tests/Fixtures/custom/CustomFixer.php"
]
}
11 changes: 11 additions & 0 deletions tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@

expect($repository->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,
]);
});

0 comments on commit 787bdf1

Please sign in to comment.