Skip to content

Commit f947742

Browse files
authored
Merge pull request #827 from PHPCSStandards/feature/pear-functiondeclaration-prevent-fixer-conflict
PEAR/FunctionDeclaration: prevent fixer conflict for unfinished closures/live coding
2 parents f117c2b + 791e61b commit f947742

5 files changed

+29
-4
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ public function process(File $phpcsFile, $stackPtr)
103103
// enforced by the previous check because there is no content between the keywords
104104
// and the opening parenthesis.
105105
// Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
106-
// by checking for the scope_opener.
106+
// by checking if the function has a name.
107107
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
108-
if ($tokens[$stackPtr]['code'] === T_FUNCTION
109-
&& (isset($tokens[$stackPtr]['scope_opener']) === true || $methodProps['has_body'] === false)
110-
) {
108+
$methodName = $phpcsFile->getDeclarationName($stackPtr);
109+
if ($tokens[$stackPtr]['code'] === T_FUNCTION && $methodName !== null) {
111110
if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) {
112111
$spaces = 'newline';
113112
} else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error/live coding.
4+
// Ensuring that the sniff does not get into a fixer conflict with itself for unfinished closure declarations
5+
// (missing close parenthesis for import use).
6+
// This must be the only test in this file.
7+
$closure = function (string $param) use ($var
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error/live coding.
4+
// Ensuring that the sniff does not get into a fixer conflict with itself for unfinished closure declarations
5+
// (missing close parenthesis for import use).
6+
// This must be the only test in this file.
7+
$closure = function(string $param) use ($var
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error/live coding.
4+
// Ensuring that the sniff does not get into a fixer conflict with itself for unfinished closure declarations
5+
// (missing close parenthesis for import use).
6+
// This must be the only test in this file.
7+
$closure = function (string $param) use ($var

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public function getErrorList($testFile='')
124124
48 => 1,
125125
];
126126

127+
case 'FunctionDeclarationUnitTest.4.inc':
128+
return [
129+
7 => 1,
130+
];
131+
127132
default:
128133
return [];
129134
}//end switch

0 commit comments

Comments
 (0)