Php-cs-fixer: explicit_string_variable: "$var" to "${var}" or "{$var}"

Created on 31 Jan 2018  路  9Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

The PHP version you are using ($ php -v):

=> PHP 7.0.22

PHP CS Fixer version you are using ($ php-cs-fixer -V):

=> PHP CS Fixer 2.9.0

The command you use to run PHP CS Fixer:

=> vendor/bin/php-cs-fixer fix --config=.php_cs.dist

The configuration file you are using, if any:

[
   'explicit_string_variable' => true
]
  • before running PHP CS Fixer (no changes):
$query = "select id from $tableName where deleted_at is null";
  • with unexpected changes applied when running PHP CS Fixer:
$query = "select id from ${tableName} where deleted_at is null";
  • with the changes you expected instead:
$query = "select id from {$tableName} where deleted_at is null";

I don't find information on php documentation about this, but I found a lot of examples on this way: {$var}

Also, I found the following problem:

  • before running PHP CS Fixer (no changes):
echo "El usuario $user->first_name no existe."
  • with unexpected changes applied when running PHP CS Fixer:
echo "El usuario ${user}->first_name no existe."
  • with the changes you expected instead:
echo "El usuario {$user->first_name} no existe."

The last example, I think is very complicated, but if we can do somenthing, great! If not, a simple warning would be enough.

Most helpful comment

@alexisverardo @pablorsk yes it is the expected behaviour, try to run:

$ php-cs-fixer describe explicit_string_variable

To have the related documentation.

For simple variable, ${var} is preferred over {$var} because the former it is considered the simple syntax, while the latter the complex one.
See the official documentation: https://secure.php.net/manual/en/language.types.string.php#language.types.string.parsing
Simple rules are preferred over complex ones.

All 9 comments

Thanks @pablorsk for reporting,

@Slamdunk , as author of the fixer, may I ask for your thoughts on this?

Fixed in #3334 released in 2.9.1

@pablorsk please try again after updating to 2.9.1
thanks @Slamdunk :+1:

@SpacePossum
I'm sorry,I try with v2.10.0

before running PHP CS Fixer (no changes):

$query = "select id from $tableName";

with unexpected changes applied when running PHP CS Fixer:

$query = "select id from ${tableName}";

with the changes you expected instead:

$query = "select id from {$tableName}";

in the case of

$user->first_name
works perfect!
{$user->first_name}

thanks for reporting back @alexisverardo
closing this issue as reported that it has been fixed :)

Sorry @SpacePossum but,

From

$query = "select id from $tableName";

to

$query = "select id from ${tableName}";

It's correct? Is not correct this?

$query = "select id from {$tableName}";

Maybe we are wrong. Do you have official documentation? I don't found anything for this case on php.net

all 3 forms have same value. all of them are correct.
fixer simply change initial form to other, that we think is better

@alexisverardo @pablorsk yes it is the expected behaviour, try to run:

$ php-cs-fixer describe explicit_string_variable

To have the related documentation.

For simple variable, ${var} is preferred over {$var} because the former it is considered the simple syntax, while the latter the complex one.
See the official documentation: https://secure.php.net/manual/en/language.types.string.php#language.types.string.parsing
Simple rules are preferred over complex ones.

Closing as there is nothing left to do here I think? Please comment if you think there is and we can reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BackEndTea picture BackEndTea  路  3Comments

fisharebest picture fisharebest  路  3Comments

OskarStark picture OskarStark  路  3Comments

EvgenyOrekhov picture EvgenyOrekhov  路  3Comments

sennewood picture sennewood  路  3Comments