Skip to content

Commit 17be5be

Browse files
committed
Refactoring
1 parent b978297 commit 17be5be

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/Resolvers/DataFromSomethingResolver.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,23 @@ public function execute(
4141

4242
if ($payloadCount === 0 || $payloadCount === 1) {
4343
$properties = $pipeline->execute($payloads[0] ?? [], $creationContext);
44-
} else {
45-
$properties = [];
4644

47-
foreach ($payloads as $payload) {
48-
foreach ($pipeline->execute($payload, $creationContext) as $key => $value) {
49-
if (array_key_exists($key, $properties) && ($value === null || $value instanceof Optional)) {
50-
continue;
51-
}
45+
return $this->dataFromArray($class, $creationContext, $payloads, $properties);
46+
}
47+
48+
$properties = [];
5249

53-
$properties[$key] = $value;
50+
foreach ($payloads as $payload) {
51+
foreach ($pipeline->execute($payload, $creationContext) as $key => $value) {
52+
if (array_key_exists($key, $properties) && ($value === null || $value instanceof Optional)) {
53+
continue;
5454
}
55+
56+
$properties[$key] = $value;
5557
}
5658
}
5759

58-
return $this->propertyMorphedData($class, $creationContext, $payloads, $properties)
59-
?? $this->dataFromArrayResolver->execute($class, $properties);
60+
return $this->dataFromArray($class, $creationContext, $payloads, $properties);
6061
}
6162

6263
protected function createFromCustomCreationMethod(
@@ -117,12 +118,12 @@ protected function createFromCustomCreationMethod(
117118
return $class::$methodName(...$payloads);
118119
}
119120

120-
protected function propertyMorphedData(
121+
protected function dataFromArray(
121122
string $class,
122123
CreationContext $creationContext,
123124
array $payloads,
124125
array $properties,
125-
): ?BaseData {
126+
): BaseData {
126127
$dataClass = $this->dataConfig->getDataClass($class);
127128

128129
if ($dataClass->isAbstract && $dataClass->propertyMorphable) {
@@ -134,6 +135,6 @@ protected function propertyMorphedData(
134135
}
135136
}
136137

137-
return null;
138+
return $this->dataFromArrayResolver->execute($class, $properties);
138139
}
139140
}

src/Resolvers/DataValidationRulesResolver.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ public function execute(
3535
): array {
3636
$dataClass = $this->dataConfig->getDataClass($class);
3737

38-
if ($propertyMorphableClass = $this->propertyMorphableClass($dataClass)) {
38+
if ($this->isPropertyMorphable($dataClass)) {
39+
/**
40+
* @var class-string<PropertyMorphableData> $class
41+
*/
3942
$payload = $path->isRoot() ? $fullPayload : Arr::get($fullPayload, $path->get(), []);
40-
$class = $propertyMorphableClass::morph($payload) ?? $class;
43+
$class = $class::morph($payload) ?? $class;
4144
$dataClass = $this->dataConfig->getDataClass($class);
4245
}
4346

@@ -286,13 +289,8 @@ protected function inferRulesForDataProperty(
286289
);
287290
}
288291

289-
/**
290-
* @return class-string<PropertyMorphableData>
291-
*/
292-
protected function propertyMorphableClass(DataClass $dataClass): ?string
292+
protected function isPropertyMorphable(DataClass $dataClass): bool
293293
{
294-
return $dataClass->isAbstract && $dataClass->propertyMorphable
295-
? $dataClass->name
296-
: null;
294+
return $dataClass->isAbstract && $dataClass->propertyMorphable;
297295
}
298296
}

0 commit comments

Comments
 (0)