-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OperatorLinebreakFixer - fix for alternative syntax
- Loading branch information
1 parent
075082e
commit 716f346
Showing
4 changed files
with
163 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
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, | ||
]); | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
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,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; | ||
', | ||
]; | ||
} | ||
} |