diff --git a/conf/config.neon b/conf/config.neon index d4d5342b62..7f494ce35b 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1273,6 +1273,11 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\TriggerErrorDynamicReturnTypeExtension + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension + - class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension tags: diff --git a/src/Type/Php/TriggerErrorDynamicReturnTypeExtension.php b/src/Type/Php/TriggerErrorDynamicReturnTypeExtension.php new file mode 100644 index 0000000000..3aca7ecb2c --- /dev/null +++ b/src/Type/Php/TriggerErrorDynamicReturnTypeExtension.php @@ -0,0 +1,37 @@ +getName() === 'trigger_error'; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type + { + if (count($functionCall->getArgs()) < 2) { + return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); + } + + $errorType = $scope->getType($functionCall->getArgs()[1]->value); + if ($errorType instanceof ConstantScalarType) { + if ($errorType->getValue() === E_USER_ERROR) { + return new NeverType(true); + } + } + + return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); + } + +} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 51c360228a..63d244aaa4 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -527,6 +527,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5501.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4743.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5017.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5992.php'); if (PHP_VERSION_ID >= 70400) { yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php'); diff --git a/tests/PHPStan/Analyser/data/bug-5992.php b/tests/PHPStan/Analyser/data/bug-5992.php new file mode 100644 index 0000000000..6c1ab9c021 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-5992.php @@ -0,0 +1,24 @@ +