Skip to content

Commit

Permalink
feature PHP-CS-Fixer#1267 SingleQuoteFixer fix more use cases (SpaceP…
Browse files Browse the repository at this point in the history
…ossum)

This PR was squashed before being merged into the 1.10 branch (closes PHP-CS-Fixer#1267).

Discussion
----------

SingleQuoteFixer fix more use cases

This helps fixing more use cases for the single quote fixer.

For example:
```php
<?php $a = "foo \$bar5";
```
```php
<?php $a = 'foo $bar5';
```

Commits
-------

3cc7907 SingleQuoteFixer fix more use cases
  • Loading branch information
keradus committed Jun 17, 2015
2 parents 08170a5 + 3cc7907 commit 0b0ec89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Symfony/CS/Fixer/Symfony/SingleQuoteFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public function fix(\SplFileInfo $file, $content)
if (
'"' === $content[0] &&
false === strpos($content, "'") &&
// regex: odd number of backslashes, not followed by double quote
!preg_match('/(?<!\\\\)(?:\\\\{2})*\\\\(?!["\\\\])/', $content, $m)
// regex: odd number of backslashes, not followed by double quote or dollar
!preg_match('/(?<!\\\\)(?:\\\\{2})*\\\\(?!["$\\\\])/', $content, $m)
) {
$content = substr($content, 1, -1);
$content = str_replace('\\"', '"', $content);
$token->setContent("'".$content."'");
$content = str_replace('\\$', '$', $content);
$token->setContent('\''.$content.'\'');
}
}

Expand Down
14 changes: 13 additions & 1 deletion Symfony/CS/Tests/Fixer/Symfony/SingleQuoteFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ public function provideTestFixCases()
<?php $a = "\\foo\\bar\\\\";
EOF
),

array(
'<?php $a = \'foo $bar7\';',
'<?php $a = "foo \$bar7";',
),
array(
'<?php $a = \'foo $(bar7)\';',
'<?php $a = "foo \$(bar7)";',
),
array(
'<?php $a = \'foo \\\\($bar8)\';',
'<?php $a = "foo \\\\(\$bar8)";',
),
array('<?php $a = "foo \\" \\$$bar";'),
array('<?php $a = \'foo bar\';'),
array('<?php $a = \'foo "bar"\';'),
array('<?php $a = "foo \'bar\'";'),
Expand Down

0 comments on commit 0b0ec89

Please sign in to comment.