diff --git a/extension.neon b/extension.neon index 5ad7e2ff6..054a8e8f3 100644 --- a/extension.neon +++ b/extension.neon @@ -184,6 +184,7 @@ services: tags: - phpstan.typeSpecifier.functionTypeSpecifyingExtension arguments: + methodName: 'abort' negate: false - @@ -191,6 +192,23 @@ services: tags: - phpstan.typeSpecifier.functionTypeSpecifyingExtension arguments: + methodName: 'abort' + negate: true + + - + class: NunoMaduro\Larastan\Types\AbortIfFunctionTypeSpecifyingExtension + tags: + - phpstan.typeSpecifier.functionTypeSpecifyingExtension + arguments: + methodName: throw + negate: false + + - + class: NunoMaduro\Larastan\Types\AbortIfFunctionTypeSpecifyingExtension + tags: + - phpstan.typeSpecifier.functionTypeSpecifyingExtension + arguments: + methodName: throw negate: true - diff --git a/src/Types/AbortIfFunctionTypeSpecifyingExtension.php b/src/Types/AbortIfFunctionTypeSpecifyingExtension.php index cb28fdf2a..942b9f15e 100644 --- a/src/Types/AbortIfFunctionTypeSpecifyingExtension.php +++ b/src/Types/AbortIfFunctionTypeSpecifyingExtension.php @@ -30,9 +30,13 @@ final class AbortIfFunctionTypeSpecifyingExtension implements FunctionTypeSpecif /** @var bool */ protected $negate; - public function __construct(bool $negate) + /** @var string */ + protected $methodName; + + public function __construct(bool $negate, string $methodName) { $this->negate = $negate; + $this->methodName = $methodName.'_'.($negate === false ? 'if' : 'unless'); } public function isFunctionSupported( @@ -40,9 +44,7 @@ public function isFunctionSupported( FuncCall $node, TypeSpecifierContext $context ): bool { - $methodName = $this->negate === false ? 'abort_if' : 'abort_unless'; - - return $functionReflection->getName() === $methodName && $context->null(); + return $functionReflection->getName() === $this->methodName && $context->null(); } public function specifyTypes( diff --git a/tests/Features/Types/ThrowIf.php b/tests/Features/Types/ThrowIf.php new file mode 100644 index 000000000..9118f174e --- /dev/null +++ b/tests/Features/Types/ThrowIf.php @@ -0,0 +1,33 @@ +