Skip to content

Commit

Permalink
AddVoidReturnTypeWhereNoReturnRector: fix never type handling (#4918)
Browse files Browse the repository at this point in the history
* AddVoidReturnTypeWhereNoReturnRector: added failling tests

* fix

* use native type

* fix PHPStan

* fix

* Update rules-tests/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector/Fixture/exception.php.inc

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

* Update rules-tests/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector/Fixture/exception.php.inc

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
staabm and samsonasik committed Sep 6, 2023
1 parent eca01b3 commit aee4200
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Rector\Core\Exception\ShouldNotHappenException;

final class ThrowsException
{
public function getValues()
{
throw new ShouldNotHappenException();
}
}
-----
<?php

use Rector\Core\Exception\ShouldNotHappenException;

final class ThrowsException
{
public function getValues(): void
{
throw new ShouldNotHappenException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

final class ExitMethod
{
protected function getValues()
{
exit();
}
}
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

final class ExitMethod
{
protected function getValues(): void
{
exit();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

final class ThrowsMethod
{
protected function getValues()
{
if (rand(0,1)) {
throw new \Exception();
}

echo 'hello world';
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

final class ThrowsMethod
{
protected function getValues(): void
{
if (rand(0,1)) {
throw new \Exception();
}

echo 'hello world';
}
}

?>
14 changes: 1 addition & 13 deletions rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SilentVoidResolver
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
) {
}

Expand All @@ -37,10 +37,6 @@ public function hasExclusiveVoid(ClassMethod | Closure | Function_ $functionLike
return false;
}

if ($this->hasNeverType($functionLike)) {
return false;
}

if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, Yield_::class)) {
return false;
}
Expand Down Expand Up @@ -134,14 +130,6 @@ private function isTryCatchAlwaysReturn(TryCatch $tryCatch): bool
return true;
}

/**
* @see https://phpstan.org/writing-php-code/phpdoc-types#bottom-type
*/
private function hasNeverType(ClassMethod | Closure | Function_ $functionLike): bool
{
return $this->betterNodeFinder->hasInstancesOf($functionLike, [Throw_::class]);
}

private function resolveReturnCount(Switch_ $switch): int
{
$casesWithReturnCount = 0;
Expand Down

0 comments on commit aee4200

Please sign in to comment.