Skip to content

Commit 550662c

Browse files
committed
refactor(fake): improve Faker resolver instantiation using MakeExtension
- Use MakeExtension trait in Faker for consistent resolver creation - Replace manual resolver instantiation with make() method - Refactor FromDefaultValue to check ignoreFromDefaultValue flag - Remove unused MakeExtension trait from FromTypeAttributes - Rename constructor parameter 'case' to 'notation' for clarity - Change visibility of ignoreFromDefaultValue to protected
1 parent d9017b2 commit 550662c

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

src/Core/Fake/Faker.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Constructo\Support\Reflective\Notation;
2020
use Constructo\Support\Set;
2121
use Constructo\Support\Value;
22+
use Constructo\Testing\MakeExtension;
2223
use DateTime;
2324
use Faker\Factory;
2425
use Faker\Generator;
@@ -190,19 +191,21 @@
190191
*/
191192
class Faker extends Engine implements Contract
192193
{
194+
use MakeExtension;
195+
193196
protected readonly Generator $generator;
194197

195198
/**
196199
* @param array<callable|Formatter> $formatters
197200
* @SuppressWarnings(StaticAccess)
198201
*/
199202
public function __construct(
200-
Notation $case = Notation::SNAKE,
203+
Notation $notation = Notation::SNAKE,
201204
array $formatters = [],
202205
private readonly ?string $locale = null,
203-
private readonly bool $ignoreFromDefaultValue = false,
206+
protected readonly bool $ignoreFromDefaultValue = false,
204207
) {
205-
parent::__construct($case, $formatters);
208+
parent::__construct($notation, $formatters);
206209

207210
$this->generator = Factory::create($this->locale($locale));
208211
}
@@ -245,12 +248,9 @@ public function generator(): Generator
245248
private function resolveParameters(array $parameters, Set $presets): Set
246249
{
247250
$values = [];
248-
$fromDefaultValue = $this->ignoreFromDefaultValue
249-
? null
250-
: new FromDefaultValue($this->notation, $this->formatters, $this->locale);
251251
foreach ($parameters as $parameter) {
252252
$field = $this->casedField($parameter);
253-
$generated = $this->generateValue($parameter, $presets, $fromDefaultValue);
253+
$generated = $this->generateValue($parameter, $presets);
254254

255255
if ($generated === null) {
256256
continue;
@@ -274,31 +274,22 @@ private function locale(?string $locale): string
274274
/**
275275
* @throws ReflectionException
276276
*/
277-
private function generateValue(
278-
ReflectionParameter $parameter,
279-
Set $presets,
280-
?FromDefaultValue $fromDefaultValue = null,
281-
): ?Value {
282-
return (new FromDependency(
283-
$this->notation,
284-
$this->formatters,
285-
$this->locale,
286-
$this->ignoreFromDefaultValue
287-
))
288-
->then(new FromTypeDate($this->notation, $this->formatters, $this->locale))
289-
->then(new FromCollection($this->notation, $this->formatters, $this->locale))
290-
->then(new FromTypeBuiltin($this->notation, $this->formatters, $this->locale))
291-
->then(
292-
new FromTypeAttributes(
293-
$this->notation,
294-
$this->formatters,
295-
$this->locale,
296-
$this->ignoreFromDefaultValue
297-
)
298-
)
299-
->then(new FromEnum($this->notation, $this->formatters, $this->locale))
300-
->then($fromDefaultValue)
301-
->then(new FromPreset($this->notation, $this->formatters, $this->locale))
277+
private function generateValue(ReflectionParameter $parameter, Set $presets): ?Value
278+
{
279+
$args = [
280+
'notation' => $this->notation,
281+
'formatters' => $this->formatters,
282+
'locale' => $this->locale,
283+
'ignoreFromDefaultValue' => $this->ignoreFromDefaultValue,
284+
];
285+
return ($this->make(FromDependency::class, $args))
286+
->then($this->make(FromTypeDate::class, $args))
287+
->then($this->make(FromCollection::class, $args))
288+
->then($this->make(FromTypeBuiltin::class, $args))
289+
->then($this->make(FromTypeAttributes::class, $args))
290+
->then($this->make(FromEnum::class, $args))
291+
->then($this->make(FromDefaultValue::class, $args))
292+
->then($this->make(FromPreset::class, $args))
302293
->resolve($parameter, $presets);
303294
}
304295
}

src/Core/Fake/Resolver/FromCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
final class FromCollection extends Resolver
1818
{
19-
use MakeExtension;
2019
use ManagedExtension;
2120

2221
/**

src/Core/Fake/Resolver/FromDefaultValue.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
final class FromDefaultValue extends Resolver
1313
{
1414
public function resolve(ReflectionParameter $parameter, Set $presets): ?Value
15+
{
16+
if ($this->ignoreFromDefaultValue) {
17+
return parent::resolve($parameter, $presets);
18+
}
19+
return $this->resolveFromDefaultValue($parameter, $presets);
20+
}
21+
22+
private function resolveFromDefaultValue(ReflectionParameter $parameter, Set $presets): ?Value
1523
{
1624
if ($parameter->isDefaultValueAvailable()) {
1725
return new Value($parameter->getDefaultValue());

src/Core/Fake/Resolver/FromTypeAttributes.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Constructo\Support\Reflective\Definition\TypeExtended;
1414
use Constructo\Support\Set;
1515
use Constructo\Support\Value;
16-
use Constructo\Testing\MakeExtension;
1716
use Constructo\Testing\ManagedExtension;
1817
use ReflectionNamedType;
1918
use ReflectionParameter;
@@ -22,7 +21,6 @@
2221

2322
final class FromTypeAttributes extends Resolver
2423
{
25-
use MakeExtension;
2624
use ManagedExtension;
2725

2826
/**
@@ -69,8 +67,14 @@ private function resolveByAttributes(ReflectionParameter $parameter): ?Value
6967
private function resolveManaged(Managed $instance): ?Value
7068
{
7169
return match ($instance->management) {
72-
'id' => new Value($this->managed()->id()),
73-
'timestamp' => new Value($this->managed()->now()),
70+
'id' => new Value(
71+
$this->managed()
72+
->id()
73+
),
74+
'timestamp' => new Value(
75+
$this->managed()
76+
->now()
77+
),
7478
default => null,
7579
};
7680
}

src/Core/Fake/Resolver/FromTypeBuiltin.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
use Constructo\Core\Fake\Resolver;
88
use Constructo\Support\Set;
99
use Constructo\Support\Value;
10-
use Constructo\Testing\MakeExtension;
1110
use Constructo\Testing\ManagedExtension;
1211
use ReflectionParameter;
1312

1413
final class FromTypeBuiltin extends Resolver
1514
{
16-
use MakeExtension;
1715
use ManagedExtension;
1816

1917
public function resolve(ReflectionParameter $parameter, Set $presets): ?Value

src/Core/Fake/Resolver/FromTypeDate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
final class FromTypeDate extends Resolver
2020
{
21-
use MakeExtension;
2221
use ManagedExtension;
2322

2423
/**

0 commit comments

Comments
 (0)