Skip to content

Commit

Permalink
Merge pull request #509 from PHPCSStandards/feature/file-findstartend…
Browse files Browse the repository at this point in the history
…ofstatement-add-extra-qa-test

File::find[Start|End]OfStatement(): add QA tests
  • Loading branch information
jrfnl authored May 22, 2024
2 parents fb351b3 + a82f02e commit 3361ff2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
37 changes: 37 additions & 0 deletions tests/Core/File/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests for the \PHP_CodeSniffer\Files\File::findEndOfStatement method.
Expand All @@ -20,6 +21,42 @@ final class FindEndOfStatementTest extends AbstractMethodUnitTest
{


/**
* Test that end of statement is NEVER before the "current" token.
*
* @return void
*/
public function testEndIsNeverLessThanCurrentToken()
{
$tokens = self::$phpcsFile->getTokens();
$errors = [];

for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}

$end = self::$phpcsFile->findEndOfStatement($i);

// Collect all the errors.
if ($end < $i) {
$errors[] = sprintf(
'End of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is less than %1$d',
$i,
$tokens[$i]['type'],
$tokens[$i]['content'],
$tokens[$i]['line'],
$end,
$tokens[$end]['type']
);
}
}

$this->assertSame([], $errors);

}//end testEndIsNeverLessThanCurrentToken()


/**
* Test a simple assignment.
*
Expand Down
10 changes: 5 additions & 5 deletions tests/Core/File/FindStartOfStatementTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ while(true) {}
$a = 1;

/* testClosureAssignment */
$a = function($b=false;){};
$a = function($b=false){};

/* testHeredocFunctionArg */
myFunction(<<<END
Expand All @@ -39,8 +39,8 @@ use Vendor\Package\{ClassA as A, ClassB, ClassC as C};

$a = [
/* testArrowFunctionArrayValue */
'a' => fn() => return 1,
'b' => fn() => return 1,
'a' => fn() => 1,
'b' => fn() => 1,
];

/* testStaticArrowFunction */
Expand Down Expand Up @@ -139,11 +139,11 @@ switch ($foo) {
/* testInsideCaseStatement */
$var = doSomething();
/* testInsideCaseBreakStatement */
break 2;
break 1;

case 2:
/* testInsideCaseContinueStatement */
continue 2;
continue 1;

case 3:
/* testInsideCaseReturnStatement */
Expand Down
41 changes: 39 additions & 2 deletions tests/Core/File/FindStartOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
Expand All @@ -22,6 +23,42 @@ final class FindStartOfStatementTest extends AbstractMethodUnitTest
{


/**
* Test that start of statement is NEVER beyond the "current" token.
*
* @return void
*/
public function testStartIsNeverMoreThanCurrentToken()
{
$tokens = self::$phpcsFile->getTokens();
$errors = [];

for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}

$start = self::$phpcsFile->findStartOfStatement($i);

// Collect all the errors.
if ($start > $i) {
$errors[] = sprintf(
'Start of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is more than %1$d',
$i,
$tokens[$i]['type'],
$tokens[$i]['content'],
$tokens[$i]['line'],
$start,
$tokens[$start]['type']
);
}
}

$this->assertSame([], $errors);

}//end testStartIsNeverMoreThanCurrentToken()


/**
* Test a simple assignment.
*
Expand Down Expand Up @@ -92,7 +129,7 @@ public function testClosureAssignment()
$start = $this->getTargetToken('/* testClosureAssignment */', T_CLOSE_CURLY_BRACKET);
$found = self::$phpcsFile->findStartOfStatement($start);

$this->assertSame(($start - 12), $found);
$this->assertSame(($start - 11), $found);

}//end testClosureAssignment()

Expand Down Expand Up @@ -224,7 +261,7 @@ public function testArrowFunctionArrayValue()
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_COMMA);
$found = self::$phpcsFile->findStartOfStatement($start);

$this->assertSame(($start - 9), $found);
$this->assertSame(($start - 7), $found);

}//end testArrowFunctionArrayValue()

Expand Down

0 comments on commit 3361ff2

Please sign in to comment.