Skip to content

Commit

Permalink
#43 - named parameters fixer (#118)
Browse files Browse the repository at this point in the history
* #43 - wip: added named argument fixer

* #43 - wip: fixed tests and added more cases for fixer

* #43 - csf
  • Loading branch information
kamilpiech97 authored Apr 8, 2024
1 parent 17d9dcc commit f39ca63
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Blumilk\Codestyle\Configuration\Utils\Rule;
use Blumilk\Codestyle\Fixers\CompactEmptyArrayFixer;
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use Blumilk\Codestyle\Fixers\NamedArgumentFixer;
use Blumilk\Codestyle\Fixers\NoCommentFixer;
use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer;
use JetBrains\PhpStorm\ArrayShape;
Expand Down Expand Up @@ -105,6 +106,7 @@ protected function getCustomFixers(): array
new NoLaravelMigrationsGeneratedCommentFixer(),
new NoCommentFixer(),
new CompactEmptyArrayFixer(),
new NamedArgumentFixer(),
];
}
}
2 changes: 2 additions & 0 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Blumilk\Codestyle\Fixers\CompactEmptyArrayFixer;
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use Blumilk\Codestyle\Fixers\NamedArgumentFixer;
use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer;
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer;
Expand Down Expand Up @@ -338,5 +339,6 @@ class CommonRules extends Rules
NoMultilineWhitespaceAroundDoubleArrowFixer::class => true,
CompactEmptyArrayFixer::class => true,
ClassKeywordFixer::class => true,
NamedArgumentFixer::class => true,
];
}
92 changes: 92 additions & 0 deletions src/Fixers/NamedArgumentFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Fixers;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

final class NamedArgumentFixer implements FixerInterface
{
public function getDefinition(): FixerDefinition
{
$codeSample = <<<'EOF'
<?php
if (floatval($percentage) >= (float)$this->option("threshold")) {
event(
new ErrorsThresholdExceeded(
requestsNumber : $requests,
errorsNumber : $errors,
percentage : $percentage,
),
);
}
EOF;

return new FixerDefinition(
"Fix named arguments formatting.",
[
new CodeSample($codeSample),
],
);
}

public function getName(): string
{
return "Blumilk/named_arguments";
}

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

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

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(CT::T_NAMED_ARGUMENT_NAME);
}

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

public function fix(SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind([CT::T_NAMED_ARGUMENT_NAME, CT::T_NAMED_ARGUMENT_COLON])) {
continue;
}

if ($token->isGivenKind(CT::T_NAMED_ARGUMENT_NAME)) {
$colonIndex = $tokens->getNextMeaningfulToken($index);

if ($colonIndex !== null && $tokens[$colonIndex]->isGivenKind(CT::T_NAMED_ARGUMENT_COLON)) {
$nextIndex = $tokens->getNextNonWhitespace($index);

if ($tokens[$index + 1]->isGivenKind(T_WHITESPACE) && $nextIndex === $colonIndex) {
$tokens->clearAt($index + 1);
$tokens->clearEmptyTokens();
}
}
} elseif ($token->isGivenKind(CT::T_NAMED_ARGUMENT_COLON)) {
if ($tokens[$index + 1]->isWhitespace() && $tokens[$index + 1]->getContent() !== " ") {
$tokens[$index + 1] = new Token([T_WHITESPACE, " "]);
} elseif (!$tokens[$index + 1]->isWhitespace()) {
$tokens->insertAt($index + 1, new Token([T_WHITESPACE, " "]));
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function providePhp80Fixtures(): array
["noMultilineWhitespaceAroundDoubleArrow"],
["compactArray"],
["classesImport"],
["namedParameters"],
];
}

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/namedParameters/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$class = new ErrorsThresholdExceeded(
requestsNumber :$requests,
errorsNumber : $errors,
percentage : $percentage,
);

$result = $reader->merge($merge, new Api(), flag : CONSTANT);
11 changes: 11 additions & 0 deletions tests/fixtures/namedParameters/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$class = new ErrorsThresholdExceeded(
requestsNumber: $requests,
errorsNumber: $errors,
percentage: $percentage,
);

$result = $reader->merge($merge, new Api(), flag: CONSTANT);

0 comments on commit f39ca63

Please sign in to comment.