Skip to content

Commit

Permalink
Fixed bug #18465 : self:: does not work in lambda functions
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@310699 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
gsherwood committed May 2, 2011
1 parent 9dbb133 commit a4c44d0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Expand Up @@ -77,10 +77,13 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
// Make sure this is another class reference.
$declarationName = $phpcsFile->getDeclarationName($currScope);
if ($declarationName === $tokens[$className]['content']) {
// Class name is the same as the current class.
$error = 'Must use "self::" for local static member reference';
$phpcsFile->addError($error, $className, 'NotUsed');
return;
// Class name is the same as the current class, which is not allowed
// except if being used inside a closure.
if ($phpcsFile->hasCondition($stackPtr, T_CLOSURE) === false) {
$error = 'Must use "self::" for local static member reference';
$phpcsFile->addError($error, $className, 'NotUsed');
return;
}
}
}

Expand Down
Expand Up @@ -45,4 +45,23 @@ class SelfMemberReferenceUnitTestExample
}


class MyClass {

public static function test($value) {
echo "$value\n";
}

public static function walk() {
$callback = function($value, $key) {
// This is valid because you cant use self:: in a closure
MyClass::test($value);
};

$array = array(1,2,3);
array_walk($array, $callback);
}
}

MyClass::walk();

?>
13 changes: 13 additions & 0 deletions CodeSniffer/Tokenizers/PHP.php
Expand Up @@ -451,10 +451,23 @@ public function processAdditional(&$tokens, $eolChar)

if ($tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
$tokens[$i]['code'] = T_CLOSURE;
$tokens[$i]['type'] = 'T_CLOSURE';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$line = $tokens[$i]['line'];
echo "\t* token $i on line $line changed from T_FUNCTION to T_CLOSURE".PHP_EOL;
}

for ($x = ($tokens[$i]['scope_opener'] + 1); $x < $tokens[$i]['scope_closer']; $x++) {
if (isset($tokens[$x]['conditions'][$i]) === false) {
continue;
}

$tokens[$x]['conditions'][$i] = T_CLOSURE;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$x]['type'];
echo "\t\t* cleaned $x ($type) *".PHP_EOL;
}
}
}

continue;
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Expand Up @@ -44,6 +44,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Squiz FileCommentSniff now enforces rule that package names cannot start with the word Squiz
- Fixed issue in Squiz FileCommentSniff where suggested package name was the same as the incorrect package name
- Fixed some issues with Squiz ArrayDeclarationSniff when using function calls in array values
- Fixed bug #18465 : "self::" does not work in lambda functions
-- Also corrects conversion of T_FUNCTION tokens to T_CLOSURE, which was not fixing token condition arrays
</notes>
<contents>
<dir name="/">
Expand Down

0 comments on commit a4c44d0

Please sign in to comment.