Convert closure to arrow function

In a lot of coding tutorials I see people hitting Alt+Enter and selecting: "Convert closure to arrow function". However, this is not showing in my PHPstorm editor (2021.3.1).

See for example my screenshot below. Any idea how to enable this? Thanks!

5 comments
Comment actions Permalink

@joris Try settings>PHP and change the language level. Mine was set on 7.3 out of the box so when I tried to manually convert a closure to arrow it gave me an error stating that I had to have 7.4 or higher. I changed it to 8.1 since that is the only PHP installed on my system. 

 

2
Comment actions Permalink

It'd be great if you could share the code sample as text.
I can't reproduce that in 2021.3.1 on the following snippet:

<?php
$posts = function($file) {
    return new stdClass();
};

Invoking Alt+Enter on function shows me the quick-fix.

0
Comment actions Permalink

Does this need to be enabled somewhere in the IDE? Eugene Morozov I am reproducing it with your code. I tried with the first code below and when it didn't work I googled and that brought me here. So I tried your code too. There is no option to convert closure to arrow function. I'm running a fresh install of PHPStorm 2021.3.1 and PHP -v 8.1.1 on a fresh new Laravel 8  app in the routes file. Thanks

$post = cache()->remember("posts.{$slug}", 3600, function () use ($path) {
return file_get_contents($path);
});

$posts = function ($file) {
return new stdClass();
};
0
Comment actions Permalink

I found it under settings>editor>inspections>php>code style> closure can be converted to arrow function but it is already enabled in both project and IDE default and still not working.

0
Comment actions Permalink

Isimmons33 I changed PHP version to 8.1 and now it's working. Thanks for providing the solution! 

0

Please sign in to leave a comment.