Skip to content

Commit 007267c

Browse files
committed
Generic/LanguageConstructSpacing: don't auto-fix 'yield from' with comment
If there is a comment between the `yield` and the `from` keywords in a `yield from` expression, the sniff should report this, but should not auto-fix it, as it cannot be determined where the comment should be moved to. Thic commit updates the sniff to throw a non-autofixable error with error code `IncorrectYieldFromWithComment` for that situation. Includes unit tests.
1 parent 08e3d96 commit 007267c

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public function process(File $phpcsFile, $stackPtr)
9393
break;
9494
}
9595

96+
if (isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true) {
97+
$hasComment = true;
98+
}
99+
96100
$found .= $tokens[$i]['content'];
97101

98102
if ($tokens[$i]['code'] === T_YIELD_FROM
@@ -107,19 +111,24 @@ public function process(File $phpcsFile, $stackPtr)
107111

108112
$error = 'Language constructs must be followed by a single space; expected 1 space between YIELD FROM found "%s"';
109113
$data = [Common::prepareForOutput($found)];
110-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectYieldFrom', $data);
111-
if ($fix === true) {
112-
preg_match('/yield/i', $found, $yield);
113-
preg_match('/from/i', $found, $from);
114-
$phpcsFile->fixer->beginChangeset();
115-
$phpcsFile->fixer->replaceToken($stackPtr, $yield[0].' '.$from[0]);
116114

117-
for ($i = ($stackPtr + 1); $i <= $yieldFromEnd; $i++) {
118-
$phpcsFile->fixer->replaceToken($i, '');
119-
}
115+
if ($hasComment === true) {
116+
$phpcsFile->addError($error, $stackPtr, 'IncorrectYieldFromWithComment', $data);
117+
} else {
118+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectYieldFrom', $data);
119+
if ($fix === true) {
120+
preg_match('/yield/i', $found, $yield);
121+
preg_match('/from/i', $found, $from);
122+
$phpcsFile->fixer->beginChangeset();
123+
$phpcsFile->fixer->replaceToken($stackPtr, $yield[0].' '.$from[0]);
120124

121-
$phpcsFile->fixer->endChangeset();
122-
}
125+
for ($i = ($stackPtr + 1); $i <= $yieldFromEnd; $i++) {
126+
$phpcsFile->fixer->replaceToken($i, '');
127+
}
128+
129+
$phpcsFile->fixer->endChangeset();
130+
}
131+
}//end if
123132

124133
return ($yieldFromEnd + 1);
125134
}//end if

src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ $newLine;
8989
// The following line must have a single space at the end (after return)
9090
return
9191
$spaceAndNewLine;
92+
93+
// Related to issue #529. These should not be auto-fixed as we don't know what to do with the comment.
94+
yield /*comment*/ from $test();
95+
yield
96+
# comment
97+
from $test();
98+
yield
99+
// phpcs:ignore Stnd.Category.SniffName
100+
from $test();

src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,12 @@ return $newLine;
8383

8484
// The following line must have a single space at the end (after return)
8585
return $spaceAndNewLine;
86+
87+
// Related to issue #529. These should not be auto-fixed as we don't know what to do with the comment.
88+
yield /*comment*/ from $test();
89+
yield
90+
# comment
91+
from $test();
92+
yield
93+
// phpcs:ignore Stnd.Category.SniffName
94+
from $test();

src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function getErrorList($testFile='')
7171
85 => 1,
7272
86 => 1,
7373
90 => 1,
74+
94 => 1,
75+
95 => 1,
76+
98 => 1,
7477
];
7578

7679
default:

0 commit comments

Comments
 (0)