Skip to content

Commit 4a0ec1a

Browse files
authored
Merge pull request #1321 from PHPCSStandards/feature/1320-generic-unnecessaryheredoc-fixer-vs-tabs
Generic/UnnecessaryHeredoc: bug fix - fixer changes indentation type
2 parents af2d9ec + 4dc8937 commit 4a0ec1a

12 files changed

+815
-16
lines changed

src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ public function process(File $phpcsFile, int $stackPtr)
128128
$phpcsFile->fixer->replaceToken($stackPtr, $replacement);
129129

130130
for ($i = ($stackPtr + 1); $i < $closer; $i++) {
131-
$content = $tokens[$i]['content'];
132-
$content = str_replace(['\\$', '\\\\'], ['$', '\\'], $content);
133-
if ($tokens[$i]['content'] !== $content) {
131+
$origContent = $tokens[$i]['content'];
132+
if (isset($tokens[$i]['orig_content']) === true) {
133+
$origContent = $tokens[$i]['orig_content'];
134+
}
135+
136+
$content = str_replace(['\\$', '\\\\'], ['$', '\\'], $origContent);
137+
if ($origContent !== $content) {
134138
$phpcsFile->fixer->replaceToken($i, $content);
135139
}
136140
}

src/Standards/Generic/Tests/Strings/UnnecessaryHeredocUnitTest.1.inc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

33
/*
4-
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
5-
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
610
*
7-
* These two files should be kept in sync!
11+
* These files should be kept in sync!
812
*/
913

1014
$nowdoc = <<<'EOD'

src/Standards/Generic/Tests/Strings/UnnecessaryHeredocUnitTest.1.inc.fixed

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

33
/*
4-
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
5-
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
610
*
7-
* These two files should be kept in sync!
11+
* These files should be kept in sync!
812
*/
913

1014
$nowdoc = <<<'EOD'

src/Standards/Generic/Tests/Strings/UnnecessaryHeredocUnitTest.2.inc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

33
/*
4-
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
5-
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
610
*
7-
* These two files should be kept in sync!
11+
* These files should be kept in sync!
812
*/
913

1014
$nowdoc = <<<'EOD'

src/Standards/Generic/Tests/Strings/UnnecessaryHeredocUnitTest.2.inc.fixed

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

33
/*
4-
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
5-
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
610
*
7-
* These two files should be kept in sync!
11+
* These files should be kept in sync!
812
*/
913

1014
$nowdoc = <<<'EOD'
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/*
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
10+
*
11+
* These files should be kept in sync!
12+
*/
13+
14+
$nowdoc = <<<'EOD'
15+
some text
16+
EOD;
17+
18+
$heredoc = <<<END
19+
some $foo text
20+
END;
21+
22+
$heredoc = <<<"END"
23+
some {$foo[0]} text
24+
END;
25+
26+
$heredoc = <<<END
27+
{$foo?->bar}
28+
END;
29+
30+
$heredoc = <<< "END"
31+
some ${beers::softdrink}
32+
END;
33+
34+
$heredoc = <<< END
35+
{${$object->getName()}} text
36+
END;
37+
38+
$heredoc = <<<"END"
39+
some {${getName()}}
40+
END;
41+
42+
$heredoc = <<<END
43+
${substr('laruence', 0, 2)}
44+
END;
45+
46+
$heredoc = <<<"END"
47+
some {$foo['bar']->baz()()}
48+
END;
49+
50+
$heredoc = <<<END
51+
{$obj->values[3]->name} text
52+
END;
53+
54+
$heredoc = <<<"END"
55+
some ${$bar}
56+
END;
57+
58+
$heredoc = <<<END
59+
${foo->bar} text
60+
END;
61+
62+
$heredoc = <<<"END"
63+
${foo["${bar}"]} text
64+
END;
65+
66+
$heredoc = <<<END
67+
some ${foo["${bar[\'baz\']}"]}
68+
END;
69+
70+
$heredoc = <<<"END"
71+
${foo->{${'a'}}} text
72+
END;
73+
74+
$heredoc = <<<END
75+
some {$foo->{$baz[1]}}
76+
END;
77+
78+
$heredoc = <<<END
79+
some text
80+
{${beers::$ale}}
81+
some text
82+
END;
83+
84+
$heredoc = <<<"END"
85+
$people->john's wife greeted $people->robert.
86+
END;
87+
88+
$heredoc = <<<END
89+
Let's make sure it also works with this: {$arr[foo][3]}
90+
END;
91+
92+
$heredoc = <<<END
93+
Testing ${foo["${bar
94+
['baz']
95+
}"]} and more testing
96+
END;
97+
98+
$heredoc = <<<"END"
99+
Testing {${foo["${bar
100+
['baz']
101+
}"]}} and more testing
102+
END;
103+
104+
$heredoc = <<<END
105+
some text
106+
END;
107+
108+
$heredoc = <<< "END"
109+
some text
110+
some \$text
111+
some text \\ including a backslash
112+
END;
113+
114+
$heredoc = <<<EOD
115+
<?php
116+
echo 'The below line contains escape characters and should be recognized as needing heredoc';
117+
echo "aa\xC3\xC3 \xC3\xB8aa";
118+
EOD;
119+
120+
echo <<<EOT
121+
This should print a capital 'A': \x41
122+
EOT;
123+
124+
echo <<<EOT
125+
Here we should have a tab and 2 'A's: \t \101 \u{41}
126+
EOT;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/*
4+
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
5+
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
6+
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
7+
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
8+
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
9+
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
10+
*
11+
* These files should be kept in sync!
12+
*/
13+
14+
$nowdoc = <<<'EOD'
15+
some text
16+
EOD;
17+
18+
$heredoc = <<<END
19+
some $foo text
20+
END;
21+
22+
$heredoc = <<<"END"
23+
some {$foo[0]} text
24+
END;
25+
26+
$heredoc = <<<END
27+
{$foo?->bar}
28+
END;
29+
30+
$heredoc = <<< "END"
31+
some ${beers::softdrink}
32+
END;
33+
34+
$heredoc = <<< END
35+
{${$object->getName()}} text
36+
END;
37+
38+
$heredoc = <<<"END"
39+
some {${getName()}}
40+
END;
41+
42+
$heredoc = <<<END
43+
${substr('laruence', 0, 2)}
44+
END;
45+
46+
$heredoc = <<<"END"
47+
some {$foo['bar']->baz()()}
48+
END;
49+
50+
$heredoc = <<<END
51+
{$obj->values[3]->name} text
52+
END;
53+
54+
$heredoc = <<<"END"
55+
some ${$bar}
56+
END;
57+
58+
$heredoc = <<<END
59+
${foo->bar} text
60+
END;
61+
62+
$heredoc = <<<"END"
63+
${foo["${bar}"]} text
64+
END;
65+
66+
$heredoc = <<<END
67+
some ${foo["${bar[\'baz\']}"]}
68+
END;
69+
70+
$heredoc = <<<"END"
71+
${foo->{${'a'}}} text
72+
END;
73+
74+
$heredoc = <<<END
75+
some {$foo->{$baz[1]}}
76+
END;
77+
78+
$heredoc = <<<END
79+
some text
80+
{${beers::$ale}}
81+
some text
82+
END;
83+
84+
$heredoc = <<<"END"
85+
$people->john's wife greeted $people->robert.
86+
END;
87+
88+
$heredoc = <<<END
89+
Let's make sure it also works with this: {$arr[foo][3]}
90+
END;
91+
92+
$heredoc = <<<END
93+
Testing ${foo["${bar
94+
['baz']
95+
}"]} and more testing
96+
END;
97+
98+
$heredoc = <<<"END"
99+
Testing {${foo["${bar
100+
['baz']
101+
}"]}} and more testing
102+
END;
103+
104+
$heredoc = <<<'END'
105+
some text
106+
END;
107+
108+
$heredoc = <<< 'END'
109+
some text
110+
some $text
111+
some text \ including a backslash
112+
END;
113+
114+
$heredoc = <<<EOD
115+
<?php
116+
echo 'The below line contains escape characters and should be recognized as needing heredoc';
117+
echo "aa\xC3\xC3 \xC3\xB8aa";
118+
EOD;
119+
120+
echo <<<EOT
121+
This should print a capital 'A': \x41
122+
EOT;
123+
124+
echo <<<EOT
125+
Here we should have a tab and 2 'A's: \t \101 \u{41}
126+
EOT;

0 commit comments

Comments
 (0)