Skip to content

Commit 134134e

Browse files
committed
Squiz/BlockComment: don't require a blank line between PHP open tag and block comment
Just like with scope openers, if a block comment is the first thing after a PHP open tag, no blank line should be required. Includes unit tests.
1 parent 390bffe commit 134134e

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ public function process(File $phpcsFile, $stackPtr)
360360

361361
// Check that the lines before and after this comment are blank.
362362
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
363-
if (isset($tokens[$contentBefore]['scope_closer']) === true
364-
&& $tokens[$contentBefore]['scope_opener'] === $contentBefore
363+
if ((isset($tokens[$contentBefore]['scope_closer']) === true
364+
&& $tokens[$contentBefore]['scope_opener'] === $contentBefore)
365+
|| $tokens[$contentBefore]['code'] === T_OPEN_TAG
365366
) {
366367
if (($tokens[$stackPtr]['line'] - $tokens[$contentBefore]['line']) !== 1) {
367368
$error = 'Empty line not required before block comment';

src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,17 @@ $y = 10 + /* test */ -2;
242242
* See: {@link https://github.com/squizlabs/PHP_CodeSniffer/issues/1918}
243243
* @phpcs:disable Standard.Category.Sniff
244244
*/
245+
246+
?>
247+
<?php
248+
/*
249+
* No blank line required above the comment if it's the first non-empty token after a PHP open tag.
250+
*/
251+
252+
?>
253+
<?php
254+
255+
256+
/*
257+
* No blank line allowed above the comment if it's the first non-empty token after a PHP open tag.
258+
*/

src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,17 @@ $y = 10 + /* test */ -2;
244244
* See: {@link https://github.com/squizlabs/PHP_CodeSniffer/issues/1918}
245245
* @phpcs:disable Standard.Category.Sniff
246246
*/
247+
248+
?>
249+
<?php
250+
/*
251+
* No blank line required above the comment if it's the first non-empty token after a PHP open tag.
252+
*/
253+
254+
?>
255+
<?php
256+
257+
258+
/*
259+
* No blank line allowed above the comment if it's the first non-empty token after a PHP open tag.
260+
*/

src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function setCliValues($testFile, $config)
4141
public function getErrorList()
4242
{
4343
$errors = [
44+
3 => 1,
4445
8 => 1,
4546
20 => 1,
4647
24 => 1,
@@ -75,6 +76,7 @@ public function getErrorList()
7576
227 => 1,
7677
232 => 1,
7778
233 => 1,
79+
256 => 1,
7880
];
7981

8082
return $errors;

0 commit comments

Comments
 (0)