Skip to content

Commit c786f09

Browse files
grongorkukulich
authored andcommitted
Fix yield inside function calls
1 parent 48e0054 commit c786f09

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/JumpStatementsSpacingSniff.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace SlevomatCodingStandard\Sniffs\ControlStructures;
44

55
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Util\Tokens;
67
use SlevomatCodingStandard\Helpers\TokenHelper;
78
use function abs;
89
use function in_array;
910
use const T_BREAK;
1011
use const T_CONTINUE;
11-
use const T_EQUAL;
1212
use const T_GOTO;
13+
use const T_OPEN_PARENTHESIS;
1314
use const T_RETURN;
1415
use const T_THROW;
1516
use const T_YIELD;
@@ -28,11 +29,7 @@ class JumpStatementsSpacingSniff extends AbstractControlStructureSpacingSniff
2829
*/
2930
public function process(File $phpcsFile, $controlStructurePointer): void
3031
{
31-
if ($this->isYieldWithAssigment($phpcsFile, $controlStructurePointer)) {
32-
return;
33-
}
34-
35-
if ($this->isYieldFromWithReturn($phpcsFile, $controlStructurePointer)) {
32+
if ($this->isOneOfYieldSpecialCases($phpcsFile, $controlStructurePointer)) {
3633
return;
3734
}
3835

@@ -75,30 +72,29 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
7572
parent::checkLinesAfter($phpcsFile, $controlStructurePointer);
7673
}
7774

78-
private function isYieldWithAssigment(File $phpcsFile, int $controlStructurePointer): bool
75+
private function isOneOfYieldSpecialCases(File $phpcsFile, int $controlStructurePointer): bool
7976
{
8077
$tokens = $phpcsFile->getTokens();
8178

82-
if ($tokens[$controlStructurePointer]['code'] !== T_YIELD) {
79+
$controlStructure = $tokens[$controlStructurePointer];
80+
if ($controlStructure['code'] !== T_YIELD && $controlStructure['code'] !== T_YIELD_FROM) {
8381
return false;
8482
}
8583

8684
$pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $controlStructurePointer - 1);
8785

88-
return $tokens[$pointerBefore]['code'] === T_EQUAL;
89-
}
90-
91-
private function isYieldFromWithReturn(File $phpcsFile, int $controlStructurePointer): bool
92-
{
93-
$tokens = $phpcsFile->getTokens();
94-
95-
if ($tokens[$controlStructurePointer]['code'] !== T_YIELD_FROM) {
96-
return false;
86+
// check if yield is used in assignment
87+
if (in_array($tokens[$pointerBefore]['code'], Tokens::$assignmentTokens, true)) {
88+
return true;
9789
}
9890

99-
$pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $controlStructurePointer - 1);
91+
// check if yield is used in a return statement
92+
if ($tokens[$pointerBefore]['code'] === T_RETURN) {
93+
return true;
94+
}
10095

101-
return $tokens[$pointerBefore]['code'] === T_RETURN;
96+
// check if yield is used inside parentheses (function call, while, ...)
97+
return $tokens[$pointerBefore]['code'] === T_OPEN_PARENTHESIS;
10298
}
10399

104100
private function isStackedSingleLineYield(File $phpcsFile, int $controlStructureStartPointer, bool $previous): bool

tests/Sniffs/ControlStructures/data/jumpStatementsSpacingWithDefaultSettingsNoErrors.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,16 @@ function () {
142142
)
143143
->andDone();
144144
};
145+
146+
function () {
147+
$x += yield 1;
148+
149+
run(
150+
yield 1
151+
);
152+
153+
run(yield 1);
154+
155+
while (yield 1) {
156+
}
157+
};

0 commit comments

Comments
 (0)