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

Generic/InlineControlStructure: stop listening for T_SWITCH #595

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this improvement also be done RecurseScopeMapDefaultKeywordConditionsTest tests ?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth it to also have a variation of this test in the RecurseScopeMapDefaultKeywordConditionsTest ?

switch ($value):
/* testSwitchCaseScopeCloserSharedWithSwitch */
case 1:
echo 'one';
endswitch;

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/879
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a similar test to this one in the RecurseScopeMapDefaultKeywordConditionsTest test ?

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
Loading