Skip to content

Commit

Permalink
Merge pull request #394 from rodrigoprimo/test-coverage-call-time-pas…
Browse files Browse the repository at this point in the history
…s-by-reference

Generic/CallTimePassByReference: support anonymous classes and improve code coverage
  • Loading branch information
jrfnl committed Apr 22, 2024
2 parents 271a13a + b91a18b commit 8d2363d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function register()
return [
T_STRING,
T_VARIABLE,
T_ANON_CLASS,
];

}//end register()
Expand All @@ -50,12 +51,12 @@ public function process(File $phpcsFile, $stackPtr)

$prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true);

// Skip tokens that are the names of functions or classes
// Skip tokens that are the names of functions
// within their definitions. For example: function myFunction...
// "myFunction" is T_STRING but we should skip because it is not a
// function or method *call*.
$prevCode = $tokens[$prev]['code'];
if ($prevCode === T_FUNCTION || $prevCode === T_CLASS) {
if ($prevCode === T_FUNCTION) {
return;
}

Expand All @@ -69,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr)
true
);

if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
return;
}

Expand All @@ -86,10 +87,6 @@ public function process(File $phpcsFile, $stackPtr)
];

while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) {
if (isset($tokens[$nextSeparator]['nested_parenthesis']) === false) {
continue;
}

if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) {
$nextSeparator = $tokens[$nextSeparator]['bracket_closer'];
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ myfunc(MY_CONST&$myvar);

efg( true == &$b );
efg( true === &$b );

foo($a, bar(&$b));
foo($a, array(&$b));

enum Foo {}
interface Foo {}
trait Foo {}

$instance = new $var($a);
$instance = new MyClass($a);
$instance = new $var(&$a);
$instance = new MyClass(&$a);

$anon = new class($a) {};
$anon = new class(&$a) {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error.
// This should be the only test in this file.
// Testing that the sniff is *not* triggered when there are only empty tokens after a variable and nothing else.

$var
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

foo(
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,30 @@ final class CallTimePassByReferenceUnitTest 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='CallTimePassByReferenceUnitTest.1.inc')
{
return [
9 => 1,
12 => 1,
15 => 1,
18 => 2,
23 => 1,
30 => 1,
];
switch ($testFile) {
case 'CallTimePassByReferenceUnitTest.1.inc':
return [
9 => 1,
12 => 1,
15 => 1,
18 => 2,
23 => 1,
30 => 1,
41 => 1,
50 => 1,
51 => 1,
54 => 1,
];

default:
return [];
}

}//end getErrorList()

Expand Down

0 comments on commit 8d2363d

Please sign in to comment.