Skip to content

Commit 9ecefd5

Browse files
committed
Bleeding edge - MissingReturnRule - make the error non-ignorable for native typehints
1 parent f306a8b commit 9ecefd5

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

conf/config.level0.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ services:
172172
arguments:
173173
checkExplicitMixedMissingReturn: %checkExplicitMixedMissingReturn%
174174
checkPhpDocMissingReturn: %checkPhpDocMissingReturn%
175+
bleedingEdge: %featureToggles.bleedingEdge%
175176
tags:
176177
- phpstan.rules.rule
177178

src/Rules/Missing/MissingReturnRule.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class MissingReturnRule implements Rule
2828

2929
private bool $checkPhpDocMissingReturn;
3030

31+
private bool $bleedingEdge;
32+
3133
public function __construct(
3234
bool $checkExplicitMixedMissingReturn,
33-
bool $checkPhpDocMissingReturn
35+
bool $checkPhpDocMissingReturn,
36+
bool $bleedingEdge = false
3437
)
3538
{
3639
$this->checkExplicitMixedMissingReturn = $checkExplicitMixedMissingReturn;
3740
$this->checkPhpDocMissingReturn = $checkPhpDocMissingReturn;
41+
$this->bleedingEdge = $bleedingEdge;
3842
}
3943

4044
public function getNodeType(): string
@@ -106,8 +110,14 @@ public function processNode(Node $node, Scope $scope): array
106110
}
107111

108112
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
113+
$errorBuilder = RuleErrorBuilder::message(sprintf('%s should always throw an exception or terminate script execution but doesn\'t do that.', $description))->line($node->getNode()->getStartLine());
114+
115+
if ($this->bleedingEdge && $node->hasNativeReturnTypehint()) {
116+
$errorBuilder->nonIgnorable();
117+
}
118+
109119
return [
110-
RuleErrorBuilder::message(sprintf('%s should always throw an exception or terminate script execution but doesn\'t do that.', $description))->line($node->getNode()->getStartLine())->build(),
120+
$errorBuilder->build(),
111121
];
112122
}
113123

@@ -123,10 +133,16 @@ public function processNode(Node $node, Scope $scope): array
123133
return [];
124134
}
125135

136+
$errorBuilder = RuleErrorBuilder::message(
137+
sprintf('%s should return %s but return statement is missing.', $description, $returnType->describe(VerbosityLevel::typeOnly()))
138+
)->line($node->getNode()->getStartLine());
139+
140+
if ($this->bleedingEdge && $node->hasNativeReturnTypehint()) {
141+
$errorBuilder->nonIgnorable();
142+
}
143+
126144
return [
127-
RuleErrorBuilder::message(
128-
sprintf('%s should return %s but return statement is missing.', $description, $returnType->describe(VerbosityLevel::typeOnly()))
129-
)->line($node->getNode()->getStartLine())->build(),
145+
$errorBuilder->build(),
130146
];
131147
}
132148

0 commit comments

Comments
 (0)