Skip to content
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

Generic/CallTimePassByReference: support anonymous classes and improve code coverage #394

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