Skip to content

Commit

Permalink
Merge pull request #595 from rodrigoprimo/inline-control-strutucture-…
Browse files Browse the repository at this point in the history
…remove-switch

Generic/InlineControlStructure: stop listening for T_SWITCH
  • Loading branch information
jrfnl authored Dec 20, 2024
2 parents 12c260f + 105b4be commit b35dba4
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function register()
T_FOREACH,
T_WHILE,
T_DO,
T_SWITCH,
T_FOR,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,42 @@ if ($a)
</div>

<?php
switch ($this->error):
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
Ung&uuml;ltiges Geschlecht!
<?php break;
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
Die eingetragene E-Mail-Adresse ist bereits registriert.
<?php break;
endswitch;

if ($this->allowShopping !== true):
if ($this->status != Shop_Cart :: OK):
switch ($this->status):
case Shop_Cart :: NOT_FOUND:
echo 'foo';
endswitch;

if ($error === ERROR_ONE): ?>
Error one!
<?php elseif ($error === ERROR_TWO): ?>
Error two!
<?php endif;



if ($value):
if ($anotherValue):
foreach ($array as $element):
echo (function($element): string { return $element; } )($element);
endforeach;
endif;
else:
echo 'foo';
endif;


// ELSE IF split over multiple lines (not inline)
if ($test) {
} else
if ($test) {
} else {
}

switch($response = \Bar::baz('bat', function ($foo) {
if ((function () {
return 'bar';
})) {
case 1:
return 'test';
})())
echo 'one';





case 2:
return 'other';
}

$stuff = [1,2,3];
foreach($stuff as $num)
Expand Down Expand Up @@ -201,16 +201,16 @@ if (true)
catch(Exception $e) {
}

switch ($num) {
case 0:
if (1 > $num)
return bar(
baz(
"foobarbaz"
)
);
break;
}
for ($i = 0; $i <= 4; $i++)
if ($i % 2)
return bar(
baz(
"foobarbaz"
)
);




do {
$i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,43 +91,44 @@ if ($a) {
</div>

<?php
switch ($this->error):
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
Ung&uuml;ltiges Geschlecht!
<?php break;
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
Die eingetragene E-Mail-Adresse ist bereits registriert.
<?php break;
endswitch;

if ($this->allowShopping !== true):
if ($this->status != Shop_Cart :: OK):
switch ($this->status):
case Shop_Cart :: NOT_FOUND:
echo 'foo';
endswitch;

if ($error === ERROR_ONE): ?>
Error one!
<?php elseif ($error === ERROR_TWO): ?>
Error two!
<?php endif;



if ($value):
if ($anotherValue):
foreach ($array as $element):
echo (function($element): string { return $element; } )($element);
endforeach;
endif;
else:
echo 'foo';
endif;


// ELSE IF split over multiple lines (not inline)
if ($test) {
} else
if ($test) {
} else {
}

switch($response = \Bar::baz('bat', function ($foo) {
if ((function () {
return 'bar';
})) {
case 1:
return 'test';

case 2:
return 'other';
})()) {
echo 'one';
}






$stuff = [1,2,3];
foreach($stuff as $num) {
if ($num %2 ) {
Expand Down Expand Up @@ -229,18 +230,19 @@ if (true) {
}
}

switch ($num) {
case 0:
if (1 > $num) {
return bar(
baz(
"foobarbaz"
)
);
}
break;
for ($i = 0; $i <= 4; $i++) {
if ($i % 2) {
return bar(
baz(
"foobarbaz"
)
);
}
}




do {
$i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ do {

do i++; while (i < 5);

SomeClass.prototype.switch = function() {
SomeClass.prototype.for = function() {
// do something
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ do {
do { i++;
} while (i < 5);

SomeClass.prototype.switch = function() {
SomeClass.prototype.for = function() {
// do something
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getErrorList($testFile='')
62 => 1,
66 => 1,
78 => 1,
109 => 1,
120 => 1,
128 => 1,
134 => 1,
Expand All @@ -69,7 +70,8 @@ public function getErrorList($testFile='')
191 => 1,
195 => 1,
198 => 1,
206 => 1,
204 => 1,
205 => 1,
222 => 1,
232 => 1,
235 => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ switch (true) {
case CONSTANT = 1:
/* testIsNotEnumCaseIsCaseInsensitive */
cAsE CONSTANT:
/* testCaseConstantCloserMarker */
}

switch ($x) {
/* testCaseInSwitchWhenCreatingEnumInSwitch1 */
case 'a': {
enum Foo {}
break;
/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */
}

/* testCaseInSwitchWhenCreatingEnumInSwitch2 */
case 'b';
enum Bar {}
/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */
break;
}

Expand All @@ -93,3 +96,37 @@ enum Foo: string {
/* testKeywordAsEnumCaseNameShouldBeString7 */
case ARRAY = 'array';
}

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
switch ($value):
/* testSwitchCaseScopeCloserSharedWithSwitch */
case 1:
echo 'one';
endswitch;

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/879
switch ($type) {
/* testSwitchCaseNestedIfWithAndWithoutBraces */
case 1:
if ($foo) {
return true;
} elseif ($baz)
return true;
else {
echo 'else';
}
break;
}

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/1590
switch ($num) {
/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */
case 0:
if (1 > $num)
return bar(
baz(
"foobarbaz"
)
);
break;
}
Loading

0 comments on commit b35dba4

Please sign in to comment.