Skip to content

Commit

Permalink
Merge pull request #764 from rodrigoprimo/test-coverage-nesting-level
Browse files Browse the repository at this point in the history
Generic/NestingLevel: improve code coverage
  • Loading branch information
jrfnl authored Dec 10, 2024
2 parents 0d969c9 + 9811fc5 commit 5033272
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
// Ignore abstract and interface methods. Bail early when live coding.
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ function nestingFive()
function nestingSix()
{
if ($condition) {
echo 'hi';
switch ($condition)
{
} else {
switch ($condition) {
case '1':
if ($condition === '1') {
if ($cond) {
} elseif ($condition === '2') {
do {
foreach ($conds as $cond) {
echo 'hi';
}
}
} while ($cond > 5);
}
break;
}
Expand Down Expand Up @@ -79,24 +79,30 @@ function nestingEleven()
case '1':
if ($condition === '1') {
if ($cond) {
switch ($cond) {
case '1':
if ($cond === '1') {
foreach ($conds as $cond) {
if ($cond === 'hi') {
if ($cond !== 'bye') {
echo 'hi';
}
try {
if ( $cond === '1' ) {
for ( $i = 0; $i < 10; $i ++ ) {
while ($i < 5) {
if ( $cond === 'hi' ) {
match ( $cond ) {
'hi' => 'something',
};
}
}
}
break;
}
}
} catch (Exception $e) {}
}
}
break;
}
}
}

?>
abstract class AbstractClass {
abstract public function sniffShouldIgnoreAbstractMethods();
}

interface MyInterface {
public function sniffShouldIgnoreInterfaceMethods();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing opening curly bracket).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function sniffShouldBailMissingScopeOpener()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing closing curly bracket).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function sniffShouldBailMissingScopeCloser() {
28 changes: 21 additions & 7 deletions src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ final class NestingLevelUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the test file to process.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [73 => 1];
switch ($testFile) {
case 'NestingLevelUnitTest.1.inc':
return [73 => 1];
default:
return [];
}

}//end getErrorList()

Expand All @@ -41,14 +48,21 @@ public function getErrorList()
* 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 test file to process.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
27 => 1,
46 => 1,
];
switch ($testFile) {
case 'NestingLevelUnitTest.1.inc':
return [
27 => 1,
46 => 1,
];
default:
return [];
}

}//end getWarningList()

Expand Down

0 comments on commit 5033272

Please sign in to comment.