Skip to content

Commit

Permalink
ExceptionTypeResolver - pass Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 20, 2021
1 parent a125304 commit dec03af
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Rules/Exceptions/DefaultExceptionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Exceptions;

use Nette\Utils\Strings;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;

class DefaultExceptionTypeResolver implements ExceptionTypeResolver
Expand Down Expand Up @@ -44,7 +45,7 @@ public function __construct(
$this->checkedExceptionClasses = $checkedExceptionClasses;
}

public function isCheckedException(string $className): bool
public function isCheckedException(string $className, Scope $scope): bool
{
foreach ($this->uncheckedExceptionRegexes as $regex) {
if (Strings::match($className, $regex) !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Exceptions/ExceptionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace PHPStan\Rules\Exceptions;

use PHPStan\Analyser\Scope;

interface ExceptionTypeResolver
{

public function isCheckedException(string $className): bool;
public function isCheckedException(string $className, Scope $scope): bool;

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function check(?Type $throwType, array $throwPoints): array

if (
$throwPointType instanceof TypeWithClassName
&& !$this->exceptionTypeResolver->isCheckedException($throwPointType->getClassName())
&& !$this->exceptionTypeResolver->isCheckedException($throwPointType->getClassName(), $throwPoint->getScope())
) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Rules\Exceptions;

use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\Testing\TestCase;

class DefaultExceptionTypeResolverTest extends TestCase
Expand Down Expand Up @@ -140,7 +142,7 @@ public function testIsCheckedException(
): void
{
$resolver = new DefaultExceptionTypeResolver($this->createBroker(), $uncheckedExceptionRegexes, $uncheckedExceptionClasses, $checkedExceptionRegexes, $checkedExceptionClasses);
$this->assertSame($expectedResult, $resolver->isCheckedException($className));
$this->assertSame($expectedResult, $resolver->isCheckedException($className, self::getContainer()->getByType(ScopeFactory::class)->create(ScopeContext::create(__DIR__))));
}

}

0 comments on commit dec03af

Please sign in to comment.