Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom fixers #290

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,
]);
});