Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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