squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Tokenizer issue on closure that returns by reference #594

Closed donatj closed 9 years ago

donatj commented 9 years ago

I was using PSR1.Files.SideEffects and it was failing on code using a closure that returned by reference.

I did some poking in that Sniff and from what I can understand it's not the Sniff's fault, it's the tokenizers fault.

I determined that in the following which passes there is no T_FUNCTION symbol, but there is a T_CLOSURE

<?php
echo "test";
$b = function () {
    echo "hello";
};

However with this it fails, which simply changes the closure to return by reference, there is a T_FUNCTION and no T_CLOSURE

<?php
echo "test";
$b = function &() {
    echo "hello";
};

I believe the issue is somewhere around Tokenizers/PHP.php:862 but without taking a deep dive into the code it is beyond my ability to fix - otherwise I would have submitted a pull request.

                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;
                    }
gsherwood commented 9 years ago

Thanks for reporting. I've fixed the problem in the tokenizer.