Skip to content

Commit e97439c

Browse files
committed
Fix BackedEnum::from() and tryFrom() methods
1 parent ae35752 commit e97439c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Reflection/SignatureMap/Php8SignatureMapProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function __construct(
5050
public function hasMethodSignature(string $className, string $methodName, int $variant = 0): bool
5151
{
5252
$lowerClassName = strtolower($className);
53+
if ($lowerClassName === 'backedenum') {
54+
return false;
55+
}
5356
if (!array_key_exists($lowerClassName, $this->map->classes)) {
5457
return $this->functionSignatureMapProvider->hasMethodSignature($className, $methodName, $variant);
5558
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ public function dataFileAsserts(): iterable
896896
}
897897

898898
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7068.php');
899+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7096.php');
899900
}
900901

901902
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Bug7096;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param class-string<\BackedEnum> $enumClass
12+
*/
13+
function enumFromString(string $enumClass, string|int $value): void
14+
{
15+
assertType(\BackedEnum::class, $enumClass::from($value));
16+
assertType(\BackedEnum::class . '|null', $enumClass::tryFrom($value));
17+
}
18+
19+
function customStaticMethod(): static
20+
{
21+
return new static();
22+
}
23+
24+
/**
25+
* @param class-string<self> $class
26+
*/
27+
function test(string $class): void
28+
{
29+
assertType(self::class, $class::customStaticMethod());
30+
}
31+
32+
}

0 commit comments

Comments
 (0)