Skip to content

Commit

Permalink
Merge 681cb47 into abf2d0f
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Aug 30, 2020
2 parents abf2d0f + 681cb47 commit ee64600
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Rules/Generators/YieldTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\Yield_>
Expand Down Expand Up @@ -78,6 +79,9 @@ public function processNode(Node $node, Scope $scope): array
$valueType->describe($verbosityLevel)
))->build();
}
if ($scope->getType($node) instanceof VoidType && !$scope->isInFirstLevelStatement()) {
$messages[] = RuleErrorBuilder::message('Result of yield (void) is used.')->build();
}

return $messages;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Generators/YieldTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public function testRule(): void
'Generator expects value type array(0 => DateTime, 1 => DateTime, 2 => stdClass, 4 => DateTimeImmutable), array(DateTime, DateTime, stdClass, DateTimeImmutable) given.',
25,
],
[
'Result of yield (void) is used.',
137,
],
[
'Result of yield (void) is used.',
138,
],
]);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Generators/data/yield.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,30 @@ function hash_to_alt_sequence2(iterable $hash): iterable
}

}

/**
* @return \Generator<int, int, int, void>
*/
function yieldWithIntSendType(){
yield 1;
$something = yield 1;
var_dump(yield 1);
}

/**
* @return \Generator<int, int, void, void>
*/
function yieldWithVoidSendType(){
yield 1;
$something = yield 1;
var_dump(yield 1);
}

/**
* @return \Generator<int, int>
*/
function yieldWithoutSendType(){
yield 1;
$something = yield 1;
var_dump(yield 1);
}

0 comments on commit ee64600

Please sign in to comment.