Plugin-php: Prettier adding extra unnecessary parenthesis in method with assignment call

Created on 13 Dec 2018  ·  12Comments  ·  Source: prettier/plugin-php

Prettier 1.15.3

PHP Plugin 0.9.0-prerelease.5

# Options (if any):
{
  "singleQuote": true,
  "tabWidth": 2,
  "bracketSpacing": false,
  "trailingComma": "all"
}

Input:

    $fieldWrap->addChild($span= new Tag\Html('span'));

Output:

    $fieldWrap->addChild(($span= new Tag\Html('span')));

Expected behaviour:
Prettier does not need to double up the braces around the assignment inside the method call.

enhancement

All 12 comments

I don't know about this case in particular, but I know that in, e.g., a while loop, prettier will add an extra set of parens to indicate that assignment is happening instead of an equality check – like while (($var = $other)) {.

I'd imagine that assignment within a method call is doing this for the same reasons.

@hawkrives please provide example

@cseufert prettier do same https://prettier.io/playground/#N4Igxg9gdgLgprEAuEACVASAZgSzgGwBMB1AJwEMAHAOnMMIGEALHIgCgwGdLyoBeVFDgB3VAAkYAW3xsA5N16yAlEoDcIADQgIlGDmidkocqVIRhABRMJDKcgDcIOQppAAjCmADWcGAGUeMBwoAHNkGFIAVzgtJil8YhZ4BTA4PxscPXtMgE9kcE5DLWDOOFIYCwoQyXJkLHJ8Uq0AK04ADwAhTx9-ckk4ABlguDqGppBWtr9gkPw4AEVIiHhRxpiQHlJS0ny3cjcc-GhXSlJgmGJnGCZkAA4ABi1TiFKyKnzTuG37Ea1SOAAjpEcP9KuRqrUkPU1lpSpIcOEoutODM5otliMoWN1jB9pdCNdkAAmLQRcisGYMCCSGr5KDQX4gSKlAAq+1s0NKAF8uUA, but i think we should change logic for PHP

@evilebottnawi just something like this https://prettier.io/playground/#N4Igxg9gdgLgprEAuEACVB3AFgSwDZyoAURAHgLyoCeAlDasADpTrqmqUBOAhlACY5YRGs1bUOqHv0Exho1AF8QAGhAQADjBzQAzslDdOnCBgAKhhHpTcAbhBx8VIAEY8wAazgwAyuu5hBAHNkGE4AVzhVLBgAWzwAdVx4HT8wOG9LHC0bLKpkcB09VUEdOE4YUx5AmO5kADNuPFLVACsdUgAhN08fbhi4ABlBOHrG5pA20m8gggBFMIh4UabIkD9OUs58525nKjxoJ3VOGXiHGCxkAA4ABlVjiFL4nnV847hNmxHVTjgARzCOF+lW41VqSAaK1UpRiOBC4VWOhmcHmixGELGqxguzOfAuyAATKpQtx8EEAMIQGI1fJQaDfEBhUoAFV2VkhpQUCiAA

I personally like the extra parentheses around assignment in odd locations, in both JS and PHP, but maybe I'm in the minority opinion here.

@hawkrives we remove don't add parens in this case because php and js have difference how built-in function works, many php function return null when something wrong or something can be executed so in php it is normal practice use if ($content = file_get_content('file.text')).

Maybe for while we also don't add parens

@evilebottnawi ah, ok. Thanks for the explanation.

/cc @czosel

  1. what do you think about extra parens in control structure

Now:

if (($file = file_get_content())) {
}

I think we should avoid this, exclude while

  1. what do you think about extra parens in arguments for function/new/etc

Now:

call(($file = new Foo()), $foo, $bar);

I think we should avoid parens here too in PHP

Generally I like the extra parens if we spot an assignment in an unusual place. if ($file = file_get_content()) { indeed seems common in PHP, so we might not want to add parens there. Assignment in function arguments or in while looks odd to me, so I'd leave the extra parens there. Or can you point to some typical use cases for that @evilebottnawi?

@czosel After investigate in laravel/symfony/drupal code based it is frequent practice to do call($var = new Foo()).

Alright, I'm good with dropping the parens then.

Just note for while, popular code:

while ($info = mysql_fetch_array($data_jurisdiction)) {}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackel picture hackel  ·  3Comments

bertuz picture bertuz  ·  5Comments

czosel picture czosel  ·  6Comments

ichiriac picture ichiriac  ·  3Comments

czosel picture czosel  ·  5Comments