Skip to content

Commit

Permalink
Update getFixerConfig method in PhpCsFixerCodingStandard (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Mar 26, 2024
1 parent a4989d4 commit 1a01736
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/PhpCsFixer/PhpCsFixerCodingStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,28 @@ public function getRules(): array
return $this->ruleSet;
}

public function getFixerConfig(?Finder $customFinder = null, array $customRules = []): ConfigInterface
{
$customRules = [
NoDuplicatedArrayKeyFixer::name() => true,
NoUselessCommentFixer::name() => true,
] + $customRules;
public function getFixerConfig(
?Finder $customFinder = null,
array $customRules = [],
bool $inheritRuleSet = true,
): ConfigInterface {
if ($inheritRuleSet) {
$customRules = [
NoDuplicatedArrayKeyFixer::name() => true,
NoUselessCommentFixer::name() => true,
] + $customRules;

$all = self::mergeRules($this->getRules(), $customRules);
} else {
$all = $customRules;
}

return (new Config($this->styleName))
->setRiskyAllowed(true)
->registerCustomFixers(new Fixers())
->setCacheFile("{$this->projectPath}/build/php-cs-fixer-cache.json")
->setFinder($customFinder ?? $this->getFinder())
->setRules($this->getRules() + $customRules);
->setRules($all);
}

public function getFinder(): Finder
Expand All @@ -265,4 +274,18 @@ public function getFinder(): Finder
->exclude('build')
->name('/\.php$/');
}

private static function mergeRules(array $original, array $overrides): array
{
foreach ($overrides as $newKey => $newValue) {
if (isset($original[$newKey]) && $newValue === null) {
unset($original[$newKey]);
continue;
}

$original[$newKey] = $newValue;
}

return $original;
}
}

0 comments on commit 1a01736

Please sign in to comment.