Skip to content

Commit

Permalink
Runtime meta: replace getters with readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Feb 16, 2025
1 parent 7e9cb4e commit b7c20be
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 253 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Simplify finding missing fields (performance optimization)
- Reduce calls needed to find field names for "did you mean" error helper
- Runtime meta
- replace getters with readonly properties

### Fixed

Expand Down
10 changes: 5 additions & 5 deletions src/Meta/MetaResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function resolve(ReflectionClass $class, CompileMeta $meta): RuntimeMeta
$this->resolveFieldsMeta($class, $meta),
);

$this->checkObjectCanBeInstantiated($class, $runtimeMeta->getClass());
$this->checkObjectCanBeInstantiated($class, $runtimeMeta->class);

return $runtimeMeta;
}
Expand Down Expand Up @@ -177,7 +177,7 @@ private function checkObjectCanBeInstantiated(ReflectionClass $class, ClassRunti
{
$injectors = [];
foreach ($meta->getModifier(RequiresDependenciesModifier::class) as $modifier) {
$injectors[] = $modifier->getArgs()->injector;
$injectors[] = $modifier->args->injector;
}

$this->objectCreator->createInstance($class->getName(), $injectors);
Expand Down Expand Up @@ -211,10 +211,10 @@ private function propertyNameToFieldName(FieldRuntimeMeta $fieldMeta)
{
$modifier = $fieldMeta->getModifier(FieldNameModifier::class);
if ($modifier !== null) {
return $modifier->getArgs()->name;
return $modifier->args->name;
}

return $fieldMeta->getProperty()->getName();
return $fieldMeta->property->getName();
}

/**
Expand Down Expand Up @@ -314,7 +314,7 @@ private function resolveCallbacksMeta(
$classReflector,
);

$array[$callbackMeta->getType()][$key] = $callbackMeta;
$array[$callbackMeta->type][$key] = $callbackMeta;
}

return $array;
Expand Down
32 changes: 5 additions & 27 deletions src/Meta/Runtime/CallbackRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
* @phpstan-type T_SERIALIZED array{type: class-string<Callback<T>>, args: T, declaringClass: class-string<MappedObject>}
*
* @template-covariant T of Args
*
* @readonly
*/
final class CallbackRuntimeMeta
{

/** @var class-string<Callback<T>> */
private string $type;
public string $type;

/** @var T */
private Args $args;
public Args $args;

/** @var ReflectionClass<covariant MappedObject> */
private ReflectionClass $declaringClass;
public ReflectionClass $declaringClass;

/**
* @param class-string<Callback<T>> $type
Expand All @@ -36,30 +38,6 @@ public function __construct(string $type, Args $args, ReflectionClass $declaring
$this->declaringClass = $declaringClass;
}

/**
* @return class-string<Callback<T>>
*/
public function getType(): string
{
return $this->type;
}

/**
* @return T
*/
public function getArgs(): Args
{
return $this->args;
}

/**
* @return ReflectionClass<covariant MappedObject>
*/
public function getDeclaringClass(): ReflectionClass
{
return $this->declaringClass;
}

/**
* @return T_SERIALIZED
*/
Expand Down
15 changes: 4 additions & 11 deletions src/Meta/Runtime/ClassRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,26 @@ final class ClassRuntimeMeta extends NodeRuntimeMeta
{

/** @var array<class-string<Modifier<Args>>, list<ModifierRuntimeMeta<Args>>> */
private array $modifiers;
public array $modifiers;

/**
* @param array<class-string<Modifier<Args>>, list<ModifierRuntimeMeta<Args>>> $modifiers
* @template T_ARGS of Args
* @param array<class-string<Modifier<T_ARGS>>, list<ModifierRuntimeMeta<T_ARGS>>> $modifiers
*/
public function __construct(array $callbacks, array $docs, array $modifiers)
{
parent::__construct($callbacks, $docs);
$this->modifiers = $modifiers;
}

/**
* @return array<class-string<Modifier<Args>>, list<ModifierRuntimeMeta<Args>>>
*/
public function getModifiers(): array
{
return $this->modifiers;
}

/**
* @template T of Args
* @param class-string<Modifier<T>> $type
* @return list<ModifierRuntimeMeta<T>>
*/
public function getModifier(string $type): array
{
return $this->getModifiers()[$type] ?? [];
return $this->modifiers[$type] ?? [];
}

/**
Expand Down
39 changes: 7 additions & 32 deletions src/Meta/Runtime/FieldRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ final class FieldRuntimeMeta extends NodeRuntimeMeta
{

/** @var RuleRuntimeMeta<Args> */
private RuleRuntimeMeta $rule;
public RuleRuntimeMeta $rule;

private DefaultValueMeta $default;
public DefaultValueMeta $default;

private ReflectionProperty $property;
public ReflectionProperty $property;

/** @var array<class-string<Modifier<Args>>, ModifierRuntimeMeta<Args>> */
private array $modifiers;
public array $modifiers;

/**
* @param array<class-string<Modifier<Args>>, ModifierRuntimeMeta<Args>> $modifiers
* @template T_ARGS of Args
* @param array<class-string<Modifier<T_ARGS>>, ModifierRuntimeMeta<T_ARGS>> $modifiers
* @param RuleRuntimeMeta<Args> $rule
*/
public function __construct(
Expand All @@ -40,40 +41,14 @@ public function __construct(
$this->modifiers = $modifiers;
}

/**
* @return RuleRuntimeMeta<Args>
*/
public function getRule(): RuleRuntimeMeta
{
return $this->rule;
}

public function getDefault(): DefaultValueMeta
{
return $this->default;
}

public function getProperty(): ReflectionProperty
{
return $this->property;
}

/**
* @return array<class-string<Modifier<Args>>, ModifierRuntimeMeta<Args>>
*/
public function getModifiers(): array
{
return $this->modifiers;
}

/**
* @template T of Args
* @param class-string<Modifier<T>> $type
* @return ModifierRuntimeMeta<T>|null
*/
public function getModifier(string $type): ?ModifierRuntimeMeta
{
return $this->getModifiers()[$type] ?? null;
return $this->modifiers[$type] ?? null;
}

/**
Expand Down
22 changes: 4 additions & 18 deletions src/Meta/Runtime/ModifierRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

/**
* @template-covariant T of Args
*
* @readonly
*/
final class ModifierRuntimeMeta
{

/** @var class-string<Modifier<T>> */
private string $type;
public string $type;

/** @var T */
private Args $args;
public Args $args;

/**
* @param class-string<Modifier<T>> $type
Expand All @@ -27,22 +29,6 @@ public function __construct(string $type, Args $args)
$this->args = $args;
}

/**
* @return class-string<Modifier<T>>
*/
public function getType(): string
{
return $this->type;
}

/**
* @return T
*/
public function getArgs(): Args
{
return $this->args;
}

/**
* @return array<mixed>
*/
Expand Down
27 changes: 4 additions & 23 deletions src/Meta/Runtime/NodeRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
use Orisai\ObjectMapper\Meta\Shared\DocMeta;

/**
* @readonly
*
* @internal
*/
abstract class NodeRuntimeMeta
{

/** @var array<class-string<Callback<Args>>, array<int, CallbackRuntimeMeta<Args>>> */
private array $callbacks;
public array $callbacks;

/** @var array<string, DocMeta> */
private array $docs;
public array $docs;

/**
* @template T_ARGS of Args
Expand All @@ -29,19 +31,6 @@ public function __construct(array $callbacks, array $docs)
$this->docs = $docs;
}

public function hasAnyCallbacks(): bool
{
return $this->callbacks !== [];
}

/**
* @return array<class-string<Callback<Args>>, array<int, CallbackRuntimeMeta<Args>>>
*/
public function getCallbacks(): array
{
return $this->callbacks;
}

/**
* @return array<int, CallbackRuntimeMeta<Args>>
*/
Expand All @@ -50,14 +39,6 @@ public function getCallbacksByType(string $type): array
return $this->callbacks[$type] ?? [];
}

/**
* @return array<string, DocMeta>
*/
public function getDocs(): array
{
return $this->docs;
}

/**
* @return array<mixed>
*/
Expand Down
22 changes: 4 additions & 18 deletions src/Meta/Runtime/RuleRuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

/**
* @template-covariant T of Args
*
* @readonly
*/
final class RuleRuntimeMeta
{

/** @var class-string<Rule<T>> */
private string $type;
public string $type;

/** @var T */
private Args $args;
public Args $args;

/**
* @param class-string<Rule<T>> $type
Expand All @@ -27,22 +29,6 @@ public function __construct(string $type, Args $args)
$this->args = $args;
}

/**
* @return class-string<Rule<T>>
*/
public function getType(): string
{
return $this->type;
}

/**
* @return T
*/
public function getArgs(): Args
{
return $this->args;
}

/**
* @return array<mixed>
*/
Expand Down
19 changes: 4 additions & 15 deletions src/Meta/Runtime/RuntimeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

/**
* Value object for meta returned from MetaLoader
*
* @readonly
*/
final class RuntimeMeta
{

private ClassRuntimeMeta $class;
public ClassRuntimeMeta $class;

/** @var array<int|string, FieldRuntimeMeta> */
private array $fields;
public array $fields;

/**
* @param array<int|string, FieldRuntimeMeta> $fields
Expand All @@ -22,19 +24,6 @@ public function __construct(ClassRuntimeMeta $class, array $fields)
$this->fields = $fields;
}

public function getClass(): ClassRuntimeMeta
{
return $this->class;
}

/**
* @return array<int|string, FieldRuntimeMeta>
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @return array<mixed>
*/
Expand Down
Loading

0 comments on commit b7c20be

Please sign in to comment.