-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backed enums - dynamic return type extension for from() and tryFrom()
- Loading branch information
1 parent
8e4a487
commit edcaaba
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Type/Php/BackedEnumFromMethodDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use BackedEnum; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
use PHPStan\Type\Enum\EnumCaseObjectType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
use function in_array; | ||
|
||
class BackedEnumFromMethodDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return BackedEnum::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return in_array($methodReflection->getName(), ['from', 'tryFrom'], true); | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (!$methodReflection->getDeclaringClass()->isBackedEnum()) { | ||
return null; | ||
} | ||
|
||
$arguments = $methodCall->getArgs(); | ||
if (count($arguments) < 1) { | ||
return null; | ||
} | ||
|
||
$valueType = $scope->getType($arguments[0]->value); | ||
|
||
$enumCases = $methodReflection->getDeclaringClass()->getEnumCases(); | ||
if (count($enumCases) === 0) { | ||
if ($methodReflection->getName() === 'tryFrom') { | ||
return new NullType(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
if (count($valueType->getConstantScalarValues()) === 0) { | ||
return null; | ||
} | ||
|
||
$resultEnumCases = []; | ||
foreach ($enumCases as $enumCase) { | ||
if ($enumCase->getBackingValueType() === null) { | ||
continue; | ||
} | ||
$enumCaseValues = $enumCase->getBackingValueType()->getConstantScalarValues(); | ||
if (count($enumCaseValues) !== 1) { | ||
continue; | ||
} | ||
|
||
foreach ($valueType->getConstantScalarValues() as $value) { | ||
if ($value === $enumCaseValues[0]) { | ||
$resultEnumCases[] = new EnumCaseObjectType($enumCase->getDeclaringEnum()->getName(), $enumCase->getName(), $enumCase->getDeclaringEnum()); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (count($resultEnumCases) === 0) { | ||
if ($methodReflection->getName() === 'tryFrom') { | ||
return new NullType(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return TypeCombinator::union(...$resultEnumCases); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php // lint >= 8.1 | ||
|
||
namespace EnumFrom; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
enum FooIntegerEnum: int | ||
{ | ||
|
||
case BAR = 1; | ||
case BAZ = 2; | ||
|
||
} | ||
|
||
enum FooStringEnum: string | ||
{ | ||
|
||
case BAR = 'bar'; | ||
case BAZ = 'baz'; | ||
|
||
} | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
assertType('1', FooIntegerEnum::BAR->value); | ||
assertType('EnumFrom\FooIntegerEnum::BAR', FooIntegerEnum::BAR); | ||
|
||
assertType('null', FooIntegerEnum::tryFrom(0)); | ||
assertType(FooIntegerEnum::class, FooIntegerEnum::from(0)); | ||
assertType('EnumFrom\FooIntegerEnum::BAR', FooIntegerEnum::tryFrom(0 + 1)); | ||
assertType('EnumFrom\FooIntegerEnum::BAR', FooIntegerEnum::from(1 * FooIntegerEnum::BAR->value)); | ||
|
||
assertType('EnumFrom\FooIntegerEnum::BAZ', FooIntegerEnum::tryFrom(2)); | ||
assertType('EnumFrom\FooIntegerEnum::BAZ', FooIntegerEnum::tryFrom(FooIntegerEnum::BAZ->value)); | ||
assertType('EnumFrom\FooIntegerEnum::BAZ', FooIntegerEnum::from(FooIntegerEnum::BAZ->value)); | ||
|
||
assertType("'bar'", FooStringEnum::BAR->value); | ||
assertType('EnumFrom\FooStringEnum::BAR', FooStringEnum::BAR); | ||
|
||
assertType('null', FooStringEnum::tryFrom('barz')); | ||
assertType(FooStringEnum::class, FooStringEnum::from('barz')); | ||
|
||
assertType('EnumFrom\FooStringEnum::BAR', FooStringEnum::tryFrom('ba' . 'r')); | ||
assertType('EnumFrom\FooStringEnum::BAR', FooStringEnum::from(sprintf('%s%s', 'ba', 'r'))); | ||
|
||
assertType('EnumFrom\FooStringEnum::BAZ', FooStringEnum::tryFrom('baz')); | ||
assertType('EnumFrom\FooStringEnum::BAZ', FooStringEnum::tryFrom(FooStringEnum::BAZ->value)); | ||
assertType('EnumFrom\FooStringEnum::BAZ', FooStringEnum::from(FooStringEnum::BAZ->value)); | ||
} | ||
|
||
} |