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

Add support for PHP 7.4 arrow functions #134

Closed
jrfnl opened this issue Feb 13, 2020 · 3 comments · Fixed by #179
Closed

Add support for PHP 7.4 arrow functions #134

jrfnl opened this issue Feb 13, 2020 · 3 comments · Fixed by #179
Labels
Milestone

Comments

@jrfnl
Copy link
Collaborator

jrfnl commented Feb 13, 2020

PHP 7.4 introduces arrow functions. It would be great if this library would add support for these.

// Currently throwing false positives for undefined variables $a x 2, $b x 2, $c x 1.
$fn = fn($a, $b, $c) => $a + $b; // Unused $c (false negative).

function foo() {
    $a = 120;

	// Currently throwing false positive for undefined variable $b x 2.
    $fn1 = fn($b) => $a + $b;

	// Currently throwing false positive for undefined variable $b.
    $fn2 = fn($b) => $a; // Unused $b (false negative).

	// Currently throwing false positive for undefined variable $b x 2, $c x 1.
    $fn3 = fn($b) => $b + $c; // Undefined $c (false negative). Unused variable $fn3 (correctly thrown).
    
    var_dump( $fn1(10) );
    var_dump( $fn2(10) );
}

Note: PHPCS itself doesn't properly support arrow functions until PHPCS 3.5.5 (not yet released).
If continued support for older PHPCS versions is desired, you may want to look at the PHPCSUtils FunctionDeclarations::isArrowFunction() and FunctionDeclarations::getArrowFunctionOpenClose() methods.

Refs:

@sirbrillig sirbrillig added the bug label Feb 13, 2020
@sirbrillig
Copy link
Owner

Yes. Thanks for bringing this up. I'm very excited about PHP 7.4 but I hadn't started looking into it yet. No time like the present.

@speicus
Copy link

speicus commented May 3, 2020

Subscribing for updates. Really hoping for a fix, unfortunately having to add // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable to each arrow function really discourages us from using them more widely.

@gertjankrol
Copy link

+1

@sirbrillig sirbrillig added this to the 3.0 milestone Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants