Pest: Allow usage of static closures

Created on 13 Oct 2020  路  7Comments  路  Source: pestphp/pest

When writing a test which contains no $this keyword, it would be awesome if we could define them as static.

it('can contain work', static function () {
    expect(Str::containsWord('test & word', 'test'))->toBeTrue();
});

All 7 comments

What would be the benefit from it?

Micro-op? 馃 PhpStorm always is complaining to me about this.

Yeah so the reason is basically that static functions are faster, which means static analysis would always recommend you to write it as a static function when possible.

With the usage of expect and so on we are actually not depending on $this, so it can be static 馃憤

Realistically, how much faster are they (genuinely interested if there's a good blog)? 馃檪

@owenvoke Read about it awhile ago, so can't remember the blog I found about it 馃檪

However I don't think the performance difference is of any significance. This is mostly about satisfying static analysis, else all of the static analysis tools have to get a condition in for not checking for static functions in pest tests

Sounds good. 馃憤馃徎 Thanks for the info.

I've no plans on working on this. Feel free to submit a pull request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Synchro picture Synchro  路  5Comments

dingo-d picture dingo-d  路  6Comments

nunomaduro picture nunomaduro  路  7Comments

AlexMartinFR picture AlexMartinFR  路  6Comments

nunomaduro picture nunomaduro  路  7Comments