Skip to content

Commit

Permalink
NonPrintableCharacterFixer - fix for when removing non-printable char…
Browse files Browse the repository at this point in the history
…acter break PHP syntax
  • Loading branch information
kubawerlos committed Dec 11, 2021
1 parent 56d8399 commit 940690d
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 940690d

Please sign in to comment.