Bug 221701 - Automatic line break after limit of line length (PSR-2)
Automatic line break after limit of line length (PSR-2)
Status: STARTED
Product: php
Classification: Unclassified
Component: Editor
8.0
All All
: P3 with 9 votes (vote)
: TBD
Assigned To: Ondrej Brejla
issues@php
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-11-08 09:35 UTC by naitsirch
Modified: 2014-07-24 07:46 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
:


Attachments
Screenshot of formatting (249.72 KB, image/png)
2012-11-12 14:39 UTC, naitsirch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description naitsirch 2012-11-08 09:35:22 UTC
Hi,
I am not sure if this belongs to "PHP" or general "Editor" product, so please correct me if I am wrong.

In PHP we have the PHP Framework Interop Group (http://www.php-fig.org/) which tries to standardize PHP coding and I think this is great to help developer to write clean looking code.

PSR-2 says something about line length: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#23-lines

There is a "soft limit" on line length of 120 characters. It would be great if we could make this limit configurable in Netbeans in Tools -> Options -> Editor -> Formatting.
And if we write code in a line and it becomes longer that the "soft limit", Netbeans tries to break it into smaller chunks automatically.

For example:
$object->getInstance()->getBlaa()->doAnything()->doSomething()->foobar()->getObject()->setTitle('This is new title');

Becomes to:
$object->getInstance()
    ->getBlaa()
    ->doAnything()
    ->doSomething()
    ->foobar()
    ->getObject()
    ->setTitle('This is new title');

Another example:
class A
{
    public function bee(SomeClassHint $object, $parameter1, $parameter2, $anotherParameter, $default = 'do_not_know') {
        $x = 0;
        return $x;
    }
}

Becomes to:
class A
{
    public function bee(
        SomeClassHint $object,
        $parameter1,
        $parameter2,
        $anotherParameter,
        $default = 'do_not_know'
    ) {
        $x = 0;
        return $x;
    }
}

This is defined in https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#44-method-arguments


This would be a really nice feature ;-)
Comment 1 Ondrej Brejla 2012-11-08 09:43:35 UTC
Isn't Editor->Formatting->Wrapping what you are talking about? You have to invoke formatting action of course, but it can work for you too :)
Comment 2 naitsirch 2012-11-12 14:39:29 UTC
Created attachment 127631 [details]
Screenshot of formatting

Oh you are right. I did not know what "Wrapping" was for... stupid mee :-)

But as you see in my screenshot the wrappers for "Method Parameters" and "Method Call Arguments" are not working like defined in PSR-2. And it would be nice if this standards would be use as base of the formatting.

The formatter for "Chained Method Calls" seems not to work (as expected).

Additionally the lines of code in the "Preview" is very short, no long parameter lists are use, etc so that you do not see any changes, when you change the settings from "Never" to "If long" or "Always.
Comment 3 Ondrej Brejla 2012-11-12 14:44:16 UTC
I made some improvements for 7.3, so you can test it there.
Comment 4 naitsirch 2012-11-13 13:10:31 UTC
Okay, thanks. I will test it.
Comment 5 magnetik 2014-03-12 12:13:05 UTC
Using NetBeans IDE 7.4 (Build 201310111528), the option in 
Formatting > Language PHP, Category Wrapping, setting "Chained Method Calls" on "If long" does not work.

For instance :
$object->getInstance()->getBlaa()->doAnything()->doSomething()->foobar()->getObject()->setTitle('This is new title');

is not wrapped in any way. Even if it's way other the limit of 120 char.
Comment 6 magnetik 2014-04-14 15:29:50 UTC
As of netbeans 8.0, it still seems impossible to have only one argument per line when the line is too long.

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#46-method-and-function-calls
>Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.

Same for chained method call.
Comment 7 mcrawford620 2014-07-02 20:10:55 UTC
Yes, the options for the Method Call Arguments in the Wrapping category aren't enough to match the PSR-2 standard.

"If Long" will add a line break at the specified character limit, but that just puts the method call on two lines.
"Always" adds a line break after every argument, which is good for long function calls (matches the standard) but for short function calls it also adds all the line breaks, which is not good.

Either "If Long" needs to add a line break after every argument, or there needs to be another option, in order to match the standard.
Comment 8 magnetik 2014-07-24 07:46:17 UTC
Something that would be close would be a combination of "Always" and "If long" ("Always if long" ?).

I can't get to have a level of indentation when using Always: 

	public function bee(
	SomeClassHint $object, $parameter1, $parameter2, $anotherParameter,$default = 'do_not_know'
	)
	{
		//body
	}


should be:

	public function bee(
		SomeClassHint $object, $parameter1, $parameter2, $anotherParameter,$default = 'do_not_know'
	){
		//body
	}

Note the indentation of "SomeClassHint" AND the left parenthesis if on the same line as the }.

> When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them.


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo