-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ForbidCheckedExceptionInYieldingMethodRule (#109)
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\MethodReturnStatementsNode; | ||
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<MethodReturnStatementsNode> | ||
*/ | ||
class ForbidCheckedExceptionInYieldingMethodRule implements Rule | ||
{ | ||
|
||
private DefaultExceptionTypeResolver $exceptionTypeResolver; | ||
|
||
public function __construct(DefaultExceptionTypeResolver $exceptionTypeResolver) | ||
{ | ||
$this->exceptionTypeResolver = $exceptionTypeResolver; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodReturnStatementsNode::class; | ||
} | ||
|
||
/** | ||
* @param MethodReturnStatementsNode $node | ||
* @return list<RuleError> | ||
*/ | ||
public function processNode( | ||
Node $node, | ||
Scope $scope | ||
): array | ||
{ | ||
if (!$node->getStatementResult()->hasYield()) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
|
||
foreach ($node->getStatementResult()->getThrowPoints() as $throwPoint) { | ||
foreach ($throwPoint->getType()->getObjectClassNames() as $exceptionClass) { | ||
if ($this->exceptionTypeResolver->isCheckedException($exceptionClass, $throwPoint->getScope())) { | ||
$errors[] = RuleErrorBuilder::message("Throwing checked exception $exceptionClass in yielding method is denied as it gets thrown upon Generator iteration") | ||
->line($throwPoint->getNode()->getLine()) | ||
->build(); | ||
} | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/Rule/ForbidCheckedExceptionInYieldingMethodRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver; | ||
use PHPStan\Rules\Rule; | ||
use ShipMonk\PHPStan\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ForbidCheckedExceptionInYieldingMethodRule> | ||
*/ | ||
class ForbidCheckedExceptionInYieldingMethodRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$exceptionTypeResolverMock = self::createMock(DefaultExceptionTypeResolver::class); | ||
$exceptionTypeResolverMock | ||
->expects(self::any()) | ||
->method('isCheckedException') | ||
->willReturnCallback(static function (string $className): bool { | ||
return $className === 'ForbidCheckedExceptionInYieldingMethodRule\CheckedException'; | ||
}); | ||
|
||
return new ForbidCheckedExceptionInYieldingMethodRule($exceptionTypeResolverMock); | ||
} | ||
|
||
public function testClass(): void | ||
{ | ||
$this->analyseFile(__DIR__ . '/data/ForbidCheckedExceptionInYieldingMethodRule/code.php'); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
tests/Rule/data/ForbidCheckedExceptionInYieldingMethodRule/code.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ForbidCheckedExceptionInYieldingMethodRule; | ||
|
||
use Generator; | ||
use LogicException; | ||
use RuntimeException; | ||
|
||
class CheckedException extends RuntimeException {} | ||
|
||
class A { | ||
|
||
/** | ||
* @return iterable<int> | ||
* @throws CheckedException | ||
*/ | ||
public static function throwPointOfYieldingMethod(bool $throw): iterable | ||
{ | ||
yield 1; | ||
if ($throw) { | ||
throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInYieldingMethodRule\CheckedException in yielding method is denied as it gets thrown upon Generator iteration | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<int> | ||
* @throws CheckedException | ||
*/ | ||
private static function throwPointOfNonYieldingMethod(bool $throw): iterable | ||
{ | ||
if ($throw) { | ||
throw new CheckedException(); | ||
} | ||
|
||
return [2]; | ||
} | ||
|
||
/** | ||
* @return Generator<int> | ||
*/ | ||
private static function methodWithUncheckedException(bool $throw): Generator | ||
{ | ||
if ($throw) { | ||
throw new LogicException(); | ||
} | ||
|
||
yield 3; | ||
} | ||
|
||
/** | ||
* @throws CheckedException | ||
* @return Generator<int> | ||
*/ | ||
public static function testIt(bool $throw): Generator | ||
{ | ||
yield from self::throwPointOfYieldingMethod($throw); // error: Throwing checked exception ForbidCheckedExceptionInYieldingMethodRule\CheckedException in yielding method is denied as it gets thrown upon Generator iteration | ||
yield from self::throwPointOfNonYieldingMethod($throw); // error: Throwing checked exception ForbidCheckedExceptionInYieldingMethodRule\CheckedException in yielding method is denied as it gets thrown upon Generator iteration | ||
yield from self::methodWithUncheckedException($throw); | ||
} | ||
|
||
} | ||
|