Skip to content

Commit

Permalink
Reflectors decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 8, 2024
1 parent 07e02dd commit 89284e7
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 469 deletions.
36 changes: 18 additions & 18 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ final class ClassMetadata extends RootMetadata
public function __construct(
ChangeDetector $changeDetector,
string $name,
public readonly bool $internal,
public readonly string|false $extension,
public readonly string|false $file,
public readonly int|false $startLine,
public readonly int|false $endLine,
public readonly string|false $docComment,
public readonly array $attributes,
public readonly array $typeAliases,
public readonly array $templates,
public readonly bool $interface,
public readonly bool $enum,
public readonly bool $trait,
public readonly int $modifiers,
public readonly bool $anonymous,
public readonly bool $deprecated,
public readonly ?NamedObjectType $parentType,
public readonly array $ownInterfaceTypes,
public readonly array $ownProperties,
public readonly array $ownMethods,
public readonly bool $internal = false,
public readonly string|false $extension = false,
public readonly string|false $file = false,
public readonly int|false $startLine = false,
public readonly int|false $endLine = false,
public readonly string|false $docComment = false,
public readonly array $attributes = [],
public readonly array $typeAliases = [],
public readonly array $templates = [],
public readonly bool $interface = false,
public readonly bool $enum = false,
public readonly bool $trait = false,
public readonly bool $anonymous = false,
public readonly bool $deprecated = false,
public readonly ?NamedObjectType $parentType = null,
public readonly array $ownInterfaceTypes = [],
public readonly array $ownProperties = [],
public readonly array $ownMethods = [],
public readonly array $ownTraitTypes = [],
) {
parent::__construct($name, $changeDetector);
Expand Down
22 changes: 11 additions & 11 deletions src/Metadata/MethodMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ final class MethodMetadata
public function __construct(
public readonly string $name,
public readonly string $class,
public readonly array $templates,
public readonly int $modifiers,
public readonly string|false $docComment,
public readonly bool $internal,
public readonly string|false $extension,
public readonly string|false $file,
public readonly int|false $startLine,
public readonly int|false $endLine,
public readonly bool $returnsReference,
public readonly bool $generator,
public readonly bool $deprecated,
public array $parameters,
public TypeMetadata $returnType,
public readonly array $attributes,
public readonly array $templates = [],
public readonly string|false $docComment = false,
public readonly bool $internal = false,
public readonly string|false $extension = false,
public readonly string|false $file = false,
public readonly int|false $startLine = false,
public readonly int|false $endLine = false,
public readonly bool $returnsReference = false,
public readonly bool $generator = false,
public readonly bool $deprecated = false,
public readonly array $attributes = [],
) {}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Metadata/ParameterMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public function __construct(
public readonly string $name,
public readonly ?string $class,
public readonly string $functionOrMethod,
public readonly bool $passedByReference,
public readonly bool $defaultValueAvailable,
public readonly bool $optional,
public readonly bool $variadic,
public readonly bool $promoted,
public readonly bool $deprecated,
public TypeMetadata $type,
public readonly int|false $startLine,
public readonly int|false $endLine,
public readonly array $attributes,
public readonly bool $passedByReference = false,
public readonly bool $defaultValueAvailable = false,
public readonly bool $optional = false,
public readonly bool $variadic = false,
public readonly bool $promoted = false,
public readonly bool $deprecated = false,
public readonly int|false $startLine = false,
public readonly int|false $endLine = false,
public readonly array $attributes = [],
) {}

public function withType(TypeMetadata $type): self
Expand Down
14 changes: 7 additions & 7 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ final class PropertyMetadata
public function __construct(
public readonly string $name,
public readonly string $class,
public readonly string|false $docComment,
public readonly bool $hasDefaultValue,
public readonly bool $promoted,
public readonly int $modifiers,
public readonly bool $deprecated,
public TypeMetadata $type,
public readonly int|false $startLine,
public readonly int|false $endLine,
public readonly array $attributes,
public readonly string|false $docComment = false,
public readonly bool $hasDefaultValue = false,
public readonly bool $promoted = false,
public readonly bool $deprecated = false,
public readonly int|false $startLine = false,
public readonly int|false $endLine = false,
public readonly array $attributes = [],
) {}

public function withType(TypeMetadata $type): self
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/TypeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function __construct(
* @internal
* @psalm-internal Typhoon\Reflection
*/
public static function create(?Type $native, ?Type $phpDoc): self
public static function create(?Type $native = null, ?Type $phpDoc = null): self
{
return new self(
native: $native,
Expand Down
34 changes: 8 additions & 26 deletions src/NativeReflector/NativeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,18 @@ public function reflectClass(\ReflectionClass $class): ClassMetadata
return new ClassMetadata(
changeDetector: ChangeDetector::fromReflection($class),
name: $class->name,
modifiers: $class->getModifiers(),
internal: $class->isInternal(),
extension: $class->getExtensionName(),
file: $class->getFileName(),
startLine: $class->getStartLine(),
endLine: $class->getEndLine(),
docComment: $class->getDocComment(),
attributes: $this->reflectAttributes($class->getAttributes()),
typeAliases: [],
templates: [],
interface: $class->isInterface(),
enum: $class->isEnum(),
trait: $class->isTrait(),
modifiers: $class->getModifiers(),
anonymous: $class->isAnonymous(),
deprecated: false,
parentType: $this->reflectParent($class),
ownInterfaceTypes: array_map(
static fn(string $interface): Type\NamedObjectType => types::object($interface),
Expand Down Expand Up @@ -83,14 +80,11 @@ private function reflectOwnProperties(\ReflectionClass $class): array
$properties[] = new PropertyMetadata(
name: $name,
class: $property->class,
modifiers: $modifiers,
type: $this->reflectType($property->getType(), $class->name),
docComment: $property->getDocComment(),
hasDefaultValue: $property->hasDefaultValue(),
promoted: $property->isPromoted(),
modifiers: $modifiers,
deprecated: false,
type: $this->reflectType($property->getType(), $class->name),
startLine: false,
endLine: false,
attributes: $this->reflectAttributes($property->getAttributes()),
);
}
Expand All @@ -111,8 +105,9 @@ private function reflectOwnMethods(\ReflectionClass $class): array
$methods[] = new MethodMetadata(
name: $method->name,
class: $method->class,
templates: [],
modifiers: $method->getModifiers(),
parameters: $this->reflectParameters($method, $class->name),
returnType: $this->reflectType($method->getReturnType(), $class->name),
docComment: $method->getDocComment(),
internal: $method->isInternal(),
extension: $method->getExtensionName(),
Expand All @@ -122,8 +117,6 @@ class: $method->class,
returnsReference: $method->returnsReference(),
generator: $method->isGenerator(),
deprecated: $method->isDeprecated(),
parameters: $this->reflectParameters($method, $class->name),
returnType: $this->reflectType($method->getReturnType(), $class->name),
attributes: $this->reflectAttributes($method->getAttributes()),
);
}
Expand All @@ -148,15 +141,12 @@ private function reflectParameters(\ReflectionFunctionAbstract $function, ?strin
name: $parameter->name,
class: $parameter->getDeclaringClass()?->name,
functionOrMethod: $functionOrMethod,
type: $this->reflectType($parameter->getType(), $class),
passedByReference: $parameter->isPassedByReference(),
defaultValueAvailable: $parameter->isDefaultValueAvailable(),
optional: $parameter->isOptional(),
variadic: $parameter->isVariadic(),
promoted: $parameter->isPromoted(),
deprecated: false,
type: $this->reflectType($parameter->getType(), $class),
startLine: false,
endLine: false,
attributes: $this->reflectAttributes($parameter->getAttributes()),
);
}
Expand Down Expand Up @@ -191,22 +181,14 @@ private function reflectAttributes(array $reflectionAttributes): array
*/
private function reflectType(?\ReflectionType $type, ?string $class): TypeMetadata
{
return TypeMetadata::create(
native: $this->reflectNativeType($type, $class),
phpDoc: null,
);
return TypeMetadata::create(native: $type === null ? null : $this->reflectNativeType($type, $class));
}

/**
* @param ?class-string $class
* @return ($reflectionType is null ? null : Type\Type)
*/
private function reflectNativeType(?\ReflectionType $reflectionType, ?string $class): ?Type\Type
private function reflectNativeType(\ReflectionType $reflectionType, ?string $class): Type\Type
{
if ($reflectionType === null) {
return null;
}

if ($reflectionType instanceof \ReflectionUnionType) {
return types::union(...array_map(
fn(\ReflectionType $child): Type\Type => $this->reflectNativeType($child, $class),
Expand Down
45 changes: 45 additions & 0 deletions src/PhpParserReflector/ClassReflections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Typhoon\Reflection\PhpParserReflector;

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Enum_;
use Typhoon\Reflection\ClassReflection;

final class ClassReflections
{
/**
* @psalm-suppress UnusedConstructor
*/
private function __construct() {}

/**
* @return int-mask-of<\ReflectionClass::IS_*>
*/
public static function modifiers(ClassLike $node): int
{
if ($node instanceof Enum_) {
return ClassReflection::IS_FINAL;
}

if (!$node instanceof Class_) {
return 0;
}

$modifiers = ($node->isAbstract() ? ClassReflection::IS_EXPLICIT_ABSTRACT : 0)
+ ($node->isFinal() ? ClassReflection::IS_FINAL : 0);

if (\PHP_VERSION_ID >= 80200 && $node->isReadonly()) {
/**
* @var int-mask-of<\ReflectionClass::IS_*>
* @psalm-suppress MixedOperand, UndefinedConstant, UnusedPsalmSuppress
*/
$modifiers += \ReflectionClass::IS_READONLY;
}

return $modifiers;
}
}
Loading

0 comments on commit 89284e7

Please sign in to comment.