crazyfactory/php-sniffs

BlankLineBeforeReturnSniff crashes with undefined offset -1

wmathes opened this issue · 6 comments

When running dopr lint:fix on the current ERP master branch (using sniffs 1.0.2), the following Exception is thrown:

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined offset: -1 in /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php on line 53 in /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php on line 557

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined offset: -1 in /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php on line 53 in /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php on line 557

Call Stack:
    0.0004     352072   1. {main}() /var/www/project/vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0372    1610408   2. PHP_CodeSniffer\Runner->runPHPCBF() /var/www/project/vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    0.2236    4514472   3. PHP_CodeSniffer\Runner->run() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:193
   52.5860    7914816   4. PHP_CodeSniffer\Runner->processFile() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:397
   52.6011    8091744   5. PHP_CodeSniffer\Reporter->cacheFileReport() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:606
   52.6011    8110200   6. PHP_CodeSniffer\Reports\Cbf->generateFileReport() /var/www/project/vendor/squizlabs/php_codesniffer/src/Reporter.php:262
   52.6011    8110200   7. PHP_CodeSniffer\Fixer->fixFile() /var/www/project/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php:48
   52.6035    8125136   8. PHP_CodeSniffer\Files\LocalFile->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Fixer.php:179
   52.6035    8125136   9. PHP_CodeSniffer\Files\File->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:94
   52.6148    8284616  10. CrazyFactory\Sniffs\Formatting\BlankLineBeforeReturnSniff->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Files/File.php:439
   52.6149    8286536  11. PHP_CodeSniffer\Runner->handleErrors() /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php:53

Will try to find the exact code sample causing the crash

class AbstractReport
{
    protected $ctx;
    private $progress_listener;

    const LIMIT_INFINITE = -1;
    const MIMETYPE_EXCEL = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

    public function __construct(Context $ctx)
    {
        $this->ctx = $ctx;
    }

    protected function reportProgress($message)
    {
        if (is_callable($this->progress_listener))
        {
            $c = $this->progress_listener;
            $c($message);
        }
    }

    public function setProgressListener(\Closure $callable)
    {
        $this->progress_listener = $callable;
    }

    public function getMemoryLimit() { return null; }
    public function getTimeLimit() { return null; }
    public function getContentType() { return null; }
    public function getFilename() { return null; }
    public function run() { return null; }
}

this one is the origin :)

still happening at Line 98


Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined offset: -1 in /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php on line 98 in /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php on line 557

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined offset: -1 in /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php on line 98 in /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php on line 557

Call Stack:
    0.0003     352072   1. {main}() /var/www/project/vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0854    1611168   2. PHP_CodeSniffer\Runner->runPHPCBF() /var/www/project/vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    1.4178    4519456   3. PHP_CodeSniffer\Runner->run() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:193
   19.1539    9533944   4. PHP_CodeSniffer\Runner->processFile() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:397
   19.2047   10101800   5. PHP_CodeSniffer\Reporter->cacheFileReport() /var/www/project/vendor/squizlabs/php_codesniffer/src/Runner.php:606
   19.2047   10120256   6. PHP_CodeSniffer\Reports\Cbf->generateFileReport() /var/www/project/vendor/squizlabs/php_codesniffer/src/Reporter.php:262
   19.2047   10120256   7. PHP_CodeSniffer\Fixer->fixFile() /var/www/project/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php:48
   19.2061   10136376   8. PHP_CodeSniffer\Files\LocalFile->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Fixer.php:179
   19.2061   10136376   9. PHP_CodeSniffer\Files\File->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:94
   19.2555   10701880  10. CrazyFactory\Sniffs\Formatting\BlankLineBeforeReturnSniff->process() /var/www/project/vendor/squizlabs/php_codesniffer/src/Files/File.php:439
   19.2559   10702256  11. CrazyFactory\Sniffs\Formatting\BlankLineBeforeReturnSniff->getReturnPosition() /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php:78
   19.2559   10703800  12. PHP_CodeSniffer\Runner->handleErrors() /var/www/project/vendor/crazyfactory/sniffs/src/CrazyFactory/Sniffs/Formatting/BlankLineBeforeReturnSniff.php:98

Could you please revert the code to its original before you simplify the logic ?
That fix did change the logic which causes the error because $current variable can become -1;
With my version, $current will never get lower than 0;

ce2b461

After reverting the code back, the bug should be fixed. I tested with new-erp project with no errors. And to make sure linting does not produce weird linting result please follow the solution posted in
#34

Closing... this issue was fixed after reverting.