Skip to content

Commit

Permalink
bug #6160 NonPrintableCharacterFixer - fix for when removing non-prin…
Browse files Browse the repository at this point in the history
…table character break PHP syntax (kubawerlos)

This PR was merged into the master branch.

Discussion
----------

NonPrintableCharacterFixer - fix for when removing non-printable character break PHP syntax

Apparently it is possible to use [zero-width space](https://github.com/colinodell/json5/blob/v2.2.1/src/Json5Decoder.php#L375) and [non-breaking space](https://github.com/markrogoyski/math-php/blob/v2.5.0/src/Statistics/Distance.php#L241) in such way that removing them (`NonPrintableCharacterFixer` replaces non-breaking space with normal space) breaks PHP syntax:
```php
<?php
/* Hello *<zero-width space here>/ World! */
$foo<non-breaking space here>bar = true;
```

Should we use ultimate solution here and check if removing non-printable character breaks PHP syntax or have fun with tokens and comment's content (if that's even possible, I've found these 2 cases, could be more)?

Commits
-------

940690d NonPrintableCharacterFixer - fix for when removing non-printable character break PHP syntax
  • Loading branch information
SpacePossum committed Dec 13, 2021
2 parents 38df50e + 940690d commit f2808cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Fixer/Basic/NonPrintableCharacterFixer.php
Expand Up @@ -174,7 +174,19 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

if ($token->isGivenKind(self::$tokens)) {
$tokens[$index] = new Token([$token->getId(), strtr($content, $replacements)]);
$newContent = strtr($content, $replacements);

// variable name cannot contain space
if ($token->isGivenKind([T_STRING_VARNAME, T_VARIABLE]) && str_contains($newContent, ' ')) {
continue;
}

// multiline comment must have "*/" only at the end
if ($token->isGivenKind([T_COMMENT, T_DOC_COMMENT]) && str_starts_with($newContent, '/*') && strpos($newContent, '*/') !== \strlen($newContent) - 2) {
continue;
}

$tokens[$index] = new Token([$token->getId(), $newContent]);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixer/Basic/NonPrintableCharacterFixerTest.php
Expand Up @@ -103,6 +103,18 @@ function f(string $p)
'<?php echo \'12345\';?>abc<?php ?>',
'<?php echo \'123'.pack('H*', 'e2808b').'45\';?>a'.pack('H*', 'e2808b').'bc<?php ?>',
],
[
'<?php echo "${foo'.pack('H*', 'c2a0').'bar} is great!";',
],
[
'<?php echo $foo'.pack('H*', 'c2a0').'bar;',
],
[
'<?php /* foo *'.pack('H*', 'e2808b').'/ bar */',
],
[
'<?php /** foo *'.pack('H*', 'e2808b').'/ bar */',
],
];
}

Expand Down

0 comments on commit f2808cf

Please sign in to comment.