Skip to content

Generic/InlineControlStructure: improve code coverage #1144

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

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,22 @@ foreach ($array as $sniffShouldBailEarly)
/* some comment */;

do ; while ($sniffShouldBailEarly > 5);

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

// phpcs:set Generic.ControlStructures.InlineControlStructure error false
if ($something) echo 'hello';
// phpcs:set Generic.ControlStructures.InlineControlStructure error true

for ($i = 0; $i < 5; $i++)
if ($i === 3) {
echo 'three';
} elseif ($i === 4) {
echo 'four';
} elseif ($i === 5) {
echo 'five';
}

if ($noSpaceAfterClosingParenthesis)echo 'hello'; ?>

<?php if ( $model->scenario == 'simple' ) $widget->renderPager() ?>
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,26 @@ foreach ($array as $sniffShouldBailEarly)
/* some comment */;

do ; while ($sniffShouldBailEarly > 5);

do { echo $i++;
} while ($i < 5);

// phpcs:set Generic.ControlStructures.InlineControlStructure error false
if ($something) { echo 'hello';
}
// phpcs:set Generic.ControlStructures.InlineControlStructure error true

for ($i = 0; $i < 5; $i++) {
if ($i === 3) {
echo 'three';
} elseif ($i === 4) {
echo 'four';
} elseif ($i === 5) {
echo 'five';
}
}

if ($noSpaceAfterClosingParenthesis) { echo 'hello';
} ?>

<?php if ( $model->scenario == 'simple' ) { $widget->renderPager(); } ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// This should be the only test in this file.
// There should be only empty tokens after the scope closer of the "if" token.

for ($i = 0; $i < 5; $i++)
if ($i === 3) {
echo $i;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// This should be the only test in this file.
// There should be only empty tokens after the scope closer of the "if" token.

for ($i = 0; $i < 5; $i++) {
if ($i === 3) {
echo $i;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error (missing semicolon). Should still trigger the sniff error.
// This should be the only test in this file.
// This test should explicitly NOT contain a new line at the end of the file.

if ($bar)
doSomething()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// Intentional parse error (missing semicolon). Should still trigger the sniff error.
// This should be the only test in this file.
// This test should explicitly NOT contain a new line at the end of the file.

if ($bar) {
doSomething();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// Intentional parse error (nothing after else).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

if ($bar) {
doSomething();
} else
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// Intentional parse error (missing closing brace for the first if).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
// Test for https://github.com/squizlabs/PHP_CodeSniffer/pull/2989.

if ( true ) {

// This second if is here to ensure that the sniff completely bows out of this file after
// encountering the first one without a closing brace.
if ( true )
echo 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (nothing after for closing parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

for ($i = 0; $i < 5; $i++)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (nothing after while closing parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

while ($i < 5)
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ public function getErrorList($testFile='')
269 => 1,
278 => 1,
289 => 1,
301 => 1,
307 => 1,
316 => 1,
318 => 1,
];

case 'InlineControlStructureUnitTest.10.inc':
return [
6 => 1,
];

case 'InlineControlStructureUnitTest.11.inc':
return [
7 => 1,
];

default:
Expand All @@ -97,11 +111,21 @@ public function getErrorList($testFile='')
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [];
switch ($testFile) {
case 'InlineControlStructureUnitTest.1.inc':
return [
304 => 1,
];

default:
return [];
}

}//end getWarningList()

Expand Down