Skip to content

fmt producing bad code - new without parentheses #20

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

Closed
bendavies opened this issue Dec 10, 2024 · 9 comments
Closed

fmt producing bad code - new without parentheses #20

bendavies opened this issue Dec 10, 2024 · 9 comments
Assignees

Comments

@bendavies
Copy link
Contributor

hi

in my composer.json I have

"php": "^8.3",

fmt on is formatting code valid for
https://wiki.php.net/rfc/new_without_parentheses
which is only valid for 8.4

It shouldn't do this, right?

@nhedger
Copy link
Contributor

nhedger commented Dec 10, 2024

You may be correct, but I think including a minimal reproduction snippet would help.

@azjezz
Copy link
Member

azjezz commented Dec 10, 2024

can you please share a code example that results in PHP 8.4 only syntax? i tried couple of options but i couldn't get it to output new without parens.

ref:

if let Expression::Instantiation(new) = expression {
if new.arguments.is_none() {
// parentheses are required if the instantiation has no arguments
// e.g. `new Foo->baz()` should be `(new Foo)->baz()`
return true;
}
// parentheses are not required if the instantiation has arguments
// e.g. `new Foo()->baz()`.
//
// but this is only allowed in PHP 8.4, so for now, we add
// parentheses to be safe, in the future, we can add an option
// to remove them.
//
// TODO(azjezz): we should add an option to remove parentheses.
return true;

@azjezz azjezz added the bug label Dec 10, 2024
@azjezz azjezz self-assigned this Dec 10, 2024
@bendavies
Copy link
Contributor Author

bendavies commented Dec 10, 2024

https://github.com/brick/date-time/blob/db33701540dd80d8c55d529d697582688f9e4cbb/src/Parser/IsoParsers.php#L41

git clone [email protected]:brick/date-time.git
cd date-time
cp ~/Downloads/mago-0.0.6-aarch64-apple-darwin/mago .
curl https://raw.githubusercontent.com/carthage-software/mago/refs/heads/main/examples/mago-full.toml -o mago.toml
chmod +x mago
xattr -rd com.apple.quarantine mago
./mago fmt
git diff src/Parser/IsoParsers.php

@bendavies
Copy link
Contributor Author

bendavies commented Dec 10, 2024

there some other interesting diffs here...
different issue but this doesn't look equivalent. or is it?
image

@jdreesen
Copy link

jdreesen commented Dec 10, 2024

I think it is equivalent, because the operator precedence of && is higher than ||.

But I think the parenthesis should stay, as it could be confusing otherwise.

@bendavies
Copy link
Contributor Author

bendavies commented Dec 10, 2024

indeed, confirmed equivalent with the test suite and modifying it further.
but yes, hard to grok without the parens.

@azjezz
Copy link
Member

azjezz commented Dec 10, 2024

@bendavies the reason that paren is removed is that we actually remove all parens from around expressions, and put them back only when needed.

we can add a setting to add parens in that case.

ref:

fn need_parens(&mut self, node: Node<'a>) -> bool {

@bendavies
Copy link
Contributor Author

bendavies commented Dec 10, 2024

No problem, it's not a bug. Can raise another issue. I hope my reproducer on the original issue was good for you.

@azjezz
Copy link
Member

azjezz commented Dec 10, 2024

fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants