Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforceNativeReturn: fix wrong never suggestion #116

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/Rule/EnforceNativeReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Generator;
use LogicException;
use PhpParser\Node;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\FunctionReturnStatementsNode;
Expand Down Expand Up @@ -87,8 +88,9 @@ public function processNode(Node $node, Scope $scope): array

$phpDocReturnType = $this->getPhpDocReturnType($node, $scope);
$returnType = $phpDocReturnType ?? $this->getTypeOfReturnStatements($node);
$alwaysThrows = $this->alwaysThrowsException($node);

$typeHint = $this->getTypehintByType($returnType, $scope, $phpDocReturnType !== null, $node->getStatementResult()->isAlwaysTerminating(), true);
$typeHint = $this->getTypehintByType($returnType, $scope, $phpDocReturnType !== null, $alwaysThrows, true);

if ($typeHint === null) {
return [];
Expand All @@ -103,7 +105,7 @@ private function getTypehintByType(
Type $type,
Scope $scope,
bool $typeFromPhpDoc,
bool $alwaysTerminating,
bool $alwaysThrowsException,
bool $topLevel
): ?string
{
Expand All @@ -116,7 +118,7 @@ private function getTypehintByType(
}

if ($type instanceof NeverType) {
if (($typeFromPhpDoc || $alwaysTerminating) && $this->phpVersion->getVersionId() >= 80_100) {
if (($typeFromPhpDoc || $alwaysThrowsException) && $this->phpVersion->getVersionId() >= 80_100) {
return 'never';
}

Expand Down Expand Up @@ -166,10 +168,10 @@ private function getTypehintByType(
$typeHint = 'callable';
} elseif ((new IterableType(new MixedType(), new MixedType()))->accepts($typeWithoutNull, $scope->isDeclareStrictTypes())->yes()) {
$typeHint = 'iterable';
} elseif ($this->getUnionTypehint($type, $scope, $typeFromPhpDoc, $alwaysTerminating) !== null) {
return $this->getUnionTypehint($type, $scope, $typeFromPhpDoc, $alwaysTerminating);
} elseif ($this->getIntersectionTypehint($type, $scope, $typeFromPhpDoc, $alwaysTerminating) !== null) {
return $this->getIntersectionTypehint($type, $scope, $typeFromPhpDoc, $alwaysTerminating);
} elseif ($this->getUnionTypehint($type, $scope, $typeFromPhpDoc, $alwaysThrowsException) !== null) {
return $this->getUnionTypehint($type, $scope, $typeFromPhpDoc, $alwaysThrowsException);
} elseif ($this->getIntersectionTypehint($type, $scope, $typeFromPhpDoc, $alwaysThrowsException) !== null) {
return $this->getIntersectionTypehint($type, $scope, $typeFromPhpDoc, $alwaysThrowsException);
} elseif ((new ObjectWithoutClassType())->accepts($typeWithoutNull, $scope->isDeclareStrictTypes())->yes()) {
$typeHint = 'object';
}
Expand Down Expand Up @@ -254,7 +256,12 @@ private function getClassName(Scope $scope): ?string
return $scope->getClassReflection()->getName();
}

private function getUnionTypehint(Type $type, Scope $scope, bool $typeFromPhpDoc, bool $alwaysTerminating): ?string
private function getUnionTypehint(
Type $type,
Scope $scope,
bool $typeFromPhpDoc,
bool $alwaysThrowsException
): ?string
{
if (!$type instanceof UnionType) {
return null;
Expand All @@ -277,7 +284,7 @@ private function getUnionTypehint(Type $type, Scope $scope, bool $typeFromPhpDoc
$wrap = true;
}

$subtypeHint = $this->getTypehintByType($subtype, $scope, $typeFromPhpDoc, $alwaysTerminating, false);
$subtypeHint = $this->getTypehintByType($subtype, $scope, $typeFromPhpDoc, $alwaysThrowsException, false);

if ($subtypeHint === null) {
return null;
Expand All @@ -297,7 +304,7 @@ private function getIntersectionTypehint(
Type $type,
Scope $scope,
bool $typeFromPhpDoc,
bool $alwaysTerminating
bool $alwaysThrowsException
): ?string
{
if (!$type instanceof IntersectionType) { // @phpstan-ignore-line ignore instanceof intersection
Expand All @@ -321,7 +328,7 @@ private function getIntersectionTypehint(
$wrap = true;
}

$subtypeHint = $this->getTypehintByType($subtype, $scope, $typeFromPhpDoc, $alwaysTerminating, false);
$subtypeHint = $this->getTypehintByType($subtype, $scope, $typeFromPhpDoc, $alwaysThrowsException, false);

if ($subtypeHint === null) {
return null;
Expand All @@ -337,4 +344,17 @@ private function getIntersectionTypehint(
return implode('&', $typehintParts);
}

private function alwaysThrowsException(ReturnStatementsNode $node): bool
{
$exitPoints = $node->getStatementResult()->getExitPoints();

foreach ($exitPoints as $exitPoint) {
if (!$exitPoint->getStatement() instanceof Throw_) {
return false;
}
}

return $exitPoints !== [];
}

}
9 changes: 9 additions & 0 deletions tests/Rule/data/EnforceNativeReturnTypehintRule/code-82.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ public function requireNever() // error: Missing native return typehint never
throw new \LogicException();
}

public function notRequireNever(bool $decide) // error: Missing native return typehint void
{
if ($decide) {
return;
}

throw new \LogicException();
}

public function returnNewSelf() // error: Missing native return typehint self
{
return new self;
Expand Down