Skip to content

Commit

Permalink
bug #6333 BinaryOperatorSpacesFixer - Fix for alignment in elseif (…
Browse files Browse the repository at this point in the history
…paulbalandan, SpacePossum)

This PR was merged into the master branch.

Discussion
----------

BinaryOperatorSpacesFixer - Fix for alignment in `elseif`

Caused by #6112
Fixes #6329

Commits
-------

e01726a more test
85ee54e BinaryOperatorSpacesFixer - Fix for alignment in `elseif`
  • Loading branch information
keradus committed Mar 15, 2022
2 parents d34ccf8 + e01726a commit 6fb9b93
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Fixer/Operator/BinaryOperatorSpacesFixer.php
Expand Up @@ -523,8 +523,8 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $

for ($index = $startAt; $index < $endAt; ++$index) {
$token = $tokens[$index];

$content = $token->getContent();

if (
strtolower($content) === $tokenContent
&& $this->tokensAnalyzer->isBinaryOperator($index)
Expand All @@ -543,7 +543,7 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $
continue;
}

if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH])) {
if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH, T_ELSEIF])) {
$index = $tokens->getNextMeaningfulToken($index);
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);

Expand All @@ -569,7 +569,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA
for ($index = $startAt; $index < $endAt; ++$index) {
$token = $tokens[$index];

if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH])) {
if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH, T_ELSEIF])) {
$index = $tokens->getNextMeaningfulToken($index);
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);

Expand Down
94 changes: 86 additions & 8 deletions tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
Expand Up @@ -129,15 +129,15 @@ public function provideConfiguredCases(): array
],
[
'<?php
$var = [];
$var = [1 => 2];
foreach ([
1 => 2,
2 => 3,
] as $k => $v) {
$var[] = [$i => $bar];
}',
'<?php
$var = [];
$var = [1=>2];
foreach ([
1=> 2,
2 =>3,
Expand Down Expand Up @@ -798,7 +798,7 @@ function b(
for ($i = 0; $i < 10; $i++) {
$aa = 2;
$a[$b] = array();
$a[$b] = array(12);
}',
'<?php
$ccc = 1;
Expand Down Expand Up @@ -845,7 +845,7 @@ function b(
for ($i = 0; $i < 10; $i++) {
$aa = 2;
$a[$b] = array();
$a[$b] = array(12);
}',
],
];
Expand Down Expand Up @@ -1344,15 +1344,35 @@ function b(
$abc[$bcd = 1] = 1;
}
while (false) {
while ($i = 1) {
$aa = 2;
$a[$b] = array();
}
for ($i = 0; $i < 10; $i++) {
$aa = 2;
$a[$b] = array();
}',
}
$z = 1;
switch($a = 0) {
case 1:
$b = 1;
$cc = 3;
break;
}
foreach ($a as $b) {
$aa = 2;
$a[$b] = array();
}
do {
$aa = 23;
$a[$b] = array(66);
} while ($i = 1);
$a = 3;
',
'<?php
$ccc = 1;
$bb = 1;
Expand Down Expand Up @@ -1391,15 +1411,35 @@ function b(
$abc[$bcd = 1] = 1;
}
while (false) {
while ($i = 1) {
$aa = 2;
$a[$b] = array();
}
for ($i = 0; $i < 10; $i++) {
$aa = 2;
$a[$b] = array();
}',
}
$z = 1;
switch($a = 0) {
case 1:
$b = 1;
$cc = 3;
break;
}
foreach ($a as $b) {
$aa = 2;
$a[$b] = array();
}
do {
$aa = 23;
$a[$b] = array(66);
} while ($i = 1);
$a = 3;
',
],
[
'<?php
Expand All @@ -1425,6 +1465,24 @@ function ()
'<?php
fn ($x = 1) => $x + 3;
$f = 123;
',
],
[
'<?php
if (($c = count($array)) > 100) {
$_data = \'100+\';
} elseif (($c = count($array)) > 0) {
$_data = \'0+\';
}
',
],
[
'<?php
if (($c = count($array)) > 100) {
$closure = fn ($x = 1) => $x + 3;
} elseif (($c = count($array)) > 0) {
$closure = fn ($x = 1) => $x ** 3;
}
',
],
];
Expand Down Expand Up @@ -1973,6 +2031,26 @@ function foo() {}
self::STATUS_INVALID_7 => [(2+3)=> "III", "description" => "invalid file syntax, file ignored"],
];',
],
[
'<?php
$b = [1 => function() {
foreach([$a => 2] as $b) {
$bv = [
$b => 2,
$cc => 3,
];
}}, 2 => 3];
',
'<?php
$b = [1 => function() {
foreach([$a => 2] as $b) {
$bv = [
$b => 2,
$cc => 3,
];
}}, 2 => 3];
',
],
];
}

Expand Down

0 comments on commit 6fb9b93

Please sign in to comment.