Plugin-php: Prettier added useless parenthese

Created on 11 Sep 2018  ·  16Comments  ·  Source: prettier/plugin-php

-        return static::class . '@' . $method;
+        return (static::class . '@' . $method);
enhancement

Most helpful comment

Why we should avoid parens?

Because there is not a single person anywhere doing it ?

All 16 comments

Idem :

-        return [
+        return (
+            [
                 'email' => 'required|string|email|max:255|unique:users',
                 'password' => 'required|string|min:6|confirmed',
-            ] + self::profilesRules();
+            ] + self::profilesRules()
+        );

@mathieutu return always avoid parens when expression is in one line and add parens when expression is multiline

@mathieutu

return (static::class . '@' . $method);

look as bug.

Second example expected.

Idem here:

-        return $this->customer->paymentService ?? null;
+        return ($this->customer->paymentService ?? null);

Imo in php, we should not add parentheses in any return cases. (Never seen that in any php code)

@mathieutu already fixed in master, here you examples https://github.com/prettier/plugin-php/pull/612, it is expected right?

@mathieutu

Imo in php, we should not add parentheses in any return cases. (Never seen that in any php code)

Disagree. return have same logic in js and php. Why we should avoid parens?

Why we should avoid parens?

Because there is not a single person anywhere doing it ?

@Meroje please create issue in prettier about this, we use logic from prettier.

/cc @vjeux can you describe why we need parens for return, thanks

In JavaScript, there’s a “feature” called automatic semi-colon insertion that makes the following

return
  123

be parsed as

return;
123;

So you have to have parenthesis if you want to make it multiline otherwise you return undefined.

return (
  123
);

Because of this, people in js tend to put parenthesis in a lot more places than php and put { on the same line.

Thanks for the explanation @vjeux! 🙂
So we know now that it's only linked to a js context, so it's the plugin responsibility to choose if it's kept or not.

@mathieutu feel free to send a PR

:+1: Let's remove the braces in the PHP plugin then!

ok thanks!
As I haven't contributed yet to prettier, I don't really know how it works under the hood, and how to make that done.

I'll try to check that when it'll be quieter at work.

Thank you @evilebottnawi! 🎉
After having seen your PR, I can confirm that I wasn't capable to do it right now!

@mathieutu don't worry, prettier is very easy tool, feel free to contribute

Was this page helpful?
0 / 5 - 0 ratings

Related issues

czosel picture czosel  ·  5Comments

nikulis picture nikulis  ·  5Comments

alexander-akait picture alexander-akait  ·  5Comments

em4nl picture em4nl  ·  5Comments

czosel picture czosel  ·  6Comments