Skip to content

Commit 9f7f6de

Browse files
phpstan-botclaude
authored andcommitted
Gate the magic method never return type on PhpVersion
`never` is only a valid return type since PHP 8.1, so the branch that keeps a declared `never` instead of the return type PHP mandates for a magic method is now guarded by a new PhpVersion::supportsNeverReturnType(). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent c9f93c1 commit 9f7f6de

6 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ public function enterClassMethod(
17141714
$isConstructor,
17151715
$this->attributeReflectionFactory->fromAttrGroups($classMethod->attrGroups, InitializerExprContext::fromStubParameter($this->getClassReflection()->getName(), $this->getFile(), $classMethod)),
17161716
$phpDocPureUnlessCallableIsImpureParameters,
1717+
$this->phpVersion,
17171718
),
17181719
!$classMethod->isStatic(),
17191720
);
@@ -1805,6 +1806,7 @@ public function enterPropertyHook(
18051806
false,
18061807
$this->attributeReflectionFactory->fromAttrGroups($hook->attrGroups, InitializerExprContext::fromStubParameter($this->getClassReflection()->getName(), $this->getFile(), $hook)),
18071808
[],
1809+
$this->phpVersion,
18081810
),
18091811
true,
18101812
);

‎src/Php/PhpVersion.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ public function supportsReadonlyPropertyReinitializationOnClone(): bool
362362
return $this->versionId >= 80300;
363363
}
364364

365+
public function supportsNeverReturnType(): bool
366+
{
367+
return $this->versionId >= 80100;
368+
}
369+
365370
public function supportsNeverReturnTypeInArrowFunction(): bool
366371
{
367372
return $this->versionId >= 80200;

‎src/Reflection/Php/PhpMethodFromParserNodeReflection.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Stmt\ClassMethod;
7+
use PHPStan\Php\PhpVersion;
78
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
89
use PHPStan\Reflection\Assertions;
910
use PHPStan\Reflection\AttributeReflection;
@@ -74,6 +75,7 @@ public function __construct(
7475
private bool $isConstructor,
7576
array $attributes,
7677
array $pureUnlessCallableIsImpureParameters,
78+
PhpVersion $phpVersion,
7779
)
7880
{
7981
if ($this->classMethod instanceof Node\PropertyHook) {
@@ -88,9 +90,9 @@ public function __construct(
8890
if ($this->isConstructor) {
8991
$realReturnType = new VoidType();
9092
}
91-
// PHP always accepts "never" as the declared return type of a magic method,
92-
// even when it otherwise mandates a specific one.
93-
if (!$realReturnType instanceof NeverType) {
93+
// Since PHP 8.1 "never" is always accepted as the declared return type of a magic method,
94+
// even when PHP otherwise mandates a specific one. Before 8.1 "never" is just a class name.
95+
if (!$phpVersion->supportsNeverReturnType() || !$realReturnType instanceof NeverType) {
9496
if (in_array($name, ['__destruct', '__unset', '__wakeup', '__clone'], true)) {
9597
$realReturnType = new VoidType();
9698
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Playground;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<MethodNeverRule>
10+
*/
11+
class MethodNeverRulePhp80Test extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new MethodNeverRule(new NeverRuleHelper());
17+
}
18+
19+
public function testMagicMethods(): void
20+
{
21+
// "never" is not accepted as a magic method return type before PHP 8.1,
22+
// so the return type PHP mandates still wins.
23+
$this->analyse([__DIR__ . '/data/method-never-php80.php'], [
24+
[
25+
'Method MethodNeverPhp80\MagicMethods::__clone() always throws an exception, it should have return type "never".',
26+
8,
27+
],
28+
[
29+
'Method MethodNeverPhp80\MagicMethods::__toString() always throws an exception, it should have return type "never".',
30+
13,
31+
],
32+
]);
33+
}
34+
35+
public static function getAdditionalConfigFiles(): array
36+
{
37+
return [
38+
__DIR__ . '/data/method-never-php-8.0.neon',
39+
];
40+
}
41+
42+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
phpVersion: 80000
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace MethodNeverPhp80;
4+
5+
class MagicMethods
6+
{
7+
8+
public function __clone(): never
9+
{
10+
throw new \Exception();
11+
}
12+
13+
public function __toString(): never
14+
{
15+
throw new \Exception();
16+
}
17+
18+
}

0 commit comments

Comments
 (0)