Skip to content

Commit 59be92f

Browse files
committed
Support for native never return type
1 parent 280175f commit 59be92f

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/Type/ParserNodeTypeToPHPStanType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public static function resolve($type, ?ClassReflection $classReflection): Type
8282
return new NullType();
8383
} elseif ($type === 'mixed') {
8484
return new MixedType(true);
85+
} elseif ($type === 'never') {
86+
return new NeverType(true);
8587
}
8688

8789
return new MixedType();

src/Type/TypehintHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ private static function getTypeObjectFromTypehint(string $typeString, ?string $s
5757
return new ErrorType();
5858
case 'null':
5959
return new NullType();
60+
case 'never':
61+
return new NeverType(true);
6062
default:
6163
return new ObjectType($typeString);
6264
}
@@ -102,6 +104,9 @@ public static function decideTypeFromReflection(
102104
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\null')) {
103105
$reflectionTypeString = 'null';
104106
}
107+
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\never')) {
108+
$reflectionTypeString = 'never';
109+
}
105110

106111
$type = self::getTypeObjectFromTypehint($reflectionTypeString, $selfClass);
107112
if ($reflectionType->allowsNull()) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ public function dataFileAsserts(): iterable
532532
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php');
533533
}
534534

535+
if (PHP_VERSION_ID >= 80100 || self::$useStaticReflectionProvider) {
536+
yield from $this->gatherAssertTypes(__DIR__ . '/data/never.php');
537+
}
538+
535539
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2760.php');
536540
}
537541

tests/PHPStan/Analyser/data/never.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NeverTest;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(): never
11+
{
12+
exit();
13+
}
14+
15+
public function doBar()
16+
{
17+
assertType('*NEVER*', $this->doFoo());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)