Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP wrong format indentation #7462

Open
riksoft opened this issue Jun 12, 2024 · 9 comments
Open

PHP wrong format indentation #7462

riksoft opened this issue Jun 12, 2024 · 9 comments
Assignees
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Milestone

Comments

@riksoft
Copy link
Contributor

riksoft commented Jun 12, 2024

Apache NetBeans version

Apache NetBeans 22

What happened

The code inside a function influences how is formatted the code into another function when making use of ALT+Shift+F
I already filed this bug that is a similar problemi for JS (#7243) but this one I think it's even worse.

Language / Project Type / NetBeans Component

PHP project

How to reproduce

(1) Comment the line
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
and press ALT Shift+F
You'll see "Array(" in the function stuff() moving in the right position.
(2)
Remove the comment from
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
and "array(" will move again in the bad position.
(3)
In the line
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
change "test3" with "test33333333333333333333" and you'll see "array(" moving further right.
I haven't found any workaround to solve this problem.

<?php

class test
{
	public static function test($res)
	{
		$test4 = [1, 2, 3, 4];

		// The lenght of this line changes the format position of "array("
		// in function stuff() :-o !!!
		// E.g. try to change test3 into test333333
		// and format the code again: "array(" will be moved further right.
		// Or simply comment the following line and reformat
		$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];

		return $res;
	}


	public static function stuff()
	{
		return array_merge(self::registerRules(),
										 array(
			 'opt1' => FILTER_SANITIZE_NUMBER_INT,
			 'opt2' => FILTER_SANITIZE_NUMBER_INT
		  )
		);
	}
}

I'd like "array(" in the return of stuff() to stay on the first line, like this

return array_merge(self::registerRules(), array(
			 'opt1' => FILTER_SANITIZE_NUMBER_INT,
			 'opt2' => FILTER_SANITIZE_NUMBER_INT
		  )

or something like this

return array_merge(self::registerRules(),
				array(
					'opt1' => FILTER_SANITIZE_NUMBER_INT,
					'opt2' => FILTER_SANITIZE_NUMBER_INT
				)
		);

But that's not the point: regardless of what I expect to see, the problem is that "array(" moves right according to the length of
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
in the other function. That's quite weird.

PS: If this could help, I format all my code with tabs, not spaces.

Did this work correctly in an earlier version?

No / Don't know

Operating System

Debian Bookworm (12)

JDK

Java: 17.0.11; OpenJDK 64-Bit Server VM 17.0.11+9-Debian-1deb12u1 - Runtime: OpenJDK Runtime Environment 17.0.11+9-Debian-1deb12u1

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

No response

Are you willing to submit a pull request?

No

Attached

Screenshot of the editor formatting options
f2
f1

zip with export of formatting options
format.zip

@riksoft riksoft added kind:bug Bug report or fix needs:triage Requires attention from one of the committers labels Jun 12, 2024
@riksoft
Copy link
Contributor Author

riksoft commented Jun 12, 2024

I've noted that the problem is the use of implode()
E.g the problem does not happen turning
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
into

$sTest4 = implode(',', $test4);
$res = ['test1' => 1, 'test2' => 2, 'test3' => $sTest4];

@troizet troizet added the PHP [ci] enable extra PHP tests (php/php.editor) label Jun 12, 2024
@junichi11
Copy link
Member

I can't reproduce it.
Please write your formatting options, an expected result, and actual result.

@riksoft
Copy link
Contributor Author

riksoft commented Jun 13, 2024

I've modified the first post adding the info about formatting (screenshots + format setting export) + what I would expect.

@junichi11
Copy link
Member

Could you attach an example project as a zip file?

  1. Create a new project
  2. Write an example code
  3. Project properties > Formatting > Use project specific options
  4. Set your formatting option to reproduce it
  5. Zip your example project
  6. Attach it to this issue

@riksoft
Copy link
Contributor Author

riksoft commented Jun 14, 2024

Your idea about using Project properties > Formatting > Use project specific options (that I didn't even know it existed :-) ) was the key to start comparing the global with the specific settings (that was not affected by such problem).
So comparing and trying I've found the problem is here:

j1

Actually not even the default is good because it causes this problem

nowrap

The best is
Setting Never + "wrap after (" and "wrap )"

And this solves the problem. I'm gonna set it in the global option as well! Doesn't split unless I do and if I do it wraps well.

final

@junichi11
Copy link
Member

Please attach your example project.

@riksoft
Copy link
Contributor Author

riksoft commented Jun 17, 2024

For unknown reason I wasn't able to have back the exact bad behaviour mentioned before. It's almost the same though. The only difference is that before the problem was only on the
line self::registerRules(), array(
with the keyword array moving far right depending on the
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
lenght
while in this example it's even worse because the line
$res = ['test1' => 1, 'test2' => 2, 'test3' => implode(',', $test4)];
that in the original case was staying put, in this case $test4 goes on the next line.

junichi.tar.gz

@junichi11 junichi11 removed the needs:triage Requires attention from one of the committers label Jun 17, 2024
@junichi11
Copy link
Member

Reproducible. Thanks!

@riksoft
Copy link
Contributor Author

riksoft commented Jun 19, 2024

Thanks to you, you're doing a great job!

@junichi11 junichi11 self-assigned this Jun 24, 2024
@junichi11 junichi11 added this to the NB23 milestone Jun 24, 2024
@matthiasblaesing matthiasblaesing modified the milestones: NB23, NB24 Oct 1, 2024
@matthiasblaesing matthiasblaesing modified the milestones: NB24, NB25 Dec 14, 2024
@junichi11 junichi11 modified the milestones: NB25, NB26 Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

No branches or pull requests

4 participants