Skip to content

Commit

Permalink
RequirePreviousExceptionPassRule - rename strict mode parameter to re…
Browse files Browse the repository at this point in the history
…portEvenIfExceptionIsNotAcceptableByRethrownOne (#13)
  • Loading branch information
olsavmic committed Aug 4, 2022
1 parent 1c7e459 commit a478a23
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function validate(): void {
```neon
rules:
- ShipMonk\PHPStan\Rule\RequirePreviousExceptionPassRule(
checkRethrownExceptionAcceptsCaughtOne: true
reportEvenIfExceptionIsNotAcceptableByRethrownOne: false
)
```
```php
Expand All @@ -203,7 +203,7 @@ try {
}
```

- If you want to be even stricter, you can set up `checkRethrownExceptionAcceptsCaughtOne` to `false` and the rule will start reporting even cases where the thrown exception does not have parameter matching the caught exception
- If you want to be even stricter, you can set up `reportEvenIfExceptionIsNotAcceptableByRethrownOne` to `true` and the rule will start reporting even cases where the thrown exception does not have parameter matching the caught exception
- That will force you to add the parameter to be able to pass it as previous
- Usable only if you do not throw exceptions from libraries, which is a good practice anyway

Expand Down
8 changes: 4 additions & 4 deletions src/Rule/RequirePreviousExceptionPassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class RequirePreviousExceptionPassRule implements Rule

private Standard $printer;

private bool $checkRethrownExceptionAcceptsCaughtOne;
private bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne;

public function __construct(Standard $printer, bool $checkRethrownExceptionAcceptsCaughtOne = true)
public function __construct(Standard $printer, bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = false)
{
$this->printer = $printer;
$this->checkRethrownExceptionAcceptsCaughtOne = $checkRethrownExceptionAcceptsCaughtOne;
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne = $reportEvenIfExceptionIsNotAcceptableByRethrownOne;
}

public function getNodeType(): string
Expand Down Expand Up @@ -121,7 +121,7 @@ private function processExceptionCreation(bool $strictTypes, ?string $caughtExce
}
}

if ($this->checkRethrownExceptionAcceptsCaughtOne) {
if (!$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne) {
$accepts = false;

foreach ($this->getCallLikeParameters($node, $scope) as $parameter) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Rule/RequirePreviousExceptionPassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
class RequirePreviousExceptionPassRuleTest extends RuleTestCase
{

private ?bool $checkRethrownExceptionAcceptsCaughtOne = null;
private ?bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = null;

protected function getRule(): Rule
{
if ($this->checkRethrownExceptionAcceptsCaughtOne === null) {
if ($this->reportEvenIfExceptionIsNotAcceptableByRethrownOne === null) {
throw new LogicException('Testcase need to initialize this');
}

return new RequirePreviousExceptionPassRule(
self::getContainer()->getByType(Standard::class),
$this->checkRethrownExceptionAcceptsCaughtOne,
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne,
);
}

public function testWithAcceptCheck(): void
{
$this->checkRethrownExceptionAcceptsCaughtOne = true;
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne = false;
$this->analyseFile(__DIR__ . '/data/RequirePreviousExceptionPassRule/with-accept-check.php');
}

public function testWithoutAcceptCheck(): void
{
$this->checkRethrownExceptionAcceptsCaughtOne = false;
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne = true;
$this->analyseFile(__DIR__ . '/data/RequirePreviousExceptionPassRule/without-accept-check.php');
}

Expand Down

0 comments on commit a478a23

Please sign in to comment.