Skip to content

Commit

Permalink
LineEndingFixer - T_CLOSE_TAG support, StringLineEndingFixer - T_INLI…
Browse files Browse the repository at this point in the history
…NE_HTML support
  • Loading branch information
SpacePossum committed Feb 25, 2020
1 parent ed34dde commit 522e819
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Fixer/StringNotation/StringLineEndingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class StringLineEndingFixer extends AbstractFixer implements WhitespacesAw
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isAnyTokenKindsFound([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE]);
return $tokens->isAnyTokenKindsFound([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_INLINE_HTML]);
}

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$ending = $this->whitespacesConfig->getLineEnding();

foreach ($tokens as $tokenIndex => $token) {
if (!$token->isGivenKind([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE])) {
if (!$token->isGivenKind([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_INLINE_HTML])) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Whitespace/LineEndingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

if ($token->isGivenKind([T_OPEN_TAG, T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_START_HEREDOC])) {
if ($token->isGivenKind([T_CLOSE_TAG, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_START_HEREDOC, T_WHITESPACE])) {
$tokens[$index] = new Token([
$token->getId(),
Preg::replace(
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/StringNotation/StringLineEndingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function provideFixCases()
sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), $input),
sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
],
'not T_CLOSE_TAG, do T_INLINE_HTML' => [
"<?php foo(); ?>\r\nA\n\n",
"<?php foo(); ?>\r\nA\r\n\r\n",
],
];
}

Expand Down
36 changes: 19 additions & 17 deletions tests/Fixer/Whitespace/LineEndingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ public function provideFixCases()
"<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \n |\r\nTEST;\r\n", // both cases
];

// !T_INLINE_HTML
$cases[] = [
"<?php ?>\r\n<?php ?>\r\n",
$cases['T_INLINE_HTML'] = [
"<?php ?>\nZ\r\n<?php ?>\nZ\r\n",
];

// !T_CONSTANT_ENCAPSED_STRING
$cases[] = [
$cases['!T_CONSTANT_ENCAPSED_STRING'] = [
"<?php \$a=\"a\r\n\";",
];

Expand All @@ -65,6 +63,16 @@ public function provideFixCases()
"<?php echo 'foo',\r\r\n'bar';",
];

$cases['T_CLOSE_TAG'] = [
"<?php\n?>\n<?php\n",
"<?php\n?>\r\n<?php\n",
];

$cases['T_CLOSE_TAG II'] = [
"<?php\n?>\n<?php\n?>\n<?php\n",
"<?php\n?>\r\n<?php\n?>\r\n<?php\n",
];

return $cases;
}

Expand Down Expand Up @@ -103,37 +111,31 @@ public function provideMessyWhitespacesCases()
private function provideCommonCases()
{
return [
// T_OPEN_TAG
[
'T_OPEN_TAG' => [
"<?php\n \$a = 1;",
"<?php\r\n \$a = 1;",
],
// T_WHITESPACE
[
'T_WHITESPACE' => [
"<?php \n \$a\n= 1;\n",
"<?php \r\n \$a\r\n= 1;\r\n",
],
// T_COMMENT
[
'T_COMMENT' => [
"<?php /*\n*/",
"<?php /*\r\n*/",
],
// T_DOC_COMMENT
[
'T_DOC_COMMENT' => [
"<?php /**\n*/",
"<?php /**\r\n*/",
],
// T_START_HEREDOC
[
'T_START_HEREDOC' => [
"<?php \$a = <<<'TEST'\nAA\nTEST;\n",
"<?php \$a = <<<'TEST'\r\nAA\r\nTEST;\r\n",
],
[
"<?php \$a = <<<TEST\nAAA\nTEST;\n",
"<?php \$a = <<<TEST\r\nAAA\r\nTEST;\r\n",
],
// T_ENCAPSED_AND_WHITESPACE
[
'T_ENCAPSED_AND_WHITESPACE' => [
"<?php \$a = <<<'TEST'\nAAAA 1\n \$b\nTEST;\n",
"<?php \$a = <<<'TEST'\r\nAAAA 1\r\n \$b\r\nTEST;\r\n",
],
Expand Down

0 comments on commit 522e819

Please sign in to comment.