Skip to content

Commit 17b4ef2

Browse files
committed
Added Enum stubs to make method purity precise
1 parent 599ac2b commit 17b4ef2

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

src/PhpDoc/ReflectionEnumStubFilesExtension.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ public function getFiles(): array
1919
return [];
2020
}
2121

22+
$files = [
23+
__DIR__ . '/../../stubs/UnitEnum.stub',
24+
__DIR__ . '/../../stubs/BackedEnum.stub',
25+
];
26+
2227
if (!$this->phpVersion->supportsLazyObjects()) {
23-
return [__DIR__ . '/../../stubs/ReflectionEnum.stub'];
28+
return [__DIR__ . '/../../stubs/ReflectionEnum.stub', ...$files];
2429
}
2530

26-
return [__DIR__ . '/../../stubs/ReflectionEnumWithLazyObjects.stub'];
31+
return [__DIR__ . '/../../stubs/ReflectionEnumWithLazyObjects.stub', ...$files];
2732
}
2833

2934
}

stubs/BackedEnum.stub

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
interface BackedEnum extends UnitEnum
4+
{
5+
/**
6+
* @pure
7+
*/
8+
public static function from(int|string $value): static;
9+
10+
/**
11+
* @pure
12+
*/
13+
public static function tryFrom(int|string $value): ?static;
14+
}

stubs/UnitEnum.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
interface UnitEnum
4+
{
5+
/**
6+
* @return list<static>
7+
* @pure
8+
*/
9+
public static function cases(): array;
10+
}

tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,10 @@ public function testBug12224(): void
174174
]);
175175
}
176176

177+
#[RequiresPhp('>= 8.1')]
178+
public function testBug13201(): void
179+
{
180+
$this->analyse([__DIR__ . '/data/bug-13201.php'], []);
181+
}
182+
177183
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php // lint >= 8.1
2+
3+
namespace PHPStan\Rules\Pure\data\Bug13201;
4+
5+
enum Foo: string
6+
{
7+
8+
case Bar = 'bar';
9+
case Unknown = 'unknown';
10+
11+
}
12+
13+
/**
14+
* @pure
15+
*/
16+
function createWithFallback(string $type): self
17+
{
18+
return Foo::tryFrom($type) ?? Foo::Unknown;
19+
}

0 commit comments

Comments
 (0)