Not seeing how to easily set Laravel Code Style to PSR-2 and PSR-4

Laravel follows the PSR-2 coding standard and the PSR-4 autoloading standard: https://laravel.com/docs/5.7/contributions#coding-style

However I'm not seeing an option to set PSR-2 and PSR-4 standards (nor do I know if it really should have any other standards)...

There's no "Laravel" option in Code Style anywhere, and setting it to PSR-1/PSR-2 does odd things in the example code:

So, I'm trying to avoid manually configuring everything to have something compliant for easy coding (and minimized conflicts) among team members.

10 comments
Comment actions Permalink

Sorry, I can't see what are those odd things from just a screenshot.

It would be great if you could somehow illustrate what is wrong with built-in PSR1/PSR2 style.

 

PSR-4 is just about FQCN and file loading - it mostly does not relate to code style. We are currently working on all the PSR-4 related aspects:

https://youtrack.jetbrains.com/issue/WI-25581

https://youtrack.jetbrains.com/issue/WI-39151

https://youtrack.jetbrains.com/issue/WI-38310

https://youtrack.jetbrains.com/issue/WI-39147

and others

1
Comment actions Permalink

Dmitry, do you notice function foo has its parentheses split on two lines? That's neither PSR-2 nor 4. Can you explain which PSR that is or why it did that?

I simply want to try to follow Laravel code style, which you can see here: https://github.com/laravel

Their docs mention PSR-2 and 4, so however those apply to code style, I'd like PHPStorm to let me set and use.

0
Comment actions Permalink

Can you explain which PSR that is or why it did that?

I can't reproduce that after setting the predefined style to PSR-1/PSR-2.

Is it only shown as such in the preview pane, or the actual code is also being reformatted in this way?

I simply want to try to follow Laravel code style

By default, PhpStorm doesn't include this code style. The Laravel plugin also doesn't supply it.

But, there are user-defined code styles you can import to avoid manual configuration:

https://www.google.com/search?q=phpstorm%20laravel%20code%20style

0
Comment actions Permalink

OK so it isn't reproducing after I've opened into another project today.

However if Laravel uses PSR-2 and PSR-4, and PSR-4 doesn't apply to code style, then isn't PSR-1/PSR-2 the same thing as Laravel style?

Just clarifying because Eugene says PHPStorm doesn't include Laravel code style; but if it has a pre-defined setting that does conform to its style, then it actually does include Laravel's current code style (it doesn't need to be labeled as such for it to be the same).

0
Comment actions Permalink

isn't PSR-1/PSR-2 the same thing as Laravel style?

0
Comment actions Permalink

I wasn't saying that Larevel = PSR-2... I'm just trying to find a way to set PHPStorm to follow Laravel's current code style standards, and so far I'm not seeing a non-manual configuration for this that matches, or I'm finding bugs where PHPStorm is not following the PSR-1/PSR-2 standards when set!

I was able to reproduce the issue I mentioned, even for Laravel, as shown here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360002531360-Code-Style-function-and-for-parameters-formatting-on-multiple-lines?page=1#community_comment_360000311500

In whatever case, PHPStorm is doing something wrong, and while I try to figure this out manually, I would simply like to know if anyone has a pre-configured Laravel-compliant code-style setting already that they can export and I can import. Thank you.

0
Comment actions Permalink

In whatever case, PHPStorm is doing something wrong, and while I try to figure this out manually, I would simply like to know if anyone has a pre-configured Laravel-compliant code-style setting already that they can export and I can import. Thank you.

Not sure if you missed the link or just didn't like it, but as I mentioned, there are user-defined code style schemes available on GitHub: https://www.google.com/search?q=phpstorm%20laravel%20code%20style

0
Comment actions Permalink

I did see the link and already had done searches like that. The first result - https://github.com/michaeldyrynda/phpstorm-laravel-code-style - says it's for PSR-2... no mention of PSR-4. The second result - https://laracasts.com/discuss/channels/general-discussion/code-style-preset-for-phpstorm-8?page=1 - is for Laravel 4.2 and from 2015. The third one is just an article about the first. The fourth links to JetBrains' Laravel Help page and doesn't talk about code style. The fifth is another JetBrains page about general Code Style which just underlies the screenshot I put in the first post (meaning, it's not specific enough), and which I also pointed out doesn't work correctly with Laravel's PSRs anyways. The next link points to this thread... which is a bad sign. The next one is an article from talking about the fourth link with no mention of code style, just code completion in the plugins. And the next one - https://laraveldaily.com/how-to-configure-phpstorm-for-psr-2/ - is also from 2015.

So, if all of these results are from 2015 or only mentioning PSR-2 and simply recommend all of the stuff I've already tried, and there's still no setting in PHPStorm to configure code style to Laravel 5.7's PSR recommendations (which I'm assuming the php artisan make command conforms to), then I'm going to fall back on this being either a bug in PHPStorm, an oversight by JetBrains, or a bug in one of the programs.

Right now the only option I'm seeing is to manually configure it or try these old things... what I was looking for here was someone who'd already gotten this to work on the latest-enough Laravel in PHPStorm, but it looks like I'll have to try the thing from 2-4 years ago that doesn't mention half of the spec Laravel mentions, the https://github.com/michaeldyrynda/phpstorm-laravel-code-style plugin, but as I said it doesn't look assured and I'm a little wary of causing new bugs or even corruption with an old plugin, what do I know...

0
Comment actions Permalink

the thing from 2-4 years ago that doesn't mention half of the spec Laravel mentions

0
Comment actions Permalink

It looks like it's possible to match code style better in the settings, as the PSR-1/PSR-2 pre-defined config does things differently than PSR-1/PSR-2, which are configurable and which I was trying to demonstrate in the screenshots, so it's not true that the predefined PSR-1/PSR-2 settings are configured as best as possible.

Here's two comparisons of pre-PSR-1/PSR-2 pre-defined config and post:

Notice how parentheses are put on their own lines, and `function bar`, the `foreach (array`, the `$x = array(`, and the `$zz = array(` show their params/values on their own lines? That's neither a PSR-1 nor PSR-2 setting... those are just the obvious examples. To fix those, I have to go to "Function declaration parameters" and uncheck "New line after '('", "Place ')' on new line", and "Keep ')' and '{' on one line", along with "wrap" settings to match the PSRs for everybody and Laravel. 

So as far as I can tell, JetBrains is either miconfiguring PSR-1/PSR-2 or configuring it to their own taste against some obvious examples in the PSRs, and in a way that adds a ton of extraneous lines and frankly makes the code harder to read and bloated (going against the point of PSRs).

I honestly don't mind if you like the preconfiguration, but please stop saying it's accurate or "as best as possible", because it's demonstrably not, and I'm looking for a quick solution (such as some other selection) but I don't think there is one, I'll just have to manually configure and export/import for each relevant project.

0

Please sign in to leave a comment.