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

clean ups #6196

Merged
merged 1 commit into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Cache/Cache.php
Expand Up @@ -88,8 +88,6 @@ public function toJson(): string

/**
* @throws \InvalidArgumentException
*
* @return Cache
*/
public static function fromJson(string $json): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Fixer/Comment/HeaderCommentFixer.php
Expand Up @@ -148,12 +148,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

if (!isset($locationIndices[$locationIndex]) || $possibleLocation === $location) {
$locationIndices[$locationIndex] = $possibleLocation;

continue;
}
}

foreach (array_values($locationIndices) as $possibleLocation) {
foreach ($locationIndices as $possibleLocation) {
// figure out where the comment should be placed
$headerNewIndex = $this->findHeaderCommentInsertionIndex($tokens, $possibleLocation);

Expand Down
25 changes: 3 additions & 22 deletions src/Fixer/Operator/TernaryOperatorSpacesFixer.php
Expand Up @@ -18,6 +18,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function isCandidate(Tokens $tokens): bool
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$alternativeSyntaxAnalyzer = new AlternativeSyntaxAnalyzer();
$gotoLabelAnalyzer = new GotoLabelAnalyzer();
$ternaryOperatorIndices = [];
$excludedIndices = $this->getColonIndicesForSwitch($tokens);
Expand All @@ -76,7 +78,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

if ($this->belongsToAlternativeSyntax($tokens, $index)) {
if ($alternativeSyntaxAnalyzer->belongsToAlternativeSyntax($tokens, $index)) {
continue;
}

Expand Down Expand Up @@ -121,27 +123,6 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}
}

private function belongsToAlternativeSyntax(Tokens $tokens, int $index): bool
{
if (!$tokens[$index]->equals(':')) {
return false;
}

$closeParenthesisIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$closeParenthesisIndex]->isGivenKind(T_ELSE)) {
return true;
}
if (!$tokens[$closeParenthesisIndex]->equals(')')) {
return false;
}

$openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $closeParenthesisIndex);

$alternativeControlStructureIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex);

return $tokens[$alternativeControlStructureIndex]->isGivenKind([T_DECLARE, T_ELSEIF, T_FOR, T_FOREACH, T_IF, T_SWITCH, T_WHILE]);
}

/**
* @return int[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php
Expand Up @@ -39,11 +39,11 @@ public function belongsToAlternativeSyntax(Tokens $tokens, int $index): bool
}

$openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $prevIndex);

$beforeOpenParenthesisIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex);

return $tokens[$beforeOpenParenthesisIndex]->isGivenKind([
T_DECLARE,
T_ELSEIF,
T_FOR,
T_FOREACH,
T_IF,
Expand Down
9 changes: 5 additions & 4 deletions tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
Expand Up @@ -37,7 +37,8 @@ public function testBelongsToAlternativeSyntax(array $expectedPositives, string
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
static::assertSame(
\in_array($index, $expectedPositives, true),
(new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index)
(new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index),
'@ index: '.$index
);
}
}
Expand All @@ -59,9 +60,9 @@ public function provideBelongsToAlternativeSyntaxCases(): iterable
'<?php foreach([1, 2, 3] as $i): echo $i; endforeach;',
];

yield 'if' => [
[6, 14],
'<?php if ($condition): echo 1; else: echo 2; endif;',
yield 'if, elseif, else' => [
[6, 17, 25],
'<?php if ($condition): echo 1; elseif($a): echo 2; else: echo 3; endif;',
];

yield 'switch' => [
Expand Down