netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

[PHP] Implement full PSR/PER code style and use by default

Open terax6669 opened this issue 2 years ago • 12 comments

Description

Some things that I noticed, but I'm sure there are more:

  • Formatting rules for PHP should adhere to PSR by default
  • ~~Formatting rules in the Blank Lines category seem to be impossible to adhere to PSR. Example with traits - according to PSR traits should be the first thin inside a class. We have Before Use Trait but not After. Therefore I must set Before Function to 1 to separate traits and functions. Now even if I set After Class Header to 0 I will get a blank line when I have a method inside a class but no trait (which would be the most common scenario).~~ Impemented in #6467.
<?php

class Foo
{
    use Bar;

    public function foobar()
    {
    }
}

// vs

class Foo
{
// this blank line shouldn't be here!
    public function foobar()
    {
    }
}
  • Templates/Scripting/PHPClass.php class opening bracket should be on a new line
  • Same thing for the cls code template and the constructor method inside (it's hidden by the fact that default settings in NetBeans reformat code templates on completion)
  • Related: #4609
  • Related: #4641

Use case/motivation

PSR is the de facto standard for PHP.

There are a few things in PSR that are not available in NetBeans. It would be great to fix those and also set it as default formatting for PHP files in the next version!

Related issues

  • #4609
  • #4611
  • #4641
  • #5186
  • #6715
  • #6716
  • #6717
  • #7524

Are you willing to submit a pull request?

No

Code of Conduct

Yes

terax6669 avatar Sep 25 '22 15:09 terax6669

+1. With each passing day, it gets harder to find non-PSR-12 code. Netbeans is wonderful, but you cannot use it out of the box if you work in a team where people use multiple IDE's and PSR-12 is a must.

erickcomp avatar Sep 26 '22 16:09 erickcomp

Hi guys! Is there any plan on implementing this? I dare to say that one cannot code modern PHP app with Netbeans without having PSR-12 support nowadays.

erickcomp avatar Nov 08 '22 06:11 erickcomp

@erickcomp I think if we treat this ticket as an epic task and break it down into smaller chunks, the devs would be able to implement it step by step. I already listed some related issues.

If you notice any incompatibilities please make a separate issue on it and I'll add it to the issue description.

terax6669 avatar Dec 05 '22 17:12 terax6669

Issue #4611 is related to blank lines in class.

KacerCZ avatar Jan 15 '23 18:01 KacerCZ

Just to reference up-to-date recommendation, PSR-12 was replaced by PER coding style. https://www.php-fig.org/per/coding-style/

KacerCZ avatar Jan 30 '23 07:01 KacerCZ

Just adding my 2 cents to the discussion, implementing and maintaining native full compliance to PSR or its successors seems like quite a big task and that's not even the single coding standard in use. Various reasons, including those outside of our control, may lead to other standards being implemented by different projects.

Perhaps this task could be delegated to PHPCS. It is very fast and there is already a basic integration of it. NB could ship (or be able to download) rule sets for popular standards to use with projects that don't come with their own sets. It should be even possible to display CS errors inline, much like the PHP parser's. Going this way might prove a simpler alternative, while giving a freedom to choose specific standards to the users at the same time.

czukowski avatar Jul 19 '23 13:07 czukowski

+1 To adding After Use Trait

zkwbbr avatar Sep 07 '23 19:09 zkwbbr

It seems we have (most of) the options to adhere to the per coding-style. Noob question: Is it hard to make these configs the default when you install the IDE?

erickcomp avatar Nov 23 '23 01:11 erickcomp

I'd say the settings are very obtuse, but some trial and error will allow you to set it up properly.

terax6669 avatar Nov 23 '23 01:11 terax6669

I see. But can we get very close to PER coding style, right? If so, could we get this settings out of the box on NB? I mean, when we install it for the first time, we got formatting options as close as possible to the PER coding style.

erickcomp avatar Nov 23 '23 11:11 erickcomp

If you do, could you post a list of all the changes you have to make from the default config? Would make it easier to implement that.

As for whether that would be the default... I think it should, but I'm not making decisions. IDK how that works, maybe Junichi can weigh in.

terax6669 avatar Nov 23 '23 11:11 terax6669

I'm planning to take some time to create this config in the next few weeks. I'll let you guys know!

erickcomp avatar Nov 25 '23 12:11 erickcomp