Skip to content

Commit 30d985f

Browse files
committed
fix: apply linter suggestions
1 parent 1fe6ee0 commit 30d985f

8 files changed

Lines changed: 58 additions & 26 deletions

File tree

src/Core/Reflect/Resolver/RequirementResolver.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,7 @@ private function applyRequirementRule(ReflectionParameter $parameter, Field $fie
5757
return;
5858
}
5959

60-
if ($this->shouldBePresent($parameter)) {
61-
$field->present();
62-
return;
63-
}
64-
65-
if ($this->shouldBeFilled($parameter)) {
66-
$field->filled();
67-
}
68-
69-
if ($this->shouldUseSometimesRequired($parameter)) {
70-
$field->required();
71-
}
60+
$this->applyRequirementRuleNotStrict($parameter, $field);
7261
}
7362

7463
private function isStrictlyRequired(ReflectionParameter $parameter): bool
@@ -98,4 +87,20 @@ private function shouldUseSometimesRequired(ReflectionParameter $parameter): boo
9887
$parameter->isDefaultValueAvailable() &&
9988
! $parameter->allowsNull();
10089
}
90+
91+
private function applyRequirementRuleNotStrict(ReflectionParameter $parameter, Field $field): void
92+
{
93+
if ($this->shouldBePresent($parameter)) {
94+
$field->present();
95+
return;
96+
}
97+
98+
if ($this->shouldBeFilled($parameter)) {
99+
$field->filled();
100+
}
101+
102+
if ($this->shouldUseSometimesRequired($parameter)) {
103+
$field->required();
104+
}
105+
}
101106
}

src/Core/Serialize/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Builder extends Engine
3030
* @return T
3131
* @throws AdapterException
3232
*/
33-
public function build(string $class, Set $set, array $path = []): mixed
33+
public function build(string $class, Set $set = new Set([]), array $path = []): mixed
3434
{
3535
try {
3636
return $this->make($class, $set, $path);

src/Factory/DefaultSpecsFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Constructo\Factory;
66

77
use Constructo\Contract\Reflect\SpecsFactory;
8+
use Constructo\Core\Serialize\Builder;
89
use Constructo\Support\Metadata\Schema\Registry\Specs;
910
use InvalidArgumentException;
1011

@@ -17,13 +18,15 @@
1718

1819
readonly class DefaultSpecsFactory implements SpecsFactory
1920
{
20-
public function __construct(private array $specs = [])
21-
{
21+
public function __construct(
22+
private Builder $builder,
23+
private array $specs = [],
24+
) {
2225
}
2326

2427
public function make(): Specs
2528
{
26-
$registry = new Specs();
29+
$registry = new Specs($this->builder);
2730
foreach ($this->specs as $key => $value) {
2831
$this->validate($key, $value);
2932
$registry->register(stringify($key), arrayify($value));

src/Support/Metadata/Schema/Field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public function hasRule(string $rule): bool
166166

167167
public function __call(string $name, array $arguments): self
168168
{
169-
if ($this->is(self::MAPPING, $name)) {
169+
if ($this->isFrom(self::MAPPING, $name)) {
170170
$this->handleMapping(...$arguments);
171171
return $this;
172172
}
173-
if ($this->is(self::VISIBILITY, $name)) {
173+
if ($this->isFrom(self::VISIBILITY, $name)) {
174174
$this->handleVisibility($name);
175175
return $this;
176176
}
@@ -182,7 +182,7 @@ public function __call(string $name, array $arguments): self
182182
throw new BadMethodCallException(sprintf("Entry '%s' is not supported.", $name));
183183
}
184184

185-
private function is(array $haystack, string $needle): bool
185+
private function isFrom(array $haystack, string $needle): bool
186186
{
187187
return in_array($needle, $haystack, true);
188188
}

src/Support/Metadata/Schema/Field/Formatter/MergeFormatter.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@
1919
class MergeFormatter implements Formatter
2020
{
2121
public function format(mixed $value, mixed $option = null): array
22+
{
23+
$items = $this->getItems($value);
24+
if (is_string($items) && class_exists($items) && is_subclass_of($items, BackedEnum::class)) {
25+
$items = $items::cases();
26+
}
27+
return $this->formatItems($items);
28+
}
29+
30+
private function getItems(mixed $value): mixed
2231
{
2332
if (! is_array($value) || count($value) < 1) {
2433
throw new BadMethodCallException('MergeFormatter requires an array with at least one element.');
2534
}
2635
[$items] = $value;
27-
if (is_string($items) && class_exists($items) && is_subclass_of($items, BackedEnum::class)) {
28-
$items = $items::cases();
29-
}
36+
return $items;
37+
}
38+
39+
private function formatItems(mixed $items): array
40+
{
3041
$callback = fn (mixed $item) => match (true) {
3142
$item instanceof BackedEnum => $item->value,
3243
default => stringify($item),

src/Support/Metadata/Schema/Field/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function enforce(mixed $item): string
5757
fn ($element): string => $this->enforce($element),
5858
is_array($item)
5959
? $item
60-
: iterator_to_array($item)
60+
: iterator_to_array($item, false)
6161
)
6262
),
6363
default => stringify($item),

src/Support/Metadata/Schema/Registry/Specs.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Constructo\Support\Metadata\Schema\Registry;
66

77
use Constructo\Contract\Formatter;
8+
use Constructo\Core\Serialize\Builder;
89
use Constructo\Support\Set;
910
use InvalidArgumentException;
1011

@@ -18,6 +19,10 @@ class Specs
1819
{
1920
private array $specs = [];
2021

22+
public function __construct(private readonly Builder $builder)
23+
{
24+
}
25+
2126
public function get(string $name): ?Spec
2227
{
2328
$name = snakify($name);
@@ -54,7 +59,15 @@ protected function defineFormatter(Set $properties): ?Formatter
5459
$given = gettype($formatter);
5560
throw new InvalidArgumentException(sprintf('Formatter must be a valid class-string, %s given.', $given));
5661
}
57-
$instance = new $formatter();
62+
return $this->createFormatter($formatter);
63+
}
64+
65+
/**
66+
* @param class-string<object> $formatter
67+
*/
68+
private function createFormatter(string $formatter): Formatter
69+
{
70+
$instance = $this->builder->build($formatter);
5871
if (! $instance instanceof Formatter) {
5972
$given = gettype($instance);
6073
throw new InvalidArgumentException(

src/Testing/MakeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
trait MakeExtension
1414
{
1515
/**
16-
* @template T of mixed
16+
* @template T of object
1717
* @param class-string<T> $class
1818
* @param array<string, mixed> $args
1919
*
2020
* @return T
2121
* @throws ReflectionException
2222
*/
23-
protected function make(string $class, array $args = []): mixed
23+
protected function make(string $class, array $args = []): object
2424
{
2525
return (new ReflectionClass($class))->newInstanceArgs(array_values($args));
2626
}

0 commit comments

Comments
 (0)