Skip to content

Commit 40f1a55

Browse files
committed
✨ New Generic.WhiteSpace.HereNowdocIdentifierSpacing sniff
New `Generic.WhiteSpace.HereNowdocIdentifierSpacing` sniff which forbids whitespace between the `<<<` and the identifier string in heredoc/nowdoc start tokens. Includes fixer. Includes tests. Includes XML docs.
1 parent 4625938 commit 40f1a55

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<documentation title="Heredoc Nowdoc Identifier Spacing">
2+
<standard>
3+
<![CDATA[
4+
There should be no space between the <<< and the heredoc/nowdoc identifier string.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: No space between the <<< and the identifier string.">
9+
<![CDATA[
10+
$heredoc = <em><<<EOD</em>
11+
some text
12+
EOD;
13+
]]>
14+
</code>
15+
<code title="Invalid: Whitespace between the <<< and the identifier string.">
16+
<![CDATA[
17+
$heredoc = <em><<< END</em>
18+
some text
19+
END;
20+
]]>
21+
</code>
22+
</code_comparison>
23+
</documentation>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Ensures heredoc/nowdoc identifiers do not have any whitespace before them.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
class HereNowdocIdentifierSpacingSniff implements Sniff
16+
{
17+
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array<int|string>
23+
*/
24+
public function register()
25+
{
26+
return [
27+
T_START_HEREDOC,
28+
T_START_NOWDOC,
29+
];
30+
31+
}//end register()
32+
33+
34+
/**
35+
* Processes this test, when one of its tokens is encountered.
36+
*
37+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38+
* @param int $stackPtr The position of the current token in
39+
* the stack passed in $tokens.
40+
*
41+
* @return void
42+
*/
43+
public function process(File $phpcsFile, $stackPtr)
44+
{
45+
$tokens = $phpcsFile->getTokens();
46+
47+
if (strpos($tokens[$stackPtr]['content'], ' ') === false
48+
&& strpos($tokens[$stackPtr]['content'], "\t") === false
49+
) {
50+
// Nothing to do.
51+
return;
52+
}
53+
54+
$error = 'There should be no space between the <<< and the heredoc/nowdoc identifier string';
55+
$data = [$tokens[$stackPtr]['content']];
56+
57+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceFound', $data);
58+
if ($fix === true) {
59+
$replacement = str_replace([' ', "\t"], '', $tokens[$stackPtr]['content']);
60+
$phpcsFile->fixer->replaceToken($stackPtr, $replacement);
61+
}
62+
63+
}//end process()
64+
65+
66+
}//end class
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
$heredoc = <<<EOD
4+
some text
5+
EOD;
6+
7+
$nowdoc = <<<'EOD'
8+
some text
9+
EOD;
10+
11+
$heredoc = <<< END
12+
some text
13+
END;
14+
15+
$nowdoc = <<< 'END'
16+
some text
17+
END;
18+
19+
$heredoc = <<< "END"
20+
some text
21+
END;
22+
23+
$nowdoc = <<< 'END'
24+
some text
25+
END;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
$heredoc = <<<EOD
4+
some text
5+
EOD;
6+
7+
$nowdoc = <<<'EOD'
8+
some text
9+
EOD;
10+
11+
$heredoc = <<<END
12+
some text
13+
END;
14+
15+
$nowdoc = <<<'END'
16+
some text
17+
END;
18+
19+
$heredoc = <<<"END"
20+
some text
21+
END;
22+
23+
$nowdoc = <<<'END'
24+
some text
25+
END;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Unit test class for the HereNowdocIdentifierSpacing sniff.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Tests\WhiteSpace;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
/**
15+
* Unit test class for the LanguageConstructSpacing sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\HereNowdocIdentifierSpacingSniff
18+
*/
19+
final class HereNowdocIdentifierSpacingUnitTest extends AbstractSniffUnitTest
20+
{
21+
22+
23+
/**
24+
* Returns the lines where errors should occur.
25+
*
26+
* The key of the array should represent the line number and the value
27+
* should represent the number of errors that should occur on that line.
28+
*
29+
* @return array<int, int>
30+
*/
31+
public function getErrorList()
32+
{
33+
return [
34+
11 => 1,
35+
15 => 1,
36+
19 => 1,
37+
23 => 1,
38+
];
39+
40+
}//end getErrorList()
41+
42+
43+
/**
44+
* Returns the lines where warnings should occur.
45+
*
46+
* The key of the array should represent the line number and the value
47+
* should represent the number of warnings that should occur on that line.
48+
*
49+
* @return array<int, int>
50+
*/
51+
public function getWarningList()
52+
{
53+
return [];
54+
55+
}//end getWarningList()
56+
57+
58+
}//end class

0 commit comments

Comments
 (0)