Skip to content

Commit

Permalink
bug #6143 OperatorLinebreakFixer - fix for alternative syntax (kubawe…
Browse files Browse the repository at this point in the history
…rlos)

This PR was squashed before being merged into the master branch (closes #6143).

Discussion
----------

OperatorLinebreakFixer - fix for alternative syntax

Fixes #6142

Commits
-------

716f346 OperatorLinebreakFixer - fix for alternative syntax
  • Loading branch information
SpacePossum committed Dec 9, 2021
2 parents 73f051d + 716f346 commit 3de1d1b
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Fixer/Operator/OperatorLinebreakFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
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 @@ -121,6 +122,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$referenceAnalyzer = new ReferenceAnalyzer();
$gotoLabelAnalyzer = new GotoLabelAnalyzer();
$alternativeSyntaxAnalyzer = new AlternativeSyntaxAnalyzer();

$excludedIndices = $this->getExcludedIndices($tokens);

Expand All @@ -140,6 +142,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

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

if (\in_array($index, $excludedIndices, true)) {
continue;
}
Expand Down
54 changes: 54 additions & 0 deletions src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tokenizer\Analyzer;

use PhpCsFixer\Tokenizer\Tokens;

/**
* @internal
* @TODO 4.0 remove this analyzer and move this logic into a transformer
*/
final class AlternativeSyntaxAnalyzer
{
public function belongsToAlternativeSyntax(Tokens $tokens, int $index): bool
{
if (!$tokens[$index]->equals(':')) {
return false;
}

$prevIndex = $tokens->getPrevMeaningfulToken($index);

if ($tokens[$prevIndex]->isGivenKind(T_ELSE)) {
return true;
}

if (!$tokens[$prevIndex]->equals(')')) {
return false;
}

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

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

return $tokens[$beforeOpenParenthesisIndex]->isGivenKind([
T_DECLARE,
T_FOR,
T_FOREACH,
T_IF,
T_SWITCH,
T_WHILE,
]);
}
}
16 changes: 16 additions & 0 deletions tests/Fixer/Operator/OperatorLinebreakFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ function foo()
prepare_value:
$objectsPool[$value] = [$id = \count($objectsPool)];
'];

yield 'alternative syntax' => [
'<?php
if (true):
echo 1;
else:
echo 2;
endif;
while (true):
echo "na";
endwhile;
',
null,
['position' => 'beginning'],
];
}

/**
Expand Down
87 changes: 87 additions & 0 deletions tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Tokenizer\Analyzer;

use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer
*/
final class AlternativeSyntaxAnalyzerTest extends TestCase
{
/**
* @param int[] $expectedPositives
*
* @dataProvider provideBelongsToAlternativeSyntaxCases
*/
public function testBelongsToAlternativeSyntax(array $expectedPositives, string $source): void
{
$tokens = Tokens::fromCode($source);

for ($index = $tokens->count() - 1; $index >= 0; --$index) {
static::assertSame(
\in_array($index, $expectedPositives, true),
(new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index)
);
}
}

public function provideBelongsToAlternativeSyntaxCases(): iterable
{
yield 'declare' => [
[7],
'<?php declare(ticks=1):enddeclare;',
];

yield 'for' => [
[20],
'<?php for($i = 0; $i < 10; $i++): echo $i; endfor;',
];

yield 'foreach' => [
[17],
'<?php foreach([1, 2, 3] as $i): echo $i; endforeach;',
];

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

yield 'switch' => [
[6],
'<?php switch ($value): default: echo 4; endswitch;',
];

yield 'while' => [
[5],
'<?php while(true): echo "na"; endwhile;',
];

yield 'multiple expressions' => [
[7, 15, 51],
'<?php
if ($condition1): echo 1; else: echo 2; endif;
somelabel: echo 3;
echo $condition2 ? 4 : 5;
if ($condition3): echo 6; endif;
',
];
}
}

0 comments on commit 3de1d1b

Please sign in to comment.