Skip to content

Commit

Permalink
Bleeding edge - MissingReturnRule - make the error non-ignorable for …
Browse files Browse the repository at this point in the history
…native typehints
  • Loading branch information
ondrejmirtes committed Sep 11, 2021
1 parent f306a8b commit 9ecefd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ services:
arguments:
checkExplicitMixedMissingReturn: %checkExplicitMixedMissingReturn%
checkPhpDocMissingReturn: %checkPhpDocMissingReturn%
bleedingEdge: %featureToggles.bleedingEdge%
tags:
- phpstan.rules.rule

Expand Down
26 changes: 21 additions & 5 deletions src/Rules/Missing/MissingReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ class MissingReturnRule implements Rule

private bool $checkPhpDocMissingReturn;

private bool $bleedingEdge;

public function __construct(
bool $checkExplicitMixedMissingReturn,
bool $checkPhpDocMissingReturn
bool $checkPhpDocMissingReturn,
bool $bleedingEdge = false
)
{
$this->checkExplicitMixedMissingReturn = $checkExplicitMixedMissingReturn;
$this->checkPhpDocMissingReturn = $checkPhpDocMissingReturn;
$this->bleedingEdge = $bleedingEdge;
}

public function getNodeType(): string
Expand Down Expand Up @@ -106,8 +110,14 @@ public function processNode(Node $node, Scope $scope): array
}

if ($returnType instanceof NeverType && $returnType->isExplicit()) {
$errorBuilder = RuleErrorBuilder::message(sprintf('%s should always throw an exception or terminate script execution but doesn\'t do that.', $description))->line($node->getNode()->getStartLine());

if ($this->bleedingEdge && $node->hasNativeReturnTypehint()) {
$errorBuilder->nonIgnorable();
}

return [
RuleErrorBuilder::message(sprintf('%s should always throw an exception or terminate script execution but doesn\'t do that.', $description))->line($node->getNode()->getStartLine())->build(),
$errorBuilder->build(),
];
}

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

$errorBuilder = RuleErrorBuilder::message(
sprintf('%s should return %s but return statement is missing.', $description, $returnType->describe(VerbosityLevel::typeOnly()))
)->line($node->getNode()->getStartLine());

if ($this->bleedingEdge && $node->hasNativeReturnTypehint()) {
$errorBuilder->nonIgnorable();
}

return [
RuleErrorBuilder::message(
sprintf('%s should return %s but return statement is missing.', $description, $returnType->describe(VerbosityLevel::typeOnly()))
)->line($node->getNode()->getStartLine())->build(),
$errorBuilder->build(),
];
}

Expand Down

0 comments on commit 9ecefd5

Please sign in to comment.