Skip to content

Commit

Permalink
feat: Added plan for types
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykbaszak committed Aug 1, 2024
1 parent 358718b commit 1eb89b6
Show file tree
Hide file tree
Showing 25 changed files with 879 additions and 218 deletions.
2 changes: 0 additions & 2 deletions src/Blueprint/Domain/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ private function __construct(
}

/**
* @param BlueprintId $id
* @param array<class-string, ClassBlueprint> $classBlueprints
*/
public static function create(BlueprintId $id, array $classBlueprints): static
Expand All @@ -33,7 +32,6 @@ public static function create(BlueprintId $id, array $classBlueprints): static
}

/**
* @param BlueprintId $id
* @param array<class-string, ClassBlueprint> $classBlueprints
*/
public static function recreate(BlueprintId $id, array $classBlueprints): static
Expand Down
2 changes: 0 additions & 2 deletions src/Blueprint/Domain/Entities/ClassBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ private function __construct(
}

/**
* @param BlueprintId $id
* @param array<string, PropertyBlueprint> $propertyBlueprints
*/
public static function create(BlueprintId $id, array $propertyBlueprints): static
Expand All @@ -33,7 +32,6 @@ public static function create(BlueprintId $id, array $propertyBlueprints): stati
}

/**
* @param BlueprintId $id
* @param array<string, PropertyBlueprint> $propertyBlueprints
*/
public static function recreate(BlueprintId $id, array $propertyBlueprints): static
Expand Down
36 changes: 7 additions & 29 deletions src/Reflection/Domain/Entities/AttributeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use PBaszak\UltraMapper\Reflection\Domain\Events\ReflectionCreated;
use PBaszak\UltraMapper\Reflection\Domain\Exception\ReflectionException;
use PBaszak\UltraMapper\Reflection\Domain\Identity\ReflectionId;
use PBaszak\UltraMapper\Shared\Domain\ObjectTypes\AggregateRoot;
use PBaszak\UltraMapper\Shared\Domain\ObjectTypes\Entity;
use PBaszak\UltraMapper\Shared\Infrastructure\Normalization\Normalizable;

final class AttributeReflection extends AggregateRoot implements Normalizable, ReflectionInterface
final class AttributeReflection extends Entity implements Normalizable, ReflectionInterface
{
private ReflectionInterface&AttributesSupport $parent;

Expand All @@ -26,7 +26,6 @@ private function __construct(
private array $arguments,
private false|string $fileName,
private false|string $fileHash,
private false|string $docBlock,
) {
}

Expand All @@ -44,7 +43,6 @@ public static function create(
arguments: $reflectionAttribute->getArguments(),
fileName: $reflectionClass->getFileName(),
fileHash: $reflectionClass->getFileName() ? md5_file($reflectionClass->getFileName()) : false,
docBlock: $reflectionClass->getDocComment(),
);

$instance->parent = $parent;
Expand All @@ -58,14 +56,7 @@ public static function create(
}

/**
* @param ReflectionId $id
* @param string $name
* @param string $shortName
* @param string $namespace
* @param array<string|int, mixed> $arguments
* @param false|string $fileName
* @param false|string $fileHash
* @param false|string $docBlock
*/
public static function recreate(
ReflectionId $id,
Expand All @@ -75,7 +66,6 @@ public static function recreate(
array $arguments,
false|string $fileName,
false|string $fileHash,
false|string $docBlock,
): static {
return new static(
id: $id,
Expand All @@ -85,11 +75,10 @@ public static function recreate(
arguments: $arguments,
fileName: $fileName,
fileHash: $fileHash,
docBlock: $docBlock,
);
}

public function parent(null|(ReflectionInterface&AttributesSupport) $parent = null): ReflectionInterface&AttributesSupport
public function parent((ReflectionInterface&AttributesSupport)|null $parent = null): ReflectionInterface&AttributesSupport
{
if (!$parent) {
return $this->parent;
Expand All @@ -101,7 +90,7 @@ public function parent(null|(ReflectionInterface&AttributesSupport) $parent = nu
return $this->parent;
}

throw new \InvalidArgumentException("Cannot set parent property. Parent property is read-only.");
throw new \InvalidArgumentException('Cannot set parent property. Parent property is read-only.');
}

public function id(): ReflectionId
Expand Down Expand Up @@ -140,11 +129,6 @@ public function fileHash(): false|string
return $this->fileHash;
}

public function docBlock(): false|string
{
return $this->docBlock;
}

public function instance(): object
{
return new $this->name(...$this->arguments);
Expand All @@ -154,8 +138,8 @@ public function reflection(): \Reflector
{
if (method_exists($parentReflection = $this->parent->reflection(), 'getAttributes')) {
/**
* @var \ReflectionClass|\ReflectionMethod|\ReflectionProperty|\ReflectionParameter $parentReflection
* @var \ReflectionAttribute[] $attrs
* @var \ReflectionClass|\ReflectionMethod|\ReflectionProperty|\ReflectionParameter $parentReflection
* @var \ReflectionAttribute[] $attrs
*/
$attrs = $parentReflection->getAttributes($this->name);
foreach ($attrs as $attr) {
Expand All @@ -165,11 +149,7 @@ public function reflection(): \Reflector
}
}

throw new ReflectionException(
"ReflectionAttribute for `{$this->name}` in `{$parentReflection->__toString()}` class not found.",
"Check if the attribute is still present in the class and if the arguments match.",
5
);
throw new ReflectionException("ReflectionAttribute for `{$this->name}` in `{$parentReflection->__toString()}` class not found.", 'Check if the attribute is still present in the class and if the arguments match.', 5);
}

public function normalize(): array
Expand All @@ -182,7 +162,6 @@ public function normalize(): array
'arguments' => $this->arguments,
'fileName' => $this->fileName,
'fileHash' => $this->fileHash,
'docBlock' => $this->docBlock,
];
}

Expand All @@ -196,7 +175,6 @@ public static function denormalize(array $data): static
$data['arguments'],
$data['fileName'],
$data['fileHash'],
$data['docBlock'],
);
}
}
60 changes: 14 additions & 46 deletions src/Reflection/Domain/Entities/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

use PBaszak\UltraMapper\Reflection\Domain\Entities\Interfaces\AttributesSupport;
use PBaszak\UltraMapper\Reflection\Domain\Entities\Interfaces\ReflectionInterface;
use PBaszak\UltraMapper\Reflection\Domain\Entities\traits\Attributes;
use PBaszak\UltraMapper\Reflection\Domain\Entities\traits\Methods;
use PBaszak\UltraMapper\Reflection\Domain\Entities\traits\Properties;
use PBaszak\UltraMapper\Reflection\Domain\Events\ReflectionAdded;
use PBaszak\UltraMapper\Reflection\Domain\Entities\Traits\Attributes;
use PBaszak\UltraMapper\Reflection\Domain\Entities\Traits\Methods;
use PBaszak\UltraMapper\Reflection\Domain\Entities\Traits\Properties;
use PBaszak\UltraMapper\Reflection\Domain\Events\ReflectionCreated;
use PBaszak\UltraMapper\Reflection\Domain\Events\ReflectionRemoved;
use PBaszak\UltraMapper\Reflection\Domain\Exception\ReflectionException;
use PBaszak\UltraMapper\Reflection\Domain\Identity\ReflectionId;
use PBaszak\UltraMapper\Reflection\Domain\Reflection;
use PBaszak\UltraMapper\Shared\Domain\ObjectTypes\AggregateRoot;
use PBaszak\UltraMapper\Shared\Domain\ObjectTypes\Entity;
use PBaszak\UltraMapper\Shared\Infrastructure\Normalization\Normalizable;

final class ClassReflection extends AggregateRoot implements Normalizable, AttributesSupport, ReflectionInterface
final class ClassReflection extends Entity implements Normalizable, AttributesSupport, ReflectionInterface
{
use Attributes, Methods, Properties;
use Attributes;
use Methods;
use Properties;

private Reflection $root;

Expand All @@ -33,7 +33,6 @@ private function __construct(
private string $hash,
private false|string $fileName,
private false|string $fileHash,
private false|string $docBlock,
array $attributes = [],
array $properties = [],
array $methods = [],
Expand All @@ -45,33 +44,21 @@ private function __construct(

/**
* @param string|class-string $name
* @param Reflection $root
* @param null|PropertyReflection $parentProperty
*
* @return static
*/
public static function create(
string $name,
Reflection $root,
?PropertyReflection $parentProperty = null,
): static {
if (__CLASS__ === $name) {
throw new ReflectionException(
"Cannot create instance of $name class.",
"Please do not use `ClassReflection::create` method to create instance of $name class.",
1
);
throw new ReflectionException("Cannot create instance of $name class.", "Please do not use `ClassReflection::create` method to create instance of $name class.", 1);
}

try {
$reflection = new \ReflectionClass($name);
} catch (\ReflectionException $e) {
$message = $e->getMessage();
throw new ReflectionException(
"Class `$name` not found. $message",
"Check if the class exists, has correct namespace and filename, and is properly autoloaded.",
2
);
throw new ReflectionException("Class `$name` not found. $message", 'Check if the class exists, has correct namespace and filename, and is properly autoloaded.', 2);
}

$instance = new static(
Expand All @@ -82,7 +69,6 @@ public static function create(
hash: md5($reflection->__toString()),
fileName: $reflection->getFileName(),
fileHash: $reflection->getFileName() ? md5_file($reflection->getFileName()) : false,
docBlock: $reflection->getDocComment(),
);

$instance->root = $root;
Expand Down Expand Up @@ -110,19 +96,10 @@ public static function create(
}

/**
* @param ReflectionId $id
* @param string|class-string $name
* @param string $shortName
* @param string $namespace
* @param string $hash
* @param false|string $fileName
* @param false|string $fileHash
* @param false|string $docBlock
* @param string|class-string $name
* @param array<class-string, AttributeReflection[]> $attributes
* @param array<string, PropertyReflection> $properties
* @param array<string, MethodReflection> $methods
*
* @return static
* @param array<string, PropertyReflection> $properties
* @param array<string, MethodReflection> $methods
*/
public static function recreate(
ReflectionId $id,
Expand All @@ -132,7 +109,6 @@ public static function recreate(
string $hash,
false|string $fileName,
false|string $fileHash,
false|string $docBlock,
array $attributes,
array $properties,
array $methods
Expand All @@ -145,7 +121,6 @@ public static function recreate(
$hash,
$fileName,
$fileHash,
$docBlock,
$attributes,
$properties,
$methods
Expand All @@ -164,7 +139,7 @@ public function root(?Reflection $root = null): Reflection
return $this->root;
}

throw new \InvalidArgumentException("Cannot set root property. Root property is read-only.");
throw new \InvalidArgumentException('Cannot set root property. Root property is read-only.');
}

public function id(): ReflectionId
Expand Down Expand Up @@ -202,11 +177,6 @@ public function fileHash(): false|string
return $this->fileHash;
}

public function docBlock(): false|string
{
return $this->docBlock;
}

public function reflection(): \Reflector
{
return new \ReflectionClass($this->name);
Expand All @@ -222,7 +192,6 @@ public function normalize(): array
'hash' => $this->hash,
'fileName' => $this->fileName,
'fileHash' => $this->fileHash,
'docBlock' => $this->docBlock,
'attributes' => $this->normalizeAttributes(),
'properties' => $this->normalizeProperties(),
'methods' => $this->normalizeMethods(),
Expand All @@ -239,7 +208,6 @@ public static function denormalize(array $data): static
$data['hash'],
$data['fileName'],
$data['fileHash'],
$data['docBlock'],
static::denormalizeAttributes($data['attributes']),
static::denormalizeProperties($data['properties']),
static::denormalizeMethods($data['methods']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
interface AttributesSupport
{
/**
* @param null|class-string $filter
*
* @param class-string|null $filter
*
* @return array<class-string, AttributeReflection[]>|AttributeReflection[]
*/
public function attributes(null|string $filter = null): array;
public function attributes(?string $filter = null): array;

public function addAttribute(AttributeReflection $attribute): void;

Expand Down
Loading

0 comments on commit 1eb89b6

Please sign in to comment.