Skip to content

Commit

Permalink
Fix overriding throw point with void
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 22, 2021
1 parent dca48f3 commit da3790e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -1442,7 +1442,12 @@ private function getOverridingThrowPoints(Node\Stmt $statement, MutatingScope $s

$throwsTag = $resolvedPhpDoc->getThrowsTag();
if ($throwsTag !== null) {
return [ThrowPoint::createExplicit($scope, $throwsTag->getType(), $statement, false)];
$throwsType = $throwsTag->getType();
if ($throwsType instanceof VoidType) {
return [];
}

return [ThrowPoint::createExplicit($scope, $throwsType, $statement, false)];
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Exceptions/Bug5364Test.php
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Exceptions;

use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<MissingCheckedExceptionInMethodThrowsRule>
*/
class Bug5364Test extends RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
return new MissingCheckedExceptionInMethodThrowsRule(
new MissingCheckedExceptionInThrowsCheck(new DefaultExceptionTypeResolver(
$this->createReflectionProvider(),
[],
[],
[],
[]
))
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/bug-5364.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/bug-5364.neon',
];
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Exceptions/bug-5364.neon
@@ -0,0 +1,6 @@
parameters:
implicitThrows: false
exceptions:
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-5364.php
@@ -0,0 +1,20 @@
<?php

namespace Bug5364;

class Foo
{

/**
* @throws \Exception
*/
function test(): void {
throw new \Exception();
}

function call_test(): void {
/** @throws void */
$this->test();
}

}

0 comments on commit da3790e

Please sign in to comment.