Skip to content

Commit 6ad0de5

Browse files
committed
fix: refactor configuration handling in DocBlockHeaderFixer and add getList method in Separate
1 parent d48aaac commit 6ad0de5

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Model/Separate.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ enum Separate: string
2929
case Bottom = 'bottom';
3030
case Both = 'both';
3131
case None = 'none';
32+
33+
/**
34+
* @return non-empty-list<string>
35+
*/
36+
public static function getList(): array
37+
{
38+
return array_column(self::cases(), 'value');
39+
}
3240
}

src/Rules/DocBlockHeaderFixer.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace KonradMichalik\PhpDocBlockHeaderFixer\Rules;
2525

26+
use KonradMichalik\PhpDocBlockHeaderFixer\Model\Separate;
2627
use PhpCsFixer\AbstractFixer;
2728
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
2829
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
@@ -43,9 +44,9 @@
4344
final class DocBlockHeaderFixer extends AbstractFixer implements ConfigurableFixerInterface
4445
{
4546
/**
46-
* @var array<string, mixed>|null
47+
* @var array<string, mixed>
4748
*/
48-
protected ?array $configuration = null;
49+
private array $resolvedConfiguration = [];
4950

5051
public function getDefinition(): FixerDefinitionInterface
5152
{
@@ -77,20 +78,20 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
7778
->setDefault(true)
7879
->getOption(),
7980
(new FixerOptionBuilder('separate', 'Separate the comment'))
80-
->setAllowedValues(['top', 'bottom', 'both', 'none'])
81-
->setDefault('both')
81+
->setAllowedValues(Separate::getList())
82+
->setDefault(Separate::Both->value)
8283
->getOption(),
8384
]);
8485
}
8586

8687
public function configure(?array $configuration = null): void
8788
{
88-
$this->configuration = $this->getConfigurationDefinition()->resolve($configuration ?? []);
89+
$this->resolvedConfiguration = $this->getConfigurationDefinition()->resolve($configuration ?? []);
8990
}
9091

9192
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
9293
{
93-
$annotations = $this->configuration['annotations'] ?? [];
94+
$annotations = $this->resolvedConfiguration['annotations'] ?? [];
9495
if (empty($annotations)) {
9596
return;
9697
}
@@ -112,7 +113,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
112113
private function processClassDocBlock(Tokens $tokens, int $classIndex, array $annotations): void
113114
{
114115
$existingDocBlockIndex = $this->findExistingDocBlock($tokens, $classIndex);
115-
$preserveExisting = $this->configuration['preserve_existing'] ?? true;
116+
$preserveExisting = $this->resolvedConfiguration['preserve_existing'] ?? true;
116117

117118
if (null !== $existingDocBlockIndex) {
118119
if ($preserveExisting) {
@@ -174,7 +175,7 @@ private function replaceDocBlock(Tokens $tokens, int $docBlockIndex, array $anno
174175
*/
175176
private function insertNewDocBlock(Tokens $tokens, int $classIndex, array $annotations): void
176177
{
177-
$separate = $this->configuration['separate'] ?? 'both';
178+
$separate = $this->resolvedConfiguration['separate'] ?? 'both';
178179
$insertIndex = $this->findInsertPosition($tokens, $classIndex);
179180

180181
$tokensToInsert = [];

0 commit comments

Comments
 (0)