-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReflectionClass stub with lazy object methods
- Loading branch information
1 parent
1d63c05
commit 25ec5eb
Showing
10 changed files
with
202 additions
and
14 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
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,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
|
||
final class ReflectionClassStubFilesExtension implements StubFilesExtension | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getFiles(): array | ||
{ | ||
if (!$this->phpVersion->supportsLazyObjects()) { | ||
return [ | ||
__DIR__ . '/../../stubs/ReflectionClass.stub', | ||
]; | ||
} | ||
|
||
return [ | ||
__DIR__ . '/../../stubs/ReflectionClassWithLazyObjects.stub', | ||
]; | ||
} | ||
|
||
} |
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,113 @@ | ||
<?php | ||
|
||
/** | ||
* @template T of object | ||
*/ | ||
class ReflectionClass | ||
{ | ||
|
||
/** | ||
* @readonly | ||
* @var class-string<T> | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @param T|class-string<T> $argument | ||
* @throws ReflectionException | ||
*/ | ||
public function __construct($argument) {} | ||
|
||
/** | ||
* @return class-string<T> | ||
*/ | ||
public function getName() : string; | ||
|
||
/** | ||
* @param mixed ...$args | ||
* | ||
* @return T | ||
*/ | ||
public function newInstance(...$args) {} | ||
|
||
/** | ||
* @param array<int|string, mixed> $args | ||
* | ||
* @return T | ||
*/ | ||
public function newInstanceArgs(array $args) {} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function newInstanceWithoutConstructor(); | ||
|
||
/** | ||
* @return list<ReflectionAttribute<object>> | ||
*/ | ||
public function getAttributes(?string $name = null, int $flags = 0) | ||
{ | ||
} | ||
|
||
/** | ||
* @param callable(T): void $initializer | ||
* @return T | ||
*/ | ||
public function newLazyGhost(callable $initializer, int $options = 0): object | ||
{ | ||
} | ||
|
||
/** | ||
* @param callable(T): T $factory | ||
* @return T | ||
*/ | ||
public function newLazyProxy(callable $factory, int $options = 0): object | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
* @param callable(T): void $initializer | ||
*/ | ||
public function resetAsLazyGhost(object $object, callable $initializer, int $options = 0): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
* @param callable(T): T $factory | ||
*/ | ||
public function resetAsLazyProxy(object $object, callable $factory, int $options = 0): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
* @return T | ||
*/ | ||
public function initializeLazyObject(object $object): object | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
* @return T | ||
*/ | ||
public function markLazyObjectAsInitialized(object $object): object | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
*/ | ||
public function getLazyInitializer(object $object): ?callable | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $object | ||
*/ | ||
public function isUninitializedLazyObject(object $object): bool | ||
{ | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
/** | ||
* @template T of UnitEnum | ||
* @extends ReflectionClass<T> | ||
*/ | ||
class ReflectionEnum extends ReflectionClass | ||
{ | ||
|
||
/** | ||
* @return (T is BackedEnum ? ReflectionEnumBackedCase[] : ReflectionEnumUnitCase[]) | ||
*/ | ||
public function getCases(): array {} | ||
|
||
/** | ||
* @return (T is BackedEnum ? ReflectionEnumBackedCase : ReflectionEnumUnitCase) | ||
* @throws ReflectionException | ||
*/ | ||
public function getCase(string $name): ReflectionEnumUnitCase {} | ||
|
||
/** | ||
* @phpstan-assert-if-true self<T&BackedEnum> $this | ||
* @phpstan-assert-if-true !null $this->getBackingType() | ||
*/ | ||
public function isBacked(): bool {} | ||
|
||
} |
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,16 @@ | ||
<?php // lint < 8.4 | ||
|
||
namespace EnumReflection; | ||
|
||
use ReflectionEnum; | ||
use UnitEnum; | ||
use function PHPStan\Testing\assertType; | ||
|
||
/** @param class-string<UnitEnum> $class */ | ||
function testNarrowGetNameTypeAfterIsBacked(string $class) { | ||
$r = new ReflectionEnum($class); | ||
assertType('class-string<UnitEnum>', $r->getName()); | ||
if ($r->isBacked()) { | ||
assertType('class-string<BackedEnum>', $r->getName()); | ||
} | ||
} |
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