Skip to content

Commit

Permalink
bug #6170 NonPrintableCharacterFixer - fix for string in single quote…
Browse files Browse the repository at this point in the history
…s, having non-breaking space, linebreak, and single quote inside (kubawerlos)

This PR was squashed before being merged into the master branch (closes #6170).

Discussion
----------

NonPrintableCharacterFixer - fix for string in single quotes, having non-breaking space, linebreak, and single quote inside

In the topic of edge cases - this one is awesome, the title says it all.

Don't give me any credits for this, I'm not that smart to came with this one - all the glory goes to Google.

see it at your own risk, not the prettiest code you'll ever see: https://github.com/googleapis/google-cloud-php/blob/v0.172.0/Compute/metadata/V1/Compute.php.

Commits
-------

c02a447 NonPrintableCharacterFixer - fix for string in single quotes, having non-breaking space, linebreak, and single quote inside
  • Loading branch information
SpacePossum committed Dec 14, 2021
2 parents ea5a185 + c02a447 commit 3363706
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Fixer/Basic/NonPrintableCharacterFixer.php
Expand Up @@ -165,7 +165,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

if ($swapQuotes) {
$content = str_replace('"', '\"', $content);
$content = Preg::replace('/^\'(.*)\'$/', '"$1"', $content);
$content = Preg::replace('/^\'(.*)\'$/s', '"$1"', $content);
}

$tokens[$index] = new Token([$token->getId(), strtr($content, $escapeSequences)]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/Basic/NonPrintableCharacterFixerTest.php
Expand Up @@ -303,6 +303,10 @@ function f(string $p)
INPUT
, pack('H*', 'e2808b')),
],
[
"<?php \"String in single quotes, having non-breaking space: \\u{a0}, linebreak: \n, and single quote inside: ' is a dangerous mix.\";",
"<?php 'String in single quotes, having non-breaking space: ".pack('H*', 'c2a0').", linebreak: \n, and single quote inside: \\' is a dangerous mix.';",
],
];
}
}

0 comments on commit 3363706

Please sign in to comment.