Skip to content

Commit cc91080

Browse files
committed
MissingReturnRule - special message for @return never case
1 parent 304b8af commit cc91080

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Rules/Missing/MissingReturnRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\Generic\TemplateMixedType;
1313
use PHPStan\Type\GenericTypeVariableResolver;
1414
use PHPStan\Type\MixedType;
15+
use PHPStan\Type\NeverType;
1516
use PHPStan\Type\TypeWithClassName;
1617
use PHPStan\Type\VerbosityLevel;
1718
use PHPStan\Type\VoidType;
@@ -96,6 +97,12 @@ public function processNode(Node $node, Scope $scope): array
9697
return [];
9798
}
9899

100+
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
101+
return [
102+
RuleErrorBuilder::message(sprintf('%s should always throw an exception or terminate script execution but doesn\'t do that.', $description))->line($node->getNode()->getStartLine())->build(),
103+
];
104+
}
105+
99106
if (
100107
$returnType instanceof MixedType
101108
&& !$returnType instanceof TemplateMixedType

tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public function testRule(): void
9696
'Method MissingReturn\MissingReturnGenerators::bodySpecifiedTReturn() should return string but return statement is missing.',
9797
370,
9898
],
99+
[
100+
'Method MissingReturn\NeverReturn::doBaz() should always throw an exception or terminate script execution but doesn\'t do that.',
101+
473,
102+
],
99103
]);
100104
}
101105

tests/PHPStan/Rules/Missing/data/missing-return.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,32 @@ public function doFoo()
447447
}
448448

449449
}
450+
451+
class NeverReturn
452+
{
453+
454+
/**
455+
* @return never
456+
*/
457+
public function doFoo()
458+
{
459+
throw new \Exception();
460+
}
461+
462+
/**
463+
* @return never
464+
*/
465+
public function doBar()
466+
{
467+
die;
468+
}
469+
470+
/**
471+
* @return never
472+
*/
473+
public function doBaz()
474+
{
475+
476+
}
477+
478+
}

0 commit comments

Comments
 (0)