Skip to content

Commit

Permalink
ReflectionEnum runtime stubs (PHP 8.0+)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 6, 2021
1 parent 5a90731 commit e3a0e0d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ public static function begin(
self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled);
}

if (PHP_VERSION_ID >= 80000) {
require_once __DIR__ . '/../../stubs/runtime/Enum/UnitEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/BackedEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumUnitCase.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumBackedCase.php';
}

foreach ($container->getParameter('scanFiles') as $scannedFile) {
if (is_file($scannedFile)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ private function isClassBlacklisted(string $className): bool
if (!class_exists($className, false)) {
return true;
}
if (in_array(strtolower($className), ['reflectionuniontype', 'attribute', 'returntypewillchange', 'reflectionintersectiontype'], true)) {
if (in_array(strtolower($className), [
'reflectionuniontype',
'attribute',
'returntypewillchange',
'reflectionintersectiontype',
'unitenum',
'backedenum',
'reflectionenum',
'reflectionenumunitcase',
'reflectionenumbackedcase',
], true)) {
return true;
}
$reflection = new \ReflectionClass($className);
Expand Down
8 changes: 8 additions & 0 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public static function getContainer(): Container
require_once $file;
})($bootstrapFile);
}

if (PHP_VERSION_ID >= 80000) {
require_once __DIR__ . '/../../stubs/runtime/Enum/UnitEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/BackedEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnum.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumUnitCase.php';
require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumBackedCase.php';
}
}

return self::$containers[$cacheKey];
Expand Down
14 changes: 14 additions & 0 deletions stubs/runtime/Enum/BackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

if (\PHP_VERSION_ID < 80100) {
if (interface_exists('BackedEnum', false)) {
return;
}

interface BackedEnum extends UnitEnum
{
public static function from(int|string $value): static;

public static function tryFrom(int|string $value): ?static;
}
}
32 changes: 32 additions & 0 deletions stubs/runtime/Enum/ReflectionEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

if (\PHP_VERSION_ID < 80100) {
if (class_exists('ReflectionEnum', false)) {
return;
}

class ReflectionEnum extends ReflectionClass
{
public function __construct(object|string $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
{
}
}
}
14 changes: 14 additions & 0 deletions stubs/runtime/Enum/ReflectionEnumBackedCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

if (\PHP_VERSION_ID < 80100) {
if (class_exists('ReflectionEnumBackedCase', false)) {
return;
}

class ReflectionEnumBackedCase extends ReflectionEnumUnitCase
{
public function getBackingValue(): int|string
{
}
}
}
25 changes: 25 additions & 0 deletions stubs/runtime/Enum/ReflectionEnumUnitCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

if (\PHP_VERSION_ID < 80100) {
if (class_exists('ReflectionEnumUnitCase', false)) {
return;
}

class ReflectionEnumUnitCase extends ReflectionClassConstant
{
public function __construct(object|string $class, string $constant)
{
}

public function getValue(): UnitEnum
{
}

/**
* @return ReflectionEnum
*/
public function getEnum(): ReflectionEnum
{
}
}
}
15 changes: 15 additions & 0 deletions stubs/runtime/Enum/UnitEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if (\PHP_VERSION_ID < 80100) {
if (interface_exists('UnitEnum', false)) {
return;
}

interface UnitEnum
{
/**
* @return static[]
*/
public static function cases(): array;
}
}

0 comments on commit e3a0e0d

Please sign in to comment.