Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect scope matching when arrow function used inside IF condition #2867

Closed
snapshotpl opened this issue Feb 12, 2020 · 6 comments
Closed
Milestone

Comments

@snapshotpl
Copy link

I run phpcs with slevomat/coding-standard and I get error:

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: scope_closer in /var/www/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 195 in /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php:606
Stack trace:
#0 /var/www/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php(195): PHP_CodeSniffer\Runner->handleErrors(8, 'Undefined index...', '/var/www/vendor...', 195, Array)
#1 /var/www/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php(137): SlevomatCodingStandard\Helpers\UseStatementHelper::getUseStatementPointers(Object(PHP_CodeSniffer\Files\LocalFile), 0)
#2 /var/www/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/UselessAliasSniff.php(40): SlevomatCodingStandard\Helpers\UseStatementHelper::getFileUseStatements(Object(PHP_CodeSniffer\Files\LocalFile))
#3 /var/www/vendor/squizlabs/php_codesniffer/src/Files/File.php(496): SlevomatCodingStandard\Sniffs\Names in /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

This is code which causes error:

<?php

class MyClass
{
    public function __construct() {
        if (
            $this->valid(fn(): bool => 2 > 1)
        ) {

        }
    }

    private function valid(callable $callable): bool
    {
        return $callable();
    }
}

First reported here slevomat/coding-standard#902

@kukulich
Copy link
Contributor

I was able to prepare a workaround but I suppose that T_CLASS token should have scope_opener and scope_closer.

slevomat/coding-standard@4c88f6c

@jrfnl
Copy link
Contributor

jrfnl commented Feb 12, 2020

Confirmed. But there's more going on here: the T_COLON for the return type of the fn arrow function within the if condition is being seen as the scope opener for the if.

I have a feeling that's what's throwing everything off.

The check for scope openers for control structures using alternative syntax should probably make sure that the close parenthesis of the control structure's open parenthesis has been seen before looking for a scope opener.

@gsherwood gsherwood modified the milestones: 3.5.6, 3.5.5 Mar 8, 2020
@gsherwood
Copy link
Member

I think this is going to require the arrow function detection code to be moved out of processAdditional and into the main process method.

@gsherwood
Copy link
Member

I think this is going to require the arrow function detection code to be moved out of processAdditional and into the main process method.

Much much easier said than done. Probably need to figure something else out.

@gsherwood
Copy link
Member

Looks like it's easier to just ensure that a scope opener has the same nested parenthesis as the scope condition. So in this case, the T_COLON is nested differently to the T_IF, so it can't be the scope opener for it.

@gsherwood gsherwood changed the title Undefined index: scope_closer Incorrect scope matching when arrow function used inside IF condition Mar 25, 2020
gsherwood added a commit that referenced this issue Mar 25, 2020
gsherwood added a commit that referenced this issue Mar 25, 2020
@gsherwood
Copy link
Member

I've pushed up a fix for this issue and PHPCS is now matching the scope openers and closers correctly for the code sample. If you get a chance to test, please let me know if there are other code snippets where this doesn't work. Thanks for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants