Skip to content

Commit

Permalink
fix: complete ReflectionEnum polyfill
Browse files Browse the repository at this point in the history
Since the polyfill is autoloaded, PHPStan seems to discover the class, even with the early return:
Call to an undefined method ReflectionEnum::getCases()
Completing this polyfill solves the issue.

Signed-off-by: Alan Poulain <contact@alanpoulain.eu>
  • Loading branch information
alanpoulain committed Nov 4, 2022
1 parent 0337d92 commit efa495b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 20 additions & 3 deletions polyfill/ReflectionEnumPolyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@
}

/** @internal */
class ReflectionEnum
class ReflectionEnum extends \ReflectionClass
{
public function __construct($enum)
public function __construct($objectOrClass) {}

public function hasCase(string $name): bool
{
}

public function getCases(): array
{
}

public function getCase(string $name): ReflectionEnumUnitCase
{
}

public function isBacked(): bool
{
}
}

public function getBackingType(): ?ReflectionType
{
}
}
9 changes: 8 additions & 1 deletion src/Generator/EnumGenerator/Cases/CaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function array_key_exists;
use function array_map;
use function assert;
use function method_exists;

use const PHP_VERSION_ID;

Expand Down Expand Up @@ -65,6 +66,12 @@ public static function fromReflectionCases(ReflectionEnum $enum)
$backedCases[$singleCase->getName()] = $singleCase->getBackingValue();
}

return BackedCases::fromCasesWithType($backedCases, $backingType->getName());
// PHP 8.2
if (method_exists($backingType, 'getName')) {
$backingTypeName = $backingType->getName();
} else {
$backingTypeName = (string) $backingType;
}
return BackedCases::fromCasesWithType($backedCases, $backingTypeName);
}
}

0 comments on commit efa495b

Please sign in to comment.