-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #43 - wip: added named argument fixer * #43 - wip: fixed tests and added more cases for fixer * #43 - csf
- Loading branch information
1 parent
17d9dcc
commit f39ca63
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, " "])); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |