Skip to content

Commit b35dba4

Browse files
authored
Merge pull request #595 from rodrigoprimo/inline-control-strutucture-remove-switch
Generic/InlineControlStructure: stop listening for T_SWITCH
2 parents 12c260f + 105b4be commit b35dba4

12 files changed

+481
-80
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function register()
4848
T_FOREACH,
4949
T_WHILE,
5050
T_DO,
51-
T_SWITCH,
5251
T_FOR,
5352
];
5453

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,42 +79,42 @@ if ($a)
7979
</div>
8080

8181
<?php
82-
switch ($this->error):
83-
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
84-
Ung&uuml;ltiges Geschlecht!
85-
<?php break;
86-
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
87-
Die eingetragene E-Mail-Adresse ist bereits registriert.
88-
<?php break;
89-
endswitch;
90-
91-
if ($this->allowShopping !== true):
92-
if ($this->status != Shop_Cart :: OK):
93-
switch ($this->status):
94-
case Shop_Cart :: NOT_FOUND:
95-
echo 'foo';
96-
endswitch;
82+
83+
if ($error === ERROR_ONE): ?>
84+
Error one!
85+
<?php elseif ($error === ERROR_TWO): ?>
86+
Error two!
87+
<?php endif;
88+
89+
90+
91+
if ($value):
92+
if ($anotherValue):
93+
foreach ($array as $element):
94+
echo (function($element): string { return $element; } )($element);
95+
endforeach;
9796
endif;
9897
else:
9998
echo 'foo';
10099
endif;
101100

101+
102102
// ELSE IF split over multiple lines (not inline)
103103
if ($test) {
104104
} else
105105
if ($test) {
106106
} else {
107107
}
108108

109-
switch($response = \Bar::baz('bat', function ($foo) {
109+
if ((function () {
110110
return 'bar';
111-
})) {
112-
case 1:
113-
return 'test';
111+
})())
112+
echo 'one';
113+
114+
115+
116+
114117

115-
case 2:
116-
return 'other';
117-
}
118118

119119
$stuff = [1,2,3];
120120
foreach($stuff as $num)
@@ -201,16 +201,16 @@ if (true)
201201
catch(Exception $e) {
202202
}
203203

204-
switch ($num) {
205-
case 0:
206-
if (1 > $num)
207-
return bar(
208-
baz(
209-
"foobarbaz"
210-
)
211-
);
212-
break;
213-
}
204+
for ($i = 0; $i <= 4; $i++)
205+
if ($i % 2)
206+
return bar(
207+
baz(
208+
"foobarbaz"
209+
)
210+
);
211+
212+
213+
214214

215215
do {
216216
$i++;

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,43 +91,44 @@ if ($a) {
9191
</div>
9292

9393
<?php
94-
switch ($this->error):
95-
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
96-
Ung&uuml;ltiges Geschlecht!
97-
<?php break;
98-
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
99-
Die eingetragene E-Mail-Adresse ist bereits registriert.
100-
<?php break;
101-
endswitch;
102-
103-
if ($this->allowShopping !== true):
104-
if ($this->status != Shop_Cart :: OK):
105-
switch ($this->status):
106-
case Shop_Cart :: NOT_FOUND:
107-
echo 'foo';
108-
endswitch;
94+
95+
if ($error === ERROR_ONE): ?>
96+
Error one!
97+
<?php elseif ($error === ERROR_TWO): ?>
98+
Error two!
99+
<?php endif;
100+
101+
102+
103+
if ($value):
104+
if ($anotherValue):
105+
foreach ($array as $element):
106+
echo (function($element): string { return $element; } )($element);
107+
endforeach;
109108
endif;
110109
else:
111110
echo 'foo';
112111
endif;
113112

113+
114114
// ELSE IF split over multiple lines (not inline)
115115
if ($test) {
116116
} else
117117
if ($test) {
118118
} else {
119119
}
120120

121-
switch($response = \Bar::baz('bat', function ($foo) {
121+
if ((function () {
122122
return 'bar';
123-
})) {
124-
case 1:
125-
return 'test';
126-
127-
case 2:
128-
return 'other';
123+
})()) {
124+
echo 'one';
129125
}
130126

127+
128+
129+
130+
131+
131132
$stuff = [1,2,3];
132133
foreach($stuff as $num) {
133134
if ($num %2 ) {
@@ -229,18 +230,19 @@ if (true) {
229230
}
230231
}
231232

232-
switch ($num) {
233-
case 0:
234-
if (1 > $num) {
235-
return bar(
236-
baz(
237-
"foobarbaz"
238-
)
239-
);
240-
}
241-
break;
233+
for ($i = 0; $i <= 4; $i++) {
234+
if ($i % 2) {
235+
return bar(
236+
baz(
237+
"foobarbaz"
238+
)
239+
);
240+
}
242241
}
243242

243+
244+
245+
244246
do {
245247
$i++;
246248
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do {
2020

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

23-
SomeClass.prototype.switch = function() {
23+
SomeClass.prototype.for = function() {
2424
// do something
2525
};
2626

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ do {
2626
do { i++;
2727
} while (i < 5);
2828

29-
SomeClass.prototype.switch = function() {
29+
SomeClass.prototype.for = function() {
3030
// do something
3131
};
3232

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getErrorList($testFile='')
4848
62 => 1,
4949
66 => 1,
5050
78 => 1,
51+
109 => 1,
5152
120 => 1,
5253
128 => 1,
5354
134 => 1,
@@ -69,7 +70,8 @@ public function getErrorList($testFile='')
6970
191 => 1,
7071
195 => 1,
7172
198 => 1,
72-
206 => 1,
73+
204 => 1,
74+
205 => 1,
7375
222 => 1,
7476
232 => 1,
7577
235 => 1,

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ switch (true) {
6262
case CONSTANT = 1:
6363
/* testIsNotEnumCaseIsCaseInsensitive */
6464
cAsE CONSTANT:
65+
/* testCaseConstantCloserMarker */
6566
}
6667

6768
switch ($x) {
6869
/* testCaseInSwitchWhenCreatingEnumInSwitch1 */
6970
case 'a': {
7071
enum Foo {}
7172
break;
73+
/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */
7274
}
7375

7476
/* testCaseInSwitchWhenCreatingEnumInSwitch2 */
7577
case 'b';
7678
enum Bar {}
79+
/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */
7780
break;
7881
}
7982

@@ -93,3 +96,37 @@ enum Foo: string {
9396
/* testKeywordAsEnumCaseNameShouldBeString7 */
9497
case ARRAY = 'array';
9598
}
99+
100+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
101+
switch ($value):
102+
/* testSwitchCaseScopeCloserSharedWithSwitch */
103+
case 1:
104+
echo 'one';
105+
endswitch;
106+
107+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/879
108+
switch ($type) {
109+
/* testSwitchCaseNestedIfWithAndWithoutBraces */
110+
case 1:
111+
if ($foo) {
112+
return true;
113+
} elseif ($baz)
114+
return true;
115+
else {
116+
echo 'else';
117+
}
118+
break;
119+
}
120+
121+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/1590
122+
switch ($num) {
123+
/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */
124+
case 0:
125+
if (1 > $num)
126+
return bar(
127+
baz(
128+
"foobarbaz"
129+
)
130+
);
131+
break;
132+
}

0 commit comments

Comments
 (0)