Skip to content

Commit 08e3d96

Browse files
committed
Generic/LanguageConstructSpacing: update for changed "yield from" tokenization
This commit updates the `Generic.WhiteSpace.LanguageConstructSpacing` sniff to recognize that a multi-token `yield from` may now have whitespace and comment tokens between the keywords.
1 parent b768302 commit 08e3d96

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,30 @@ public function process(File $phpcsFile, $stackPtr)
8080
if ($tokens[$stackPtr]['code'] === T_YIELD_FROM
8181
&& strtolower($content) !== 'yield from'
8282
) {
83-
if ($tokens[($stackPtr - 1)]['code'] === T_YIELD_FROM) {
84-
// A multi-line statement that has already been processed.
85-
return;
86-
}
83+
$found = $content;
84+
$hasComment = false;
85+
$yieldFromEnd = $stackPtr;
86+
87+
// Handle potentially multi-line/multi-token "yield from" expressions.
88+
if (preg_match('`yield\s+from`i', $content) !== 1) {
89+
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
90+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false
91+
&& $tokens[$i]['code'] !== T_YIELD_FROM
92+
) {
93+
break;
94+
}
8795

88-
$found = $content;
89-
if ($tokens[($stackPtr + 1)]['code'] === T_YIELD_FROM) {
90-
// This yield from statement is split over multiple lines.
91-
$i = ($stackPtr + 1);
92-
do {
9396
$found .= $tokens[$i]['content'];
94-
$i++;
95-
} while ($tokens[$i]['code'] === T_YIELD_FROM);
96-
}
97+
98+
if ($tokens[$i]['code'] === T_YIELD_FROM
99+
&& strtolower(trim($tokens[$i]['content'])) === 'from'
100+
) {
101+
break;
102+
}
103+
}
104+
105+
$yieldFromEnd = $i;
106+
}//end if
97107

98108
$error = 'Language constructs must be followed by a single space; expected 1 space between YIELD FROM found "%s"';
99109
$data = [Common::prepareForOutput($found)];
@@ -104,18 +114,14 @@ public function process(File $phpcsFile, $stackPtr)
104114
$phpcsFile->fixer->beginChangeset();
105115
$phpcsFile->fixer->replaceToken($stackPtr, $yield[0].' '.$from[0]);
106116

107-
if ($tokens[($stackPtr + 1)]['code'] === T_YIELD_FROM) {
108-
$i = ($stackPtr + 1);
109-
do {
110-
$phpcsFile->fixer->replaceToken($i, '');
111-
$i++;
112-
} while ($tokens[$i]['code'] === T_YIELD_FROM);
117+
for ($i = ($stackPtr + 1); $i <= $yieldFromEnd; $i++) {
118+
$phpcsFile->fixer->replaceToken($i, '');
113119
}
114120

115121
$phpcsFile->fixer->endChangeset();
116122
}
117123

118-
return;
124+
return ($yieldFromEnd + 1);
119125
}//end if
120126

121127
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {

0 commit comments

Comments
 (0)