Php-cs-fixer: sprintf to interpolation fixer

Created on 16 Apr 2018  路  2Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

As of PHP 7 string interpolation is more performant than sprintf calls: https://blog.blackfire.io/php-7-performance-improvements-encapsed-strings-optimization.html (thanks @keradus)

-echo sprintf('foo %s baz', $bar);
+echo "foo $bar baz";
-echo sprintf('foo %s baz', $this->bar());
+echo "foo {$this->bar()} baz";

Obviously it gets a bit more complicated when you have expressions in the sprintf, so it shouldn't be changed here:

echo sprintf('foo %s baz', 1 + 1);
kinfeature request

Most helpful comment

Maybe variable detection (to know whether string interpolation can be applied) could be extracted from YodaStyleFixer.

All 2 comments

Maybe variable detection (to know whether string interpolation can be applied) could be extracted from YodaStyleFixer.

We also may convert this:

echo 'Hello '.$name.'!';

By this:

```php
echo "Hello {$name}!";

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bilge picture Bilge  路  3Comments

BackEndTea picture BackEndTea  路  3Comments

ro0NL picture ro0NL  路  3Comments

fisharebest picture fisharebest  路  3Comments

grachevko picture grachevko  路  3Comments