Skip to content

Commit 8d2363d

Browse files
authored
Merge pull request #394 from rodrigoprimo/test-coverage-call-time-pass-by-reference
Generic/CallTimePassByReference: support anonymous classes and improve code coverage
2 parents 271a13a + b91a18b commit 8d2363d

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function register()
2727
return [
2828
T_STRING,
2929
T_VARIABLE,
30+
T_ANON_CLASS,
3031
];
3132

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

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

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

@@ -69,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr)
6970
true
7071
);
7172

72-
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
73+
if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
7374
return;
7475
}
7576

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

8889
while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) {
89-
if (isset($tokens[$nextSeparator]['nested_parenthesis']) === false) {
90-
continue;
91-
}
92-
9390
if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) {
9491
$nextSeparator = $tokens[$nextSeparator]['bracket_closer'];
9592
continue;

src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.inc renamed to src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.1.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ myfunc(MY_CONST&$myvar);
3737

3838
efg( true == &$b );
3939
efg( true === &$b );
40+
41+
foo($a, bar(&$b));
42+
foo($a, array(&$b));
43+
44+
enum Foo {}
45+
interface Foo {}
46+
trait Foo {}
47+
48+
$instance = new $var($a);
49+
$instance = new MyClass($a);
50+
$instance = new $var(&$a);
51+
$instance = new MyClass(&$a);
52+
53+
$anon = new class($a) {};
54+
$anon = new class(&$a) {};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered when there are only empty tokens after a variable and nothing else.
6+
7+
$var
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing closing parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
foo(

src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,30 @@ final class CallTimePassByReferenceUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the test file to process.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='CallTimePassByReferenceUnitTest.1.inc')
3234
{
33-
return [
34-
9 => 1,
35-
12 => 1,
36-
15 => 1,
37-
18 => 2,
38-
23 => 1,
39-
30 => 1,
40-
];
35+
switch ($testFile) {
36+
case 'CallTimePassByReferenceUnitTest.1.inc':
37+
return [
38+
9 => 1,
39+
12 => 1,
40+
15 => 1,
41+
18 => 2,
42+
23 => 1,
43+
30 => 1,
44+
41 => 1,
45+
50 => 1,
46+
51 => 1,
47+
54 => 1,
48+
];
49+
50+
default:
51+
return [];
52+
}
4153

4254
}//end getErrorList()
4355

0 commit comments

Comments
 (0)