From c41ddb90419c84bbb4db8e37af6a0354b4f4e3c7 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Sat, 7 Sep 2024 09:57:32 +0300 Subject: [PATCH] Detach DeclarationId --- .github/workflows/split-commits.yml | 1 - .github/workflows/split-releases.yml | 1 - composer.json | 3 +- src/DeclarationId/AliasId.php | 36 --- src/DeclarationId/AnonymousClassId.php | 150 ----------- src/DeclarationId/AnonymousFunctionId.php | 57 ----- src/DeclarationId/CHANGELOG.md | 6 - src/DeclarationId/ClassConstantId.php | 51 ---- src/DeclarationId/ConstantId.php | 34 --- src/DeclarationId/Id.php | 239 ------------------ src/DeclarationId/Internal/IdIsNotDefined.php | 19 -- src/DeclarationId/Internal/IdMap.php | 155 ------------ src/DeclarationId/LICENSE | 21 -- src/DeclarationId/MethodId.php | 51 ---- src/DeclarationId/NamedClassId.php | 40 --- src/DeclarationId/NamedFunctionId.php | 46 ---- src/DeclarationId/ParameterId.php | 54 ---- src/DeclarationId/PropertyId.php | 61 ----- src/DeclarationId/README.md | 6 - src/DeclarationId/TemplateId.php | 36 --- src/DeclarationId/composer.json | 24 -- tests/DeclarationId/DescribeTest.php | 58 ----- tests/DeclarationId/EncodeDecodeTest.php | 65 ----- .../Fixtures/anonymous_array_object_5_5.php | 6 - .../Fixtures/anonymous_class_3_12.php | 3 - tests/DeclarationId/IdClassFactoryTest.php | 71 ------ 26 files changed, 2 insertions(+), 1292 deletions(-) delete mode 100644 src/DeclarationId/AliasId.php delete mode 100644 src/DeclarationId/AnonymousClassId.php delete mode 100644 src/DeclarationId/AnonymousFunctionId.php delete mode 100644 src/DeclarationId/CHANGELOG.md delete mode 100644 src/DeclarationId/ClassConstantId.php delete mode 100644 src/DeclarationId/ConstantId.php delete mode 100644 src/DeclarationId/Id.php delete mode 100644 src/DeclarationId/Internal/IdIsNotDefined.php delete mode 100644 src/DeclarationId/Internal/IdMap.php delete mode 100644 src/DeclarationId/LICENSE delete mode 100644 src/DeclarationId/MethodId.php delete mode 100644 src/DeclarationId/NamedClassId.php delete mode 100644 src/DeclarationId/NamedFunctionId.php delete mode 100644 src/DeclarationId/ParameterId.php delete mode 100644 src/DeclarationId/PropertyId.php delete mode 100644 src/DeclarationId/README.md delete mode 100644 src/DeclarationId/TemplateId.php delete mode 100644 src/DeclarationId/composer.json delete mode 100644 tests/DeclarationId/DescribeTest.php delete mode 100644 tests/DeclarationId/EncodeDecodeTest.php delete mode 100644 tests/DeclarationId/Fixtures/anonymous_array_object_5_5.php delete mode 100644 tests/DeclarationId/Fixtures/anonymous_class_3_12.php delete mode 100644 tests/DeclarationId/IdClassFactoryTest.php diff --git a/.github/workflows/split-commits.yml b/.github/workflows/split-commits.yml index 2deca65c..6976239e 100644 --- a/.github/workflows/split-commits.yml +++ b/.github/workflows/split-commits.yml @@ -14,7 +14,6 @@ jobs: matrix: package: - { namespace: ChangeDetector, repo: change-detector } - - { namespace: DeclarationId, repo: declaration-id } - { namespace: PhpStormReflectionStubs, repo: php-storm-reflection-stubs } - { namespace: Reflection, repo: reflection } - { namespace: Type, repo: type } diff --git a/.github/workflows/split-releases.yml b/.github/workflows/split-releases.yml index 70905c7c..9ad978c4 100644 --- a/.github/workflows/split-releases.yml +++ b/.github/workflows/split-releases.yml @@ -17,7 +17,6 @@ jobs: matrix: package: - { namespace: ChangeDetector, repo: change-detector } - - { namespace: DeclarationId, repo: declaration-id } - { namespace: PhpStormReflectionStubs, repo: php-storm-reflection-stubs } - { namespace: Reflection, repo: reflection } - { namespace: Type, repo: type } diff --git a/composer.json b/composer.json index 8e7cdcca..e98f4f9c 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "phpstan/phpdoc-parser": "^1.21", "psr/simple-cache": "^3.0", "symfony/deprecation-contracts": "^3.0", - "symfony/polyfill-php84": "^1.30" + "symfony/polyfill-php84": "^1.30", + "typhoon/declaration-id": "^0.4.4" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.8.2", diff --git a/src/DeclarationId/AliasId.php b/src/DeclarationId/AliasId.php deleted file mode 100644 index 179e4cc0..00000000 --- a/src/DeclarationId/AliasId.php +++ /dev/null @@ -1,36 +0,0 @@ -name, $this->class->describe()); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->class->equals($this->class) - && $value->name === $this->name; - } - - public function jsonSerialize(): array - { - return [self::CODE_ALIAS, $this->class, $this->name]; - } -} diff --git a/src/DeclarationId/AnonymousClassId.php b/src/DeclarationId/AnonymousClassId.php deleted file mode 100644 index 57360bcd..00000000 --- a/src/DeclarationId/AnonymousClassId.php +++ /dev/null @@ -1,150 +0,0 @@ - : self) - */ - protected static function fromName(string $name): self - { - if (preg_match('/@anonymous\x00(.+):(\d+)/', $name, $matches) !== 1) { - throw new \InvalidArgumentException(\sprintf('Invalid anonymous class name "%s"', self::normalizeClassNameForException($name))); - } - - /** @var non-empty-string $file */ - $file = $matches[1]; - $line = (int) $matches[2]; - - if ($line <= 0) { - throw new \InvalidArgumentException(\sprintf('Invalid anonymous class name "%s"', self::normalizeClassNameForException($name))); - } - - if (class_exists($name, autoload: false)) { - /** @var self */ - return new self(file: $file, line: $line, name: $name); - } - - /** @var self */ - return new self(file: $file, line: $line); - } - - protected static function doFromReflection(\ReflectionClass $reflection): self - { - $file = $reflection->getFileName(); - \assert($file !== false, 'Anonymous class reflection should not return false file'); - - $line = $reflection->getStartLine(); - \assert($line !== false, 'Anonymous class reflection should not return false line'); - - return new self( - file: $file, - line: $line, - name: $reflection->name, - ); - } - - /** - * @param non-empty-string $file - * @param positive-int $line - * @param ?positive-int $column - * @param TName $name - */ - protected function __construct( - public readonly string $file, - public readonly int $line, - public readonly ?int $column = null, - public readonly ?string $name = null, - ) {} - - /** - * @param positive-int $column - * @return self - */ - public function withColumn(int $column): self - { - return new self( - file: $this->file, - line: $this->line, - column: $column, - name: $this->name, - ); - } - - /** - * @return self - */ - public function withoutColumn(): self - { - return new self( - file: $this->file, - line: $this->line, - name: $this->name, - ); - } - - public function describe(): string - { - return \sprintf('anonymous class at %s:%d%s', $this->file, $this->line, $this->column === null ? '' : ':' . $this->column); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->file === $this->file - && $value->line === $this->line - && $value->column === $this->column; - } - - public function reflect(): \ReflectionClass - { - if ($this->name === null) { - throw new \LogicException(\sprintf( - "Cannot reflect %s, because it's runtime name is not available", - $this->describe(), - )); - } - - return new \ReflectionClass($this->name); - } - - public function __serialize(): array - { - return [ - 'file' => $this->file, - 'line' => $this->line, - 'column' => $this->column, - 'name' => null, - ]; - } - - public function jsonSerialize(): array - { - $data = [self::CODE_ANONYMOUS_CLASS, $this->file, $this->line]; - - if ($this->column !== null) { - $data[] = $this->column; - } - - return $data; - } -} diff --git a/src/DeclarationId/AnonymousFunctionId.php b/src/DeclarationId/AnonymousFunctionId.php deleted file mode 100644 index be56e823..00000000 --- a/src/DeclarationId/AnonymousFunctionId.php +++ /dev/null @@ -1,57 +0,0 @@ -getFileName(); - \assert($file !== false, 'Anonymous function reflection should not return false file'); - - $line = $reflection->getStartLine(); - \assert($line !== false, 'Anonymous function reflection should not return false line'); - - return new self($file, $line); - } - - public function describe(): string - { - return \sprintf('anonymous function at %s:%d%s', $this->file, $this->line, $this->column === null ? '' : ':' . $this->column); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->file === $this->file - && $value->line === $this->line - && $value->column === $this->column; - } - - public function jsonSerialize(): array - { - $data = [self::CODE_ANONYMOUS_FUNCTION, $this->file, $this->line]; - - if ($this->column !== null) { - $data[] = $this->column; - } - - return $data; - } -} diff --git a/src/DeclarationId/CHANGELOG.md b/src/DeclarationId/CHANGELOG.md deleted file mode 100644 index 9e0e2bb2..00000000 --- a/src/DeclarationId/CHANGELOG.md +++ /dev/null @@ -1,6 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). diff --git a/src/DeclarationId/ClassConstantId.php b/src/DeclarationId/ClassConstantId.php deleted file mode 100644 index 5ccc4515..00000000 --- a/src/DeclarationId/ClassConstantId.php +++ /dev/null @@ -1,51 +0,0 @@ -class instanceof AnonymousClassId) { - return \sprintf('constant %s of %s', $this->name, $this->class->describe()); - } - - return \sprintf('constant %s::%s', $this->class->name, $this->name); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->class->equals($this->class) - && $value->name === $this->name; - } - - public function reflect(): \ReflectionClassConstant - { - $class = $this->class->name ?? throw new \LogicException(\sprintf( - "Cannot reflect %s, because it's runtime name is not available", - $this->describe(), - )); - - /** @psalm-suppress ArgumentTypeCoercion */ - return new \ReflectionClassConstant($class, $this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_CLASS_CONSTANT, $this->class, $this->name]; - } -} diff --git a/src/DeclarationId/ConstantId.php b/src/DeclarationId/ConstantId.php deleted file mode 100644 index db6f75e0..00000000 --- a/src/DeclarationId/ConstantId.php +++ /dev/null @@ -1,34 +0,0 @@ -name; - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->name === $this->name; - } - - public function jsonSerialize(): array - { - return [self::CODE_CONSTANT, $this->name]; - } -} diff --git a/src/DeclarationId/Id.php b/src/DeclarationId/Id.php deleted file mode 100644 index d546142e..00000000 --- a/src/DeclarationId/Id.php +++ /dev/null @@ -1,239 +0,0 @@ - - */ - final public static function namedFunction(string $name): NamedFunctionId - { - return new NamedFunctionId($name); - } - - /** - * @param non-empty-string $file - * @param positive-int $line - * @param ?positive-int $column - */ - final public static function anonymousFunction(string $file, int $line, ?int $column = null): AnonymousFunctionId - { - return new AnonymousFunctionId($file, $line, $column); - } - - /** - * @template TName of non-empty-string - * @param TName $name - * @return NamedClassId|(TName is class-string ? AnonymousClassId : AnonymousClassId) - * @psalm-suppress InvalidReturnType, InvalidReturnStatement - */ - final public static function class(string $name): NamedClassId|AnonymousClassId - { - if (str_contains($name, '@')) { - return AnonymousClassId::fromName($name); - } - - return new NamedClassId($name); - } - - /** - * @template TName of non-empty-string - * @param TName $name - * @return NamedClassId - */ - final public static function namedClass(string $name): NamedClassId - { - if (str_contains($name, '@')) { - throw new \InvalidArgumentException(\sprintf( - 'Cannot create NamedClassId from anonymous class name %s', - AnonymousClassId::normalizeClassNameForException($name), - )); - } - - return new NamedClassId($name); - } - - /** - * @param non-empty-string $file - * @param positive-int $line - * @param ?positive-int $column - * @return AnonymousClassId - */ - final public static function anonymousClass(string $file, int $line, ?int $column = null): AnonymousClassId - { - return new AnonymousClassId($file, $line, $column); - } - - /** - * @param non-empty-string|NamedClassId|AnonymousClassId $class - * @param non-empty-string $name - */ - final public static function classConstant(string|NamedClassId|AnonymousClassId $class, string $name): ClassConstantId - { - if (\is_string($class)) { - $class = self::class($class); - } - - return new ClassConstantId($class, $name); - } - - /** - * @param non-empty-string|NamedClassId|AnonymousClassId $class - * @param non-empty-string $name - */ - final public static function property(string|NamedClassId|AnonymousClassId $class, string $name): PropertyId - { - if (\is_string($class)) { - $class = self::class($class); - } - - return new PropertyId($class, $name); - } - - /** - * @param non-empty-string|NamedClassId|AnonymousClassId $class - * @param non-empty-string $name - */ - final public static function method(string|NamedClassId|AnonymousClassId $class, string $name): MethodId - { - if (\is_string($class)) { - $class = self::class($class); - } - - return new MethodId($class, $name); - } - - /** - * @param non-empty-string $name - */ - final public static function parameter(NamedFunctionId|AnonymousFunctionId|MethodId $function, string $name): ParameterId - { - return new ParameterId($function, $name); - } - - /** - * @param non-empty-string|NamedClassId|AnonymousClassId $class - * @param non-empty-string $name - */ - final public static function alias(string|NamedClassId|AnonymousClassId $class, string $name): AliasId - { - if (\is_string($class)) { - $class = self::class($class); - } - - return new AliasId($class, $name); - } - - /** - * @param non-empty-string $name - */ - final public static function template(NamedFunctionId|AnonymousFunctionId|NamedClassId|AnonymousClassId|MethodId $declaration, string $name): TemplateId - { - return new TemplateId($declaration, $name); - } - - /** - * @return ( - * $reflection is \ReflectionFunction ? NamedFunctionId|AnonymousFunctionId : - * $reflection is \ReflectionClass ? NamedClassId|AnonymousClassId : - * $reflection is \ReflectionClassConstant ? ClassConstantId : - * $reflection is \ReflectionProperty ? PropertyId : - * $reflection is \ReflectionMethod ? MethodId : - * $reflection is \ReflectionParameter ? ParameterId : never - * ) - */ - final public static function fromReflection(\ReflectionFunctionAbstract|\ReflectionClass|\ReflectionClassConstant|\ReflectionProperty|\ReflectionParameter $reflection): self - { - return match (true) { - $reflection instanceof \ReflectionFunction => $reflection->name === self::ANONYMOUS_CLOSURE_NAME ? AnonymousFunctionId::doFromReflection($reflection) : NamedFunctionId::doFromReflection($reflection), - $reflection instanceof \ReflectionClass => $reflection->isAnonymous() ? AnonymousClassId::doFromReflection($reflection) : new NamedClassId($reflection->name), - $reflection instanceof \ReflectionClassConstant => new ClassConstantId(self::fromReflection($reflection->getDeclaringClass()), $reflection->name), - $reflection instanceof \ReflectionProperty => PropertyId::doFromReflection($reflection), - $reflection instanceof \ReflectionMethod => new MethodId(self::fromReflection($reflection->getDeclaringClass()), $reflection->name), - $reflection instanceof \ReflectionParameter => new ParameterId(self::fromReflection($reflection->getDeclaringFunction()), $reflection->name), - }; - } - - final public static function decode(string $code): self - { - /** @var self */ - return self::doDecode(json_decode($code, associative: true, flags: JSON_THROW_ON_ERROR)); - } - - private static function doDecode(mixed $data): mixed - { - if (!\is_array($data)) { - return $data; - } - - $code = array_shift($data); - $args = array_map(self::doDecode(...), $data); - - /** @psalm-suppress MixedArgument, UnhandledMatchCondition */ - return match ($code) { - self::CODE_CONSTANT => new ConstantId(...$args), - self::CODE_NAMED_FUNCTION => new NamedFunctionId(...$args), - self::CODE_ANONYMOUS_FUNCTION => new AnonymousFunctionId(...$args), - self::CODE_NAMED_CLASS => new NamedClassId(...$args), - self::CODE_ANONYMOUS_CLASS => new AnonymousClassId(...$args), - self::CODE_ALIAS => new AliasId(...$args), - self::CODE_TEMPLATE => new TemplateId(...$args), - self::CODE_CLASS_CONSTANT => new ClassConstantId(...$args), - self::CODE_PROPERTY => new PropertyId(...$args), - self::CODE_METHOD => new MethodId(...$args), - self::CODE_PARAMETER => new ParameterId(...$args), - }; - } - - /** - * @return non-empty-string - */ - final public function __toString(): string - { - return $this->describe(); - } - - /** - * @return non-empty-string - */ - abstract public function describe(): string; - - /** - * @return non-empty-string - */ - final public function encode(): string - { - return json_encode($this, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); - } - - abstract public function equals(mixed $value): bool; -} diff --git a/src/DeclarationId/Internal/IdIsNotDefined.php b/src/DeclarationId/Internal/IdIsNotDefined.php deleted file mode 100644 index f9e34b28..00000000 --- a/src/DeclarationId/Internal/IdIsNotDefined.php +++ /dev/null @@ -1,19 +0,0 @@ -describe())); - } -} diff --git a/src/DeclarationId/Internal/IdMap.php b/src/DeclarationId/Internal/IdMap.php deleted file mode 100644 index e6419141..00000000 --- a/src/DeclarationId/Internal/IdMap.php +++ /dev/null @@ -1,155 +0,0 @@ - - * @implements \IteratorAggregate - */ -final class IdMap implements \ArrayAccess, \IteratorAggregate, \Countable -{ - /** - * @var array - */ - private array $values = []; - - /** - * @param iterable $values - */ - public function __construct(iterable $values = []) - { - foreach ($values as $id => $value) { - $this->values[$id->encode()] = [$id, $value]; - } - } - - /** - * @return list - */ - public function ids(): array - { - return array_column($this->values, 0); - } - - /** - * @template TNewId of Id - * @template TNewValue - * @param TNewId $id - * @param TNewValue $value - * @return self - */ - public function with(Id $id, mixed $value): self - { - /** @var self */ - $copy = clone $this; - $copy->values[$id->encode()] = [$id, $value]; - - return $copy; - } - - /** - * @param TId $id - * @return self - */ - public function without(Id $id): self - { - $copy = clone $this; - unset($copy->values[$id->encode()]); - - return $copy; - } - - /** - * @template TNewId of Id - * @template TNewValue - * @param IdMap $idMap - * @return self - */ - public function withMap(self $idMap): self - { - /** @var self */ - $copy = clone $this; - $copy->values = [...$copy->values, ...$idMap->values]; - - return $copy; - } - - public function offsetExists(mixed $offset): bool - { - return isset($this->values[$offset->encode()]); - } - - /** - * @param TId $offset - * @return TValue - */ - public function offsetGet(mixed $offset): mixed - { - return $this->values[$offset->encode()][1] ?? throw new IdIsNotDefined($offset); - } - - public function offsetSet(mixed $offset, mixed $value): void - { - throw new \BadMethodCallException(); - } - - public function offsetUnset(mixed $offset): void - { - throw new \BadMethodCallException(); - } - - /** - * @template TNewValue - * @param callable(TValue, TId): TNewValue $mapper - * @return self - */ - public function map(callable $mapper): self - { - $copy = clone $this; - - foreach ($copy->values as [$id, &$_value]) { - $_value = $mapper($_value, $id); - } - - /** @var self */ - return $copy; - } - - /** - * @return self - */ - public function slice(int $offset = 0, ?int $limit = null): self - { - $copy = clone $this; - $copy->values = \array_slice($this->values, $offset, $limit); - - return $copy; - } - - /** - * @return \Generator - */ - public function getIterator(): \Generator - { - foreach ($this->values as [$id, $value]) { - yield $id => $value; - } - } - - /** - * @return non-negative-int - */ - public function count(): int - { - return \count($this->values); - } -} diff --git a/src/DeclarationId/LICENSE b/src/DeclarationId/LICENSE deleted file mode 100644 index da18ad05..00000000 --- a/src/DeclarationId/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2024-present Valentin Udaltsov - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/src/DeclarationId/MethodId.php b/src/DeclarationId/MethodId.php deleted file mode 100644 index be2e22f7..00000000 --- a/src/DeclarationId/MethodId.php +++ /dev/null @@ -1,51 +0,0 @@ -class instanceof AnonymousClassId) { - return \sprintf('method %s of %s', $this->name, $this->class->describe()); - } - - return \sprintf('method %s::%s()', $this->class->name, $this->name); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->class->equals($this->class) - && $value->name === $this->name; - } - - public function reflect(): \ReflectionMethod - { - $class = $this->class->name ?? throw new \LogicException(\sprintf( - "Cannot reflect %s, because it's runtime name is not available", - $this->describe(), - )); - - /** @psalm-suppress ArgumentTypeCoercion */ - return new \ReflectionMethod($class, $this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_METHOD, $this->class, $this->name]; - } -} diff --git a/src/DeclarationId/NamedClassId.php b/src/DeclarationId/NamedClassId.php deleted file mode 100644 index 06adb144..00000000 --- a/src/DeclarationId/NamedClassId.php +++ /dev/null @@ -1,40 +0,0 @@ -name; - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->name === $this->name; - } - - public function reflect(): \ReflectionClass - { - return new \ReflectionClass($this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_NAMED_CLASS, $this->name]; - } -} diff --git a/src/DeclarationId/NamedFunctionId.php b/src/DeclarationId/NamedFunctionId.php deleted file mode 100644 index 6414c700..00000000 --- a/src/DeclarationId/NamedFunctionId.php +++ /dev/null @@ -1,46 +0,0 @@ -name); - } - - public function describe(): string - { - return \sprintf('function %s()', $this->name); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->name === $this->name; - } - - public function reflect(): \ReflectionFunction - { - /** @psalm-suppress ArgumentTypeCoercion */ - return new \ReflectionFunction($this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_NAMED_FUNCTION, $this->name]; - } -} diff --git a/src/DeclarationId/ParameterId.php b/src/DeclarationId/ParameterId.php deleted file mode 100644 index c7a0cf07..00000000 --- a/src/DeclarationId/ParameterId.php +++ /dev/null @@ -1,54 +0,0 @@ -name, $this->function->describe()); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->function->equals($this->function) - && $value->name === $this->name; - } - - public function reflect(): \ReflectionParameter - { - if ($this->function instanceof NamedFunctionId) { - return new \ReflectionParameter($this->function->name, $this->name); - } - - if ($this->function instanceof AnonymousFunctionId) { - throw new \LogicException(\sprintf('Cannot reflect %s', $this->describe())); - } - - $class = $this->function->class->name ?? throw new \LogicException(\sprintf( - "Cannot reflect %s, because it's runtime name is not available", - $this->describe(), - )); - - return new \ReflectionParameter([$class, $this->function->name], $this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_PARAMETER, $this->function, $this->name]; - } -} diff --git a/src/DeclarationId/PropertyId.php b/src/DeclarationId/PropertyId.php deleted file mode 100644 index 6ddc2173..00000000 --- a/src/DeclarationId/PropertyId.php +++ /dev/null @@ -1,61 +0,0 @@ -name === '') { - // https://github.com/vimeo/psalm/pull/10091#issuecomment-1670027553 - throw new \InvalidArgumentException('Dynamic property identification is not supported'); - } - - return new self(self::fromReflection($property->getDeclaringClass()), $property->name); - } - - public function describe(): string - { - if ($this->class instanceof AnonymousClassId) { - return \sprintf('property $%s of %s', $this->name, $this->class->describe()); - } - - return \sprintf('property %s::$%s', $this->class->name, $this->name); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->class->equals($this->class) - && $value->name === $this->name; - } - - public function reflect(): \ReflectionProperty - { - $class = $this->class->name ?? throw new \LogicException(\sprintf( - "Cannot reflect %s, because it's runtime name is not available", - $this->describe(), - )); - - /** @psalm-suppress ArgumentTypeCoercion */ - return new \ReflectionProperty($class, $this->name); - } - - public function jsonSerialize(): array - { - return [self::CODE_PROPERTY, $this->class, $this->name]; - } -} diff --git a/src/DeclarationId/README.md b/src/DeclarationId/README.md deleted file mode 100644 index 4eae2b30..00000000 --- a/src/DeclarationId/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Typhoon Declaration ID - -This repository is a readonly Typhoon monorepo subsplit. -Please, open pull requests and issues in the [main repository](https://github.com/typhoon-php/typhoon). - -Read [documentation](https://github.com/typhoon-php/typhoon/tree/0.4.x). diff --git a/src/DeclarationId/TemplateId.php b/src/DeclarationId/TemplateId.php deleted file mode 100644 index 74b72674..00000000 --- a/src/DeclarationId/TemplateId.php +++ /dev/null @@ -1,36 +0,0 @@ -name, $this->declaration->describe()); - } - - public function equals(mixed $value): bool - { - return $value instanceof self - && $value->declaration->equals($this->declaration) - && $value->name === $this->name; - } - - public function jsonSerialize(): array - { - return [self::CODE_TEMPLATE, $this->declaration, $this->name]; - } -} diff --git a/src/DeclarationId/composer.json b/src/DeclarationId/composer.json deleted file mode 100644 index 1dd1cada..00000000 --- a/src/DeclarationId/composer.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "typhoon/declaration-id", - "description": "Typhoon Declaration Id", - "license": "MIT", - "type": "library", - "authors": [ - { - "name": "Valentin Udaltsov", - "email": "udaltsov.valentin@gmail.com" - }, - { - "name": "Typhoon Team", - "homepage": "https://github.com/orgs/typhoon-php/people" - } - ], - "require": { - "php": "^8.1" - }, - "autoload": { - "psr-4": { - "Typhoon\\DeclarationId\\": "" - } - } -} diff --git a/tests/DeclarationId/DescribeTest.php b/tests/DeclarationId/DescribeTest.php deleted file mode 100644 index 16665341..00000000 --- a/tests/DeclarationId/DescribeTest.php +++ /dev/null @@ -1,58 +0,0 @@ - - */ - public static function ids(): \Generator - { - yield [Id::constant('PHP_INT_MAX'), 'constant PHP_INT_MAX']; - yield [Id::namedFunction('trim'), 'function trim()']; - yield [Id::anonymousFunction('/path/to/file', 10, 20), 'anonymous function at /path/to/file:10:20']; - yield [Id::anonymousFunction('/path/to/file', 10), 'anonymous function at /path/to/file:10']; - yield [Id::namedClass(\ArrayObject::class), 'class ArrayObject']; - yield [Id::anonymousClass('/path/to/file', 12), 'anonymous class at /path/to/file:12']; - yield [Id::anonymousClass('/path/to/file', 12, 33), 'anonymous class at /path/to/file:12:33']; - yield [Id::parameter(Id::namedFunction('trim'), 'a'), 'parameter $a of function trim()']; - yield [Id::parameter(Id::anonymousFunction('/path/to/file', 10, 23), 'a'), 'parameter $a of anonymous function at /path/to/file:10:23']; - yield [Id::parameter(Id::method(\ArrayObject::class, 'offsetExists'), 'a'), 'parameter $a of method ArrayObject::offsetExists()']; - yield [Id::property(\ArrayObject::class, 'prop'), 'property ArrayObject::$prop']; - yield [Id::property(Id::anonymousClass('/path/to/file', 12), 'prop'), 'property $prop of anonymous class at /path/to/file:12']; - yield [Id::method(\ArrayObject::class, 'offsetExists'), 'method ArrayObject::offsetExists()']; - yield [Id::method(Id::anonymousClass('/path/to/file', 12), 'offsetExists'), 'method offsetExists of anonymous class at /path/to/file:12']; - yield [Id::classConstant(\ArrayObject::class, 'ARRAY_AS_PROPS'), 'constant ArrayObject::ARRAY_AS_PROPS']; - yield [Id::classConstant(Id::anonymousClass('/path/to/file', 12), 'ARRAY_AS_PROPS'), 'constant ARRAY_AS_PROPS of anonymous class at /path/to/file:12']; - yield [Id::alias(\ArrayObject::class, 'Key'), 'type alias Key of class ArrayObject']; - yield [Id::alias(Id::anonymousClass('/path/to/file', 12), 'Key'), 'type alias Key of anonymous class at /path/to/file:12']; - yield [Id::template(Id::anonymousClass('/path/to/file', 12), 'T'), 'template T of anonymous class at /path/to/file:12']; - } - - #[DataProvider('ids')] - public function test(Id $id, string $expected): void - { - $encoded = $id->describe(); - - self::assertSame($expected, $encoded); - } -} diff --git a/tests/DeclarationId/EncodeDecodeTest.php b/tests/DeclarationId/EncodeDecodeTest.php deleted file mode 100644 index bdb37c24..00000000 --- a/tests/DeclarationId/EncodeDecodeTest.php +++ /dev/null @@ -1,65 +0,0 @@ - - */ - public static function ids(): \Generator - { - yield [Id::constant('CONST'), '["c","CONST"]']; - yield [Id::namedFunction('fn'), '["nf","fn"]']; - yield [Id::anonymousFunction('file', 10, 20), '["af","file",10,20]']; - yield [Id::anonymousFunction('file', 10), '["af","file",10]']; - yield [Id::namedClass('class'), '["nc","class"]']; - yield [Id::anonymousClass('file', 12), '["ac","file",12]']; - yield [Id::anonymousClass('file', 12, 33), '["ac","file",12,33]']; - yield [Id::parameter(Id::namedFunction('test'), 'a'), '["pa",["nf","test"],"a"]']; - yield [Id::parameter(Id::anonymousFunction('file', 10, 23), 'a'), '["pa",["af","file",10,23],"a"]']; - yield [Id::parameter(Id::method(\stdClass::class, 'test'), 'a'), '["pa",["m",["nc","stdClass"],"test"],"a"]']; - yield [Id::property('a-class', 'prop'), '["p",["nc","a-class"],"prop"]']; - yield [Id::method('a-class', 'method'), '["m",["nc","a-class"],"method"]']; - yield [Id::classConstant('a-class', 'const'), '["cc",["nc","a-class"],"const"]']; - yield [Id::alias('a-class', 'alias'), '["a",["nc","a-class"],"alias"]']; - yield [Id::alias(Id::anonymousClass('file', 12), 'alias'), '["a",["ac","file",12],"alias"]']; - yield [Id::template(Id::anonymousClass('file', 12), 'alias'), '["t",["ac","file",12],"alias"]']; - } - - #[DataProvider('ids')] - public function testItEncodesToExpectedValue(Id $id, string $expected): void - { - $encoded = $id->encode(); - - self::assertSame($expected, $encoded); - } - - #[DataProvider('ids')] - public function testItDecodesToSameValue(Id $id): void - { - $encoded = $id->encode(); - - $decoded = Id::decode($encoded); - - self::assertTrue($id->equals($decoded)); - } -} diff --git a/tests/DeclarationId/Fixtures/anonymous_array_object_5_5.php b/tests/DeclarationId/Fixtures/anonymous_array_object_5_5.php deleted file mode 100644 index 515bbfd0..00000000 --- a/tests/DeclarationId/Fixtures/anonymous_array_object_5_5.php +++ /dev/null @@ -1,6 +0,0 @@ - */ - class - extends ArrayObject {}; diff --git a/tests/DeclarationId/Fixtures/anonymous_class_3_12.php b/tests/DeclarationId/Fixtures/anonymous_class_3_12.php deleted file mode 100644 index 6cc7f2e2..00000000 --- a/tests/DeclarationId/Fixtures/anonymous_class_3_12.php +++ /dev/null @@ -1,3 +0,0 @@ -file); - self::assertSame($line, $classId->line); - self::assertNull($classId->column); - self::assertSame($name, $classId->name); - } - - /** - * @param non-empty-string $class - */ - #[TestWith([\stdClass::class])] - #[TestWith([\SplFileInfo::class])] - public function testItCreatesNamedClassIdFromNamedClassString(string $class): void - { - $classId = Id::class($class); - - self::assertInstanceOf(NamedClassId::class, $classId); - self::assertSame($class, $classId->name); - } - - public function staticAnalysisTestItInfersExistingClassName(): void - { - $id = Id::class(\stdClass::class); - /** @psalm-check-type-exact $id = NamedClassId<\stdClass::class>|AnonymousClassId<\stdClass::class> */ - $_name = $id->name; - /** @psalm-check-type-exact $_name = \stdClass::class */ - } - - public function staticAnalysisTestItInfersObjectClassName(): void - { - $object = new \ArrayObject(['a']); - $id = Id::class($object::class); - /** @psalm-check-type-exact $id = NamedClassId>>|AnonymousClassId>> */ - $_name = $id->name; - /** @psalm-check-type-exact $_name = class-string<\ArrayObject<0, 'a'>> */ - } - - public function staticAnalysisTestItInfersStringName(): void - { - $id = Id::class('someClass'); - /** @psalm-check-type-exact $id = NamedClassId<'someClass'>|AnonymousClassId */ - $_name = $id->name; - /** @psalm-check-type-exact $_name = ?'someClass' */ - } -}