Skip to content

Commit

Permalink
Рефакторинг с учётом обновлений typhoon/type
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 27, 2024
1 parent 73d7de0 commit e9ebe6d
Show file tree
Hide file tree
Showing 28 changed files with 617 additions and 1,138 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
"nikic/php-parser": "^4.15",
"phpstan/phpdoc-parser": "^1.0",
"psr/simple-cache": "^3.0",
"typhoon/type": "0.3.x-dev#8062f17",
"typhoon/type-stringifier": "0.3.x-dev#8d0f17c"
"typhoon/type": "^0.3@dev",
"typhoon/type-stringifier": "^0.3@dev"
},
"require-dev": {
"dragon-code/benchmark": "^2.6",
"ergebnis/composer-normalize": "^2.42.0",
"friendsofphp/php-cs-fixer": "^3.49.0",
"infection/infection": "^0.27.9",
"friendsofphp/php-cs-fixer": "^3.50.0",
"infection/infection": "^0.27.10",
"jetbrains/phpstorm-stubs": "^2023.3",
"mockery/mockery": "^1.6.7",
"phpunit/phpunit": "^10.5.10",
"phpunit/phpunit": "^10.5.11",
"phpyh/coding-standard": "^2.6.0",
"psalm/plugin-phpunit": "^0.18.4",
"symfony/var-dumper": "^6.4.2 || ^7.0.3",
"symfony/var-dumper": "^6.4.2 || ^7.0.4",
"typhoon/opcache": "^0.2.1",
"typhoon/psalm-plugin": "^0.1.0",
"vimeo/psalm": "^5.22.1"
"vimeo/psalm": "^5.22.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
<pluginClass class="Typhoon\PsalmPlugin\TyphoonPlugin"/>
<!--<pluginClass class="Typhoon\PsalmPlugin\TyphoonPlugin"/>-->
</plugins>

<projectFiles>
Expand Down
5 changes: 3 additions & 2 deletions src/AttributeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ final class AttributeReflection extends \ReflectionAttribute
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @param AttributeMetadata<TAttribute> $metadata
* @param class-string<TAttribute> $resolvedName
* @param \Closure(): list<\ReflectionAttribute> $nativeAttributesFactory
*/
public function __construct(
private readonly string $resolvedName,
private readonly AttributeMetadata $metadata,
private readonly \Closure $nativeAttributesFactory,
) {}
Expand All @@ -41,7 +42,7 @@ public function getArguments(): array
*/
public function getName(): string
{
return $this->metadata->name;
return $this->resolvedName;
}

public function getTarget(): int
Expand Down
14 changes: 10 additions & 4 deletions src/AttributeReflection/AttributeReflections.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Typhoon\Reflection\AttributeReflection;
use Typhoon\Reflection\ClassReflection\ClassReflector;
use Typhoon\Reflection\Exception\ClassDoesNotExistException;
use Typhoon\Reflection\Metadata\AttributeMetadata;

/**
Expand Down Expand Up @@ -35,10 +36,15 @@ public static function create(
return new self(
$classReflector,
array_map(
static fn(AttributeMetadata $attribute): AttributeReflection => new AttributeReflection(
$attribute,
$nativeAttributesFactory,
),
static function (AttributeMetadata $attribute) use ($classReflector, $nativeAttributesFactory): AttributeReflection {
$name = $attribute->name;

if (!$classReflector->classExists($name)) {
throw new ClassDoesNotExistException();
}

return new AttributeReflection($name, $attribute, $nativeAttributesFactory);
},
$attributes,
),
);
Expand Down
32 changes: 23 additions & 9 deletions src/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,21 @@ public function getTraitAliases(): array

public function getTraitNames(): array
{
/** @var list<trait-string> */
return $this->metadata->traitClasses();
$traitNames = [];

foreach ($this->yieldTraits() as $name => $_trait) {
$traitNames[] = $name;
}

return $traitNames;
}

/**
* @return array<trait-string, self>
*/
public function getTraits(): array
{
$traitNames = $this->getTraitNames();

return array_combine($traitNames, array_map($this->reflectClass(...), $traitNames));
return iterator_to_array($this->yieldTraits());
}

public function getTypeAlias(string $name): Type
Expand Down Expand Up @@ -594,9 +597,7 @@ private function loadNative(): void
}

/**
* @template TObject of object
* @param class-string<TObject> $class
* @return ClassReflection<TObject>
* @param non-empty-string $class
* @throws ReflectionException
*/
private function reflectClass(string $class): self
Expand All @@ -605,7 +606,7 @@ private function reflectClass(string $class): self
}

/**
* @param class-string $class
* @param non-empty-string $class
* @throws ReflectionException
*/
private function reflectClassMetadata(string $class): ClassMetadata
Expand Down Expand Up @@ -640,4 +641,17 @@ private function yieldInterfaces(): \Generator

return $interfaces;
}

/**
* @return \Generator<trait-string, self>
*/
private function yieldTraits(): \Generator
{
foreach ($this->metadata->traitClasses() as $traitClass) {
$trait = $this->reflectClass($traitClass);
/** @var trait-string */
$name = $trait->name;
yield $name => $trait;
}
}
}
7 changes: 3 additions & 4 deletions src/ClassReflection/ClassReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

use Typhoon\Reflection\ClassReflection;
use Typhoon\Reflection\ReflectionException;
use Typhoon\Reflection\TypeContext\ClassExistenceChecker;

/**
* @internal
* @psalm-internal Typhoon\Reflection
*/
interface ClassReflector
interface ClassReflector extends ClassExistenceChecker
{
/**
* @template T of object
* @param class-string<T> $name
* @return ClassReflection<T>
* @param non-empty-string $name
* @throws ReflectionException
*/
public function reflectClass(string $name): ClassReflection;
Expand Down
22 changes: 11 additions & 11 deletions src/Inheritance/MethodsInheritanceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Typhoon\Reflection\Inheritance;

use Typhoon\Reflection\Metadata\ClassMetadata;
use Typhoon\Reflection\Metadata\InheritedName;
use Typhoon\Reflection\Metadata\MethodMetadata;
use Typhoon\Reflection\TypeResolver\TemplateResolver;
use Typhoon\Type\NamedObjectType;

/**
* @internal
Expand Down Expand Up @@ -43,15 +43,15 @@ public function setOwn(array $methods): void
}

/**
* @param list<NamedObjectType> $types
* @param list<InheritedName> $inheritedNames
* @param TraitMethodAliases $traitMethodAliases
* @param TraitMethodPrecedence $traitMethodPrecedence
*/
public function addUsed(array $types, array $traitMethodAliases, array $traitMethodPrecedence): void
public function addUsed(array $inheritedNames, array $traitMethodAliases, array $traitMethodPrecedence): void
{
foreach (array_column($types, null, 'class') as $type) {
$trait = ($this->classMetadataReflector)($type->class);
$templateResolver = TemplateResolver::create($trait->templates, $type->templateArguments);
foreach (array_column($inheritedNames, null, 'class') as $inheritedName) {
$trait = ($this->classMetadataReflector)($inheritedName->class);
$templateResolver = TemplateResolver::create($trait->templates, $inheritedName->templateArguments);

foreach ($trait->resolvedMethods($this->classMetadataReflector) as $method) {
$name = $method->name;
Expand All @@ -70,13 +70,13 @@ public function addUsed(array $types, array $traitMethodAliases, array $traitMet
}

/**
* @param list<NamedObjectType> $types
* @param list<InheritedName> $inheritedNames
*/
public function addInherited(array $types): void
public function addInherited(array $inheritedNames): void
{
foreach ($types as $type) {
$class = ($this->classMetadataReflector)($type->class);
$templateResolver = TemplateResolver::create($class->templates, $type->templateArguments);
foreach ($inheritedNames as $inheritedName) {
$class = ($this->classMetadataReflector)($inheritedName->class);
$templateResolver = TemplateResolver::create($class->templates, $inheritedName->templateArguments);

foreach ($class->resolvedMethods($this->classMetadataReflector) as $method) {
$this->method($method->name)->addInherited($method, $templateResolver);
Expand Down
18 changes: 9 additions & 9 deletions src/Inheritance/PropertiesInheritanceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Typhoon\Reflection\Inheritance;

use Typhoon\Reflection\Metadata\ClassMetadata;
use Typhoon\Reflection\Metadata\InheritedName;
use Typhoon\Reflection\Metadata\PropertyMetadata;
use Typhoon\Reflection\TypeResolver\TemplateResolver;
use Typhoon\Type\NamedObjectType;

/**
* @internal
Expand Down Expand Up @@ -40,23 +40,23 @@ public function setOwn(iterable $properties): void
}
}

public function addUsed(NamedObjectType ...$types): void
public function addUsed(InheritedName ...$names): void
{
foreach ($types as $type) {
$class = ($this->classMetadataReflector)($type->class);
$templateResolver = TemplateResolver::create($class->templates, $type->templateArguments);
foreach ($names as $name) {
$class = ($this->classMetadataReflector)($name->class);
$templateResolver = TemplateResolver::create($class->templates, $name->templateArguments);

foreach ($class->resolvedProperties($this->classMetadataReflector) as $property) {
$this->property($property->name)->addUsed($property, $templateResolver);
}
}
}

public function addInherited(NamedObjectType ...$types): void
public function addInherited(InheritedName ...$names): void
{
foreach ($types as $type) {
$class = ($this->classMetadataReflector)($type->class);
$templateResolver = TemplateResolver::create($class->templates, $type->templateArguments);
foreach ($names as $name) {
$class = ($this->classMetadataReflector)($name->class);
$templateResolver = TemplateResolver::create($class->templates, $name->templateArguments);

foreach ($class->resolvedProperties($this->classMetadataReflector) as $property) {
$this->property($property->name)->addInherited($property, $templateResolver);
Expand Down
3 changes: 1 addition & 2 deletions src/Metadata/AttributeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
* @template TAttribute of object
*/
final class AttributeMetadata
{
/**
* @param class-string<TAttribute> $name
* @param non-empty-string $name
* @param non-negative-int $position
* @param \Attribute::TARGET_* $target
*/
Expand Down
19 changes: 9 additions & 10 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
use Typhoon\Reflection\Inheritance\MethodsInheritanceResolver;
use Typhoon\Reflection\Inheritance\PropertiesInheritanceResolver;
use Typhoon\Reflection\TemplateReflection;
use Typhoon\Type\NamedObjectType;
use Typhoon\Type\Type;

/**
* @internal
* @psalm-internal Typhoon\Reflection
* @template-covariant T of object
* @template-extends RootMetadata<class-string<T>>
* @psalm-type ClassMetadataReflector = \Closure(class-string): ClassMetadata
* @psalm-type TraitMethodAliases = array<class-string, non-empty-array<non-empty-string, list<TraitMethodAlias>>>
* @psalm-type TraitMethodPrecedence = array<non-empty-string, class-string>
* @psalm-type ClassMetadataReflector = \Closure(non-empty-string): ClassMetadata
* @psalm-type TraitMethodAliases = array<non-empty-string, non-empty-array<non-empty-string, list<TraitMethodAlias>>>
* @psalm-type TraitMethodPrecedence = array<non-empty-string, non-empty-string>
*/
final class ClassMetadata extends RootMetadata
{
Expand All @@ -43,8 +42,8 @@ final class ClassMetadata extends RootMetadata
* @param list<AttributeMetadata> $attributes
* @param array<non-empty-string, Type> $typeAliases
* @param list<TemplateReflection> $templates
* @param list<NamedObjectType> $interfaceTypes
* @param list<NamedObjectType> $traitTypes
* @param list<InheritedName> $interfaceTypes
* @param list<InheritedName> $traitTypes
* @param TraitMethodAliases $traitMethodAliases
* @param TraitMethodPrecedence $traitMethodPrecedence
* @param list<PropertyMetadata> $ownProperties
Expand All @@ -68,7 +67,7 @@ public function __construct(
public readonly bool $trait = false,
public readonly bool $anonymous = false,
public readonly bool $deprecated = false,
public readonly ?NamedObjectType $parentType = null,
public readonly ?InheritedName $parentType = null,
public readonly array $interfaceTypes = [],
public readonly array $traitTypes = [],
public readonly array $traitMethodAliases = [],
Expand All @@ -82,23 +81,23 @@ public function __construct(
}

/**
* @return ?class-string
* @return ?non-empty-string
*/
public function parentClass(): ?string
{
return $this->parentType?->class;
}

/**
* @return list<class-string>
* @return list<non-empty-string>
*/
public function interfaceClasses(): array
{
return array_column($this->interfaceTypes, 'class');
}

/**
* @return list<class-string>
* @return list<non-empty-string>
*/
public function traitClasses(): array
{
Expand Down
19 changes: 19 additions & 0 deletions src/Metadata/InheritedName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Typhoon\Reflection\Metadata;

use Typhoon\Type\Type;

final class InheritedName
{
/**
* @param non-empty-string $class
* @param list<Type> $templateArguments
*/
public function __construct(
public readonly string $class,
public readonly array $templateArguments = [],
) {}
}
8 changes: 8 additions & 0 deletions src/NameContext/FullyQualifiedName.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public function __construct(
private readonly UnqualifiedName|QualifiedName $name,
) {}

/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function lastSegment(): UnqualifiedName
{
return $this->name->lastSegment();
}

public function resolve(): UnqualifiedName|QualifiedName
{
return $this->name;
Expand Down
Loading

0 comments on commit e9ebe6d

Please sign in to comment.