Plugin-php: Convert double quotes to single

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

Input:

$var = "just string without expressions";

Output:

$var = "just string without expressions";

Expected behavior:

$var = 'just string without expressions';

Only for string without variables, call and etc.

Feel free to feedback. Add :+1: if you want this.

enhancement

Most helpful comment

I think that using —single-quote makes sense. It wouldn’t turn “$a” into ‘$a’ though.

All 11 comments

Consistent string quoting is something I really love in Prettier for JS, and currently miss in the PHP plugin. If we can make sure that were not breaking anything (should not be too hard I guess) then I'm all in for this! :+1:

Hey, looking forward to using prettier with PHP, and just ran across this issue, and noticed it was marked discussion.

This is a change that could be inconvenient in some cases. Say you run prettier on save, and are working on a function that will depend on variable substitution. the first pass might include something like,

return "it works!";

Before you refine it to

return "Thanks, {$user}";

or whatever. The first change to single quotes will make it inconvenient--this makes it essentially impossible to save an intermediate version of the code with double quotes, and you'll have to manually go back and change the quotes on the second save.

While the automatic conversion to single quotes makes sense in javascript, since single and double quotes are basically the same thing, this proposed change would more like automatically converting js template literals:

const foo = `bar`;

to single quotes when they contain no placeholders.

I'd say that since PHP makes a functional distinction between single and double quotes, we should trust the programmer to use the quotes they prefer.

@karptonite Right, that’s why it’s just an idea with implementation and therefore we don’t merge this right now.

Sure! It looked from the discussion on the PR that you were moving toward merging, and I wanted to chime in, since it seemed like a move in the wrong direction to me--as you said, it changes AST.

Using prettier's config --single-quote for this might be an acceptable compromise?

@evilebottnawi I'd be ready to update the PR and hide the feature behind --single-quote if you agree.

@czosel hm, not sure, this may not be very convenient for people using the single quotes (i can want use single quotes for js or css and don't want change quotes in php). Judging by the number of :+1: people want this feature. Let's add new option for this (--prefer-single-quote), maybe better name, feel free to feedback.

@evilebottnawi How about

{
  "single-quote": true,
  "overrides": [
    {
      "files": "*.php",
      "options": {
        "single-quote": false
      }
    }
  ]
}

I'd prefer to keep the option name from Prettier, just because it's intuitive.

@czosel Many people avoid using config files for more zero configuration, let's ask @vjeux what do he think about this.

For @vjeux - in php single and double quotes is difference, but in many cases it is safe to change single to double and people want this feature (but some not). How best to do it? Introduce new option? Use --single-quote option?

I think that using —single-quote makes sense. It wouldn’t turn “$a” into ‘$a’ though.

This seems like a great solution to me. By default, leave quotes as they are, and convert to single quotes when possible when --single-quote is true.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

czosel picture czosel  ·  5Comments

nikulis picture nikulis  ·  5Comments

virgofx picture virgofx  ·  4Comments

alexander-akait picture alexander-akait  ·  3Comments

sgruetter picture sgruetter  ·  5Comments