Php-cs-fixer: Is there support for unused USE in closures ?

Created on 20 Nov 2019  路  4Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

from :

$a = 1;
$b = 2;
$test = function() use($a, $b) {
    return $a;
}

to

$a = 1;
$b = 2;
$test = function() use($a) {
    return $a;
}
kinfeature request

Most helpful comment

another crazy case:

-$fn = function() use($a, $b) {
+$fn = function() use($a) {
     $x = "b";
     return $$x;
 };

All 4 comments

I don't think such rule exist for now.

Note that it would be a risky fixer if it applies the following change:

-$fn = function() use($a, &$b) {
+$fn = function() use($a) {
     return $a;
 };

because inheriting a variable by reference creates it if it does not exist. So the change would make the variable never created.

another crazy case:

-$fn = function() use($a, $b) {
+$fn = function() use($a) {
     $x = "b";
     return $$x;
 };

don't forget about compact as well;

<?php

$b = 123;
$a = function () use ($b) {
    return compact('b');
};

var_dump($a());

(I'm working on this fixer)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ndench picture ndench  路  3Comments

grachevko picture grachevko  路  3Comments

UksusoFF picture UksusoFF  路  3Comments

teohhanhui picture teohhanhui  路  3Comments

datenmeister picture datenmeister  路  3Comments