Plugin-php: Single quotes not changed to double quotes

Created on 21 Dec 2019  路  14Comments  路  Source: prettier/plugin-php

@prettier/plugin-php v0.11.2
Playground link

Input:

<?php

$a = 'a';
$b = "b";

Output:

<?php

$a = 'a';
$b = "b";

Expected:

<?php

$a = "a";
$b = "b";

JavaScript example

Related: #633.

discussion

Most helpful comment

Fixed and released as v0.16.0 :tada:

All 14 comments

This works when you activate the single-quote option, see this example in the playground.

@czosel I expect double quotes when the single-quote option is set to false (default), which is how it works for JS/TS.

@glen-84 Sorry, I misread your issue. I'm not sure if that's something that we can / should do, because double quotes in PHP are more like template literals in JS.

cc @evilebottnawi @loilo

Ah, I see the issue now:

If:

echo 'Hello $world';

... is automatically changed to:

echo "Hello $world";

... then the behaviour of the application changes.

You could do the inverse of the single-quote handling though, i.e. if the string is "simple", then it can be changed.

Exactly! Yep, that might work. For reference: Here is the relevant function.

You could do the inverse of the single-quote handling though, i.e. if the string is "simple", then it can be changed.

This would entail looking for symbols inside the string like $, \t, \n and determine whether it can safely be changed or should be left alone. I think that's a great feature.

Would this mean that all simple strings would become single quoted and only complex ones would become doubly ones sort of like when using single quotes and back ticks in JS?

Yeah, we could consider making it "symmetric" like that. What do you think @loilo, @evilebottnawi ?

Do you think that there is any measurable difference performance-wise between 'simple' and "simple"?

As far as I know (not having the source at hand though), there are no relevant performance implications.

I think converting to double quotes would be reasonable for the sake of consistency. It was one of the first things I wondered about with this plugin, but I thought you just couldn't bother to detect whether a string can safely be converted to double quotes. 馃榿

Right - since we're doing this detection already for double -> single, we could probably use it similarly for the other direction as well.

Any updates?

Hi @utkarshgupta137

No one has worked on this yet, but I think the change should be relatively straight-forward. If you'd be interested in contributing I'm happy to help you get started; otherwise I can try to take a look myself, but can't give any guarantees about when I find the time.

Sure, let's do this.

Cool! https://github.com/prettier/plugin-php/blob/master/CONTRIBUTING.md should explain how to get started - let me know how I can help in case you get stuck!

Fixed and released as v0.16.0 :tada:

Was this page helpful?
0 / 5 - 0 ratings