Skip to content

Validate checkedExceptionClasses config #3987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ parameters:
- 'PHPStan\ShouldNotHappenException'
- 'Symfony\Component\Console\Exception\InvalidArgumentException'
- 'PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation'
- 'PHPStan\BetterReflection\SourceLocator\Exception\InvalidArgumentException'
- 'Symfony\Component\Finder\Exception\DirectoryNotFoundException'
- 'InvalidArgumentException'
- 'PHPStan\DependencyInjection\ParameterNotFoundException'
Expand Down
11 changes: 11 additions & 0 deletions src/Rules/Exceptions/DefaultExceptionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Utils\Strings;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use function count;

/**
Expand All @@ -27,6 +28,16 @@ public function __construct(
private array $checkedExceptionClasses,
)
{
foreach ($this->checkedExceptionClasses as $checkedExceptionClass) {
if (!$this->reflectionProvider->hasClass($checkedExceptionClass)) {
throw new ShouldNotHappenException('Class ' . $checkedExceptionClass . ' used in \'checkedExceptionClasses\' does not exist.');
Copy link
Contributor Author

@janedbal janedbal May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the preferred way of config validation.

}
}
foreach ($this->uncheckedExceptionClasses as $uncheckedExceptionClass) {
if (!$this->reflectionProvider->hasClass($uncheckedExceptionClass)) {
throw new ShouldNotHappenException('Class ' . $uncheckedExceptionClass . ' used in \'uncheckedExceptionClasses\' does not exist.');
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want that behind featureFlag / bleedingEdge? I think this will be pretty rare.

}

public function isCheckedException(string $className, Scope $scope): bool
Expand Down
Loading