Phpinspectionsea: [Feature Request] suggest static anonymous functions

Created on 25 Sep 2018  路  8Comments  路  Source: kalessil/phpinspectionsea

| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Issue type | Feature request |
| Plugin | Php Inspections (EA Extended) or Php Inspections (EA Ultimate), any version |
| Language level | e.g. PHP 5.4 |

Current behaviour (description/screenshot:)

no behaviour

Expected behaviour

if anonymous function doesn't use $this suggest to make it static, for example
from this

$this->app->bind($repository, function () use ($entity) {
    return \EntityManager::getRepository($entity);
});

to this

$this->app->bind($repository, static function () use ($entity) {
    return \EntityManager::getRepository($entity);
});
enhancement fixed

Most helpful comment

Hello all! This is a neat idea, but keep in mind that static closures cannot be used in all the same situations that non-static closures can. E.g.:

$o = new \stdClass();
$c = static function () { };
$d = $c->bindTo($o);

This gives a warning: "Cannot bind an instance to a static closure", but this would be valid if $c were not marked static.

All 8 comments

Wow! I even know that it is possible. haha
It have some performance advantage?

@rentalhost propaply yes, have not tested yet :smile:

I did a local test, and it speed up ~13%, which make this feature a good catch for me. 馃槃

Holy shit, how this possible at all. I mean since which PHP version?

@kalessil since php 5.4 see docs

Thank you, @vladyslavstartsev. Will check docs for side-effects.

Hello all! This is a neat idea, but keep in mind that static closures cannot be used in all the same situations that non-static closures can. E.g.:

$o = new \stdClass();
$c = static function () { };
$d = $c->bindTo($o);

This gives a warning: "Cannot bind an instance to a static closure", but this would be valid if $c were not marked static.

Implemented!

@kwvanderlinde: confirmed, let's wait for first bug-reports and only then handle the false-positive.

Was this page helpful?
0 / 5 - 0 ratings