Skip to content

Commit

Permalink
[GENERATED] Downgraded
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 5, 2024
1 parent 1d95621 commit 5d2eaf5
Show file tree
Hide file tree
Showing 188 changed files with 3,668 additions and 4,879 deletions.
70 changes: 47 additions & 23 deletions src/BetterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,60 @@

final class BetterReflection
{
public static int $phpVersion = PHP_VERSION_ID;
/**
* @var int
*/
public static $phpVersion = PHP_VERSION_ID;

private static SourceLocator|null $sharedSourceLocator = null;
/**
* @var \Roave\BetterReflection\SourceLocator\Type\SourceLocator|null
*/
private static $sharedSourceLocator = null;

private SourceLocator|null $sourceLocator = null;
/**
* @var \Roave\BetterReflection\SourceLocator\Type\SourceLocator|null
*/
private $sourceLocator = null;

private static Reflector|null $sharedReflector = null;
/**
* @var \Roave\BetterReflection\Reflector\Reflector|null
*/
private static $sharedReflector = null;

private Reflector|null $reflector = null;
/**
* @var \Roave\BetterReflection\Reflector\Reflector|null
*/
private $reflector = null;

private static Parser|null $sharedPhpParser = null;
/**
* @var \PhpParser\Parser|null
*/
private static $sharedPhpParser = null;

private Parser|null $phpParser = null;
/**
* @var \PhpParser\Parser|null
*/
private $phpParser = null;

private AstLocator|null $astLocator = null;
/**
* @var AstLocator|null
*/
private $astLocator = null;

private FindReflectionOnLine|null $findReflectionOnLine = null;
/**
* @var \Roave\BetterReflection\Util\FindReflectionOnLine|null
*/
private $findReflectionOnLine = null;

private SourceStubber|null $sourceStubber = null;
/**
* @var \Roave\BetterReflection\SourceLocator\SourceStubber\SourceStubber|null
*/
private $sourceStubber = null;

private static SourceStubber|null $sharedSourceStubber = null;
/**
* @var \Roave\BetterReflection\SourceLocator\SourceStubber\SourceStubber|null
*/
private static $sharedSourceStubber = null;

/**
* @var Standard|null
Expand All @@ -59,14 +92,8 @@ final class BetterReflection
*/
private $printer = null;

public static function populate(
int $phpVersion,
SourceLocator $sourceLocator,
Reflector $classReflector,
Parser $phpParser,
SourceStubber $sourceStubber,
Standard $printer,
): void {
public static function populate(int $phpVersion, SourceLocator $sourceLocator, Reflector $classReflector, Parser $phpParser, SourceStubber $sourceStubber, Standard $printer): void
{
self::$phpVersion = $phpVersion;
self::$sharedSourceLocator = $sourceLocator;
self::$sharedReflector = $classReflector;
Expand Down Expand Up @@ -126,10 +153,7 @@ public function findReflectionsOnLine(): FindReflectionOnLine
public function sourceStubber(): SourceStubber
{
return $this->sourceStubber
?? $this->sourceStubber = new AggregateSourceStubber(
new PhpStormStubsSourceStubber($this->phpParser(), $this->printer(), self::$phpVersion),
new ReflectionSourceStubber($this->printer()),
);
?? $this->sourceStubber = new AggregateSourceStubber(new PhpStormStubsSourceStubber($this->phpParser(), $this->printer(), self::$phpVersion), new ReflectionSourceStubber($this->printer()));
}

public function printer(): Standard
Expand Down
12 changes: 10 additions & 2 deletions src/Identifier/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ class Identifier

private const VALID_NAME_REGEXP = '/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*/';

private string $name;
/**
* @var string
*/
private $name;
/**
* @var \Roave\BetterReflection\Identifier\IdentifierType
*/
private $type;

/** @throws InvalidIdentifierName */
public function __construct(string $name, private IdentifierType $type)
public function __construct(string $name, IdentifierType $type)
{
$this->type = $type;
if (
$name === self::WILDCARD
|| $name === ReflectionFunction::CLOSURE_NAME
Expand Down
10 changes: 5 additions & 5 deletions src/Identifier/IdentifierType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class IdentifierType
self::IDENTIFIER_CONSTANT => null,
];

private string $name;
/**
* @var string
*/
private $name;

public function __construct(string $type = self::IDENTIFIER_CLASS)
{
if (! array_key_exists($type, self::VALID_TYPES)) {
throw new InvalidArgumentException(sprintf(
'%s is not a valid identifier type',
$type,
));
throw new InvalidArgumentException(sprintf('%s is not a valid identifier type', $type));
}

$this->name = $type;
Expand Down
40 changes: 26 additions & 14 deletions src/NodeCompiler/CompileNodeToValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __invoke(Node $node, CompilerContext $context): CompiledValue
$constantName = $this->resolveClassConstantName($node, $context);
}

$constExprEvaluator = new ConstExprEvaluator(function (Node\Expr $node) use ($context, $constantName): mixed {
$constExprEvaluator = new ConstExprEvaluator(function (Node\Expr $node) use ($context, $constantName) {
if ($node instanceof Node\Expr\ConstFetch) {
return $this->getConstantValue($node, $constantName, $context);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public function __invoke(Node $node, CompilerContext $context): CompiledValue
}

if ($node instanceof Node\Scalar\MagicConst\Function_) {
return $context->getFunction()?->getName() ?? '';
return (($getFunction = $context->getFunction()) ? $getFunction->getName() : null) ?? '';
}

if ($node instanceof Node\Scalar\MagicConst\Trait_) {
Expand Down Expand Up @@ -140,7 +140,10 @@ public function __invoke(Node $node, CompilerContext $context): CompiledValue
return new CompiledValue($value, $constantName);
}

private function getEnumPropertyValue(Node\Expr\PropertyFetch $node, CompilerContext $context): mixed
/**
* @return mixed
*/
private function getEnumPropertyValue(Node\Expr\PropertyFetch $node, CompilerContext $context)
{
assert($node->var instanceof Node\Expr\ClassConstFetch);
assert($node->var->class instanceof Node\Name);
Expand All @@ -165,11 +168,14 @@ private function getEnumPropertyValue(Node\Expr\PropertyFetch $node, CompilerCon

assert($node->name instanceof Node\Identifier);

return match ($node->name->toString()) {
'value' => $case->getValue(),
'name' => $case->getName(),
default => throw Exception\UnableToCompileNode::becauseOfInvalidEnumCasePropertyFetch($context, $class, $node),
};
switch ($node->name->toString()) {
case 'value':
return $case->getValue();
case 'name':
return $case->getName();
default:
throw Exception\UnableToCompileNode::becauseOfInvalidEnumCasePropertyFetch($context, $class, $node);
}
}

private function resolveConstantName(Node\Expr\ConstFetch $constNode, CompilerContext $context): string
Expand Down Expand Up @@ -202,16 +208,19 @@ private function constantExists(string $constantName, CompilerContext $context):
$context->getReflector()->reflectConstant($constantName);

return true;
} catch (IdentifierNotFound) {
} catch (IdentifierNotFound $exception) {
return false;
}
}

private function getConstantValue(Node\Expr\ConstFetch $node, string|null $constantName, CompilerContext $context): mixed
/**
* @return mixed
*/
private function getConstantValue(Node\Expr\ConstFetch $node, ?string $constantName, CompilerContext $context)
{
// It's not resolved when constant value is expression
// @infection-ignore-all Assignment, AssignCoalesce: There's no difference, ??= is just optimization
$constantName ??= $this->resolveConstantName($node, $context);
$constantName = $constantName ?? $this->resolveConstantName($node, $context);

if (defined($constantName)) {
return constant($constantName);
Expand All @@ -230,11 +239,14 @@ private function resolveClassConstantName(Node\Expr\ClassConstFetch $node, Compi
return sprintf('%s::%s', $this->resolveClassName($className, $context), $constantName);
}

private function getClassConstantValue(Node\Expr\ClassConstFetch $node, string|null $classConstantName, CompilerContext $context): mixed
/**
* @return mixed
*/
private function getClassConstantValue(Node\Expr\ClassConstFetch $node, ?string $classConstantName, CompilerContext $context)
{
// It's not resolved when constant value is expression
// @infection-ignore-all Assignment, AssignCoalesce: There's no difference, ??= is just optimization
$classConstantName ??= $this->resolveClassConstantName($node, $context);
$classConstantName = $classConstantName ?? $this->resolveClassConstantName($node, $context);

[$className, $constantName] = explode('::', $classConstantName);
assert($constantName !== '');
Expand Down Expand Up @@ -326,7 +338,7 @@ private function compileFileConstant(CompilerContext $context, Node\Scalar\Magic
*/
private function compileClassConstant(CompilerContext $context): string
{
return $context->getClass()?->getName() ?? '';
return (($getClass = $context->getClass()) ? $getClass->getName() : null) ?? '';
}

private function resolveClassName(string $className, CompilerContext $context): string
Expand Down
15 changes: 14 additions & 1 deletion src/NodeCompiler/CompiledValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
/** @internal */
class CompiledValue
{
public function __construct(public mixed $value, public string|null $constantName = null)
/**
* @var mixed
*/
public $value;
/**
* @var string|null
*/
public $constantName = null;
/**
* @param mixed $value
*/
public function __construct($value, ?string $constantName = null)
{
$this->value = $value;
$this->constantName = $constantName;
}
}
35 changes: 24 additions & 11 deletions src/NodeCompiler/CompilerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@
/** @internal */
class CompilerContext
{
public function __construct(
private Reflector $reflector,
private ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionEnumCase|ReflectionMethod|ReflectionFunction|ReflectionParameter|ReflectionConstant $contextReflection,
) {
/**
* @var \Roave\BetterReflection\Reflector\Reflector
*/
private $reflector;
/**
* @var \Roave\BetterReflection\Reflection\ReflectionClass|\Roave\BetterReflection\Reflection\ReflectionProperty|\Roave\BetterReflection\Reflection\ReflectionClassConstant|\Roave\BetterReflection\Reflection\ReflectionEnumCase|\Roave\BetterReflection\Reflection\ReflectionMethod|\Roave\BetterReflection\Reflection\ReflectionFunction|\Roave\BetterReflection\Reflection\ReflectionParameter|\Roave\BetterReflection\Reflection\ReflectionConstant
*/
private $contextReflection;
/**
* @param \Roave\BetterReflection\Reflection\ReflectionClass|\Roave\BetterReflection\Reflection\ReflectionProperty|\Roave\BetterReflection\Reflection\ReflectionClassConstant|\Roave\BetterReflection\Reflection\ReflectionEnumCase|\Roave\BetterReflection\Reflection\ReflectionMethod|\Roave\BetterReflection\Reflection\ReflectionFunction|\Roave\BetterReflection\Reflection\ReflectionParameter|\Roave\BetterReflection\Reflection\ReflectionConstant $contextReflection
*/
public function __construct(Reflector $reflector, $contextReflection)
{
$this->reflector = $reflector;
$this->contextReflection = $contextReflection;
}

public function getReflector(): Reflector
{
return $this->reflector;
}

/** @return non-empty-string|null */
public function getFileName(): string|null
public function getFileName(): ?string
{
if ($this->contextReflection instanceof ReflectionConstant) {
$fileName = $this->contextReflection->getFileName();
Expand All @@ -41,7 +51,7 @@ public function getFileName(): string|null
return $this->realPath($fileName);
}

$fileName = $this->getClass()?->getFileName() ?? $this->getFunction()?->getFileName();
$fileName = (($getClass = $this->getClass()) ? $getClass->getFileName() : null) ?? (($getFunction = $this->getFunction()) ? $getFunction->getFileName() : null);
if ($fileName === null) {
return null;
}
Expand All @@ -54,17 +64,17 @@ private function realPath(string $fileName): string
return FileHelper::normalizePath($fileName, '/');
}

public function getNamespace(): string|null
public function getNamespace(): ?string
{
if ($this->contextReflection instanceof ReflectionConstant) {
return $this->contextReflection->getNamespaceName();
}

// @infection-ignore-all Coalesce: There's no difference
return $this->getClass()?->getNamespaceName() ?? $this->getFunction()?->getNamespaceName();
return (($getClass = $this->getClass()) ? $getClass->getNamespaceName() : null) ?? (($getFunction = $this->getFunction()) ? $getFunction->getNamespaceName() : null);
}

public function getClass(): ReflectionClass|null
public function getClass(): ?\Roave\BetterReflection\Reflection\ReflectionClass
{
if ($this->contextReflection instanceof ReflectionClass) {
return $this->contextReflection;
Expand All @@ -89,7 +99,10 @@ public function getClass(): ReflectionClass|null
return $this->contextReflection->getImplementingClass();
}

public function getFunction(): ReflectionMethod|ReflectionFunction|null
/**
* @return \Roave\BetterReflection\Reflection\ReflectionMethod|\Roave\BetterReflection\Reflection\ReflectionFunction|null
*/
public function getFunction()
{
if ($this->contextReflection instanceof ReflectionMethod) {
return $this->contextReflection;
Expand Down
Loading

0 comments on commit 5d2eaf5

Please sign in to comment.