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 4 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\DI\Definitions\Statement;
use Nette\DI\Extensions\ExtensionsExtension;
use Nette\DI\Helpers;
use Nette\DI\InvalidConfigurationException;
use Nette\Schema\Context as SchemaContext;
use Nette\Schema\Elements\AnyOf;
use Nette\Schema\Elements\Structure;
Expand Down Expand Up @@ -323,6 +324,34 @@ private function validateParameters(array $parameters, array $parametersSchema):
if ($phpVersion['max'] < $phpVersion['min']) {
throw new InvalidPhpVersionException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
}

if (array_key_exists('featureToggles', $parameters) && is_array($parameters['featureToggles']) && (bool) $parameters['featureToggles']['bleedingEdge']) {
$this->validateExceptionClasses($parameters);
}
}

/**
* @param array<mixed> $parameters
*/
private function validateExceptionClasses(array $parameters): void
{
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();

if (array_key_exists('checkedExceptionClasses', $parameters) && is_array($parameters['checkedExceptionClasses'])) {
foreach ($parameters['checkedExceptionClasses'] as $checkedExceptionClass) {
if (!$reflectionProvider->hasClass($checkedExceptionClass)) {
throw new InvalidConfigurationException('Class ' . $checkedExceptionClass . ' used in \'checkedExceptionClasses\' does not exist.');
}
}
}

if (array_key_exists('uncheckedExceptionClasses', $parameters) && is_array($parameters['uncheckedExceptionClasses'])) {
foreach ($parameters['uncheckedExceptionClasses'] as $uncheckedExceptionClass) {
if (!$reflectionProvider->hasClass($uncheckedExceptionClass)) {
throw new InvalidConfigurationException('Class ' . $uncheckedExceptionClass . ' used in \'uncheckedExceptionClasses\' does not exist.');
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use ThrowsVoidFunction\DifferentException;
use ThrowsVoidFunction\MyException;

/**
Expand Down Expand Up @@ -38,7 +39,13 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
[DifferentException::class],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
15,
],
],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
Expand All @@ -53,7 +60,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
[DifferentException::class],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use ThrowsVoidMethod\DifferentException;
use ThrowsVoidMethod\MyException;
use UnhandledMatchError;
use const PHP_VERSION_ID;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
[DifferentException::class],
[
[
'Method ThrowsVoidMethod\Foo::doFoo() throws exception ThrowsVoidMethod\MyException but the PHPDoc contains @throws void.',
Expand All @@ -55,7 +56,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
[DifferentException::class],
[
[
'Method ThrowsVoidMethod\Foo::doFoo() throws exception ThrowsVoidMethod\MyException but the PHPDoc contains @throws void.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
['ThrowsVoidPropertyHook\\DifferentException'],
[
[
'Get hook for property ThrowsVoidPropertyHook\Foo::$i throws exception ThrowsVoidPropertyHook\MyException but the PHPDoc contains @throws void.',
Expand All @@ -57,7 +57,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
['ThrowsVoidPropertyHook\\DifferentException'],
[
[
'Get hook for property ThrowsVoidPropertyHook\Foo::$i throws exception ThrowsVoidPropertyHook\MyException but the PHPDoc contains @throws void.',
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Exceptions/data/throws-void-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidFunction;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

/**
* @throws void
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Exceptions/data/throws-void-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidMethod;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

class Foo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidPropertyHook;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

class Foo
{
Expand Down