Skip to content

Commit

Permalink
Support for native never return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 4, 2021
1 parent 280175f commit 59be92f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Type/ParserNodeTypeToPHPStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public static function resolve($type, ?ClassReflection $classReflection): Type
return new NullType();
} elseif ($type === 'mixed') {
return new MixedType(true);
} elseif ($type === 'never') {
return new NeverType(true);
}

return new MixedType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/TypehintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ private static function getTypeObjectFromTypehint(string $typeString, ?string $s
return new ErrorType();
case 'null':
return new NullType();
case 'never':
return new NeverType(true);
default:
return new ObjectType($typeString);
}
Expand Down Expand Up @@ -102,6 +104,9 @@ public static function decideTypeFromReflection(
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\null')) {
$reflectionTypeString = 'null';
}
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\never')) {
$reflectionTypeString = 'never';
}

$type = self::getTypeObjectFromTypehint($reflectionTypeString, $selfClass);
if ($reflectionType->allowsNull()) {
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php');
}

if (PHP_VERSION_ID >= 80100 || self::$useStaticReflectionProvider) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/never.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2760.php');
}

Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/data/never.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.1

namespace NeverTest;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo(): never
{
exit();
}

public function doBar()
{
assertType('*NEVER*', $this->doFoo());
}

}

0 comments on commit 59be92f

Please sign in to comment.