Skip to content

Commit 22497c6

Browse files
jsirishchrispenny
authored andcommitted
Refactor: Extract polymorphic relationship handling and improve error messages
- Extract polymorphic relationship logic into dedicated handlePolymorphicRelationship() method - Replace var_export with json_encode for cleaner warning output - Add better error handling for unexpected relationship types - Addresses review feedback from PR #32
1 parent 8064b21 commit 22497c6

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

src/Service/FixtureService.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,9 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
231231
}
232232

233233
foreach ($hasOneRelationships as $relationName => $relationClassName) {
234-
// Handle polymorphic relationships where relationClassName might be an array
235-
if (is_array($relationClassName)) {
236-
// For polymorphic relationships, get the base class
237-
if (isset($relationClassName['class'])) {
238-
$relationClassName = $relationClassName['class'];
239-
} else {
240-
$this->addWarning(sprintf(
241-
'Polymorphic relationship "%s" in class "%s" has invalid configuration: %s',
242-
$relationName,
243-
$dataObject->ClassName,
244-
var_export($relationClassName, true)
245-
));
246-
continue;
247-
}
234+
$relationClassName = $this->handlePolymorphicRelationship($relationName, $relationClassName, $dataObject);
235+
if ($relationClassName === null) {
236+
continue;
248237
}
249238

250239
// Relationship field names (as represented in the Database) are always appended with `ID`
@@ -342,6 +331,48 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
342331
}
343332
}
344333

334+
/**
335+
* Handle polymorphic relationship configurations where the relationship class is defined as an array
336+
*
337+
* @param string $relationName The name of the relationship
338+
* @param string|array $relationClassName The class name or polymorphic configuration array
339+
* @param DataObject $dataObject The data object being processed
340+
* @return string|null The resolved class name, or null if the relationship should be skipped
341+
*/
342+
private function handlePolymorphicRelationship(string $relationName, string|array $relationClassName, DataObject $dataObject): ?string
343+
{
344+
// If it's already a string, no processing needed
345+
if (is_string($relationClassName)) {
346+
return $relationClassName;
347+
}
348+
349+
// Handle polymorphic relationships where relationClassName is an array
350+
if (is_array($relationClassName)) {
351+
// For polymorphic relationships, get the base class
352+
if (isset($relationClassName['class'])) {
353+
return $relationClassName['class'];
354+
} else {
355+
$this->addWarning(sprintf(
356+
'Polymorphic relationship "%s" in class "%s" has invalid configuration: %s',
357+
$relationName,
358+
$dataObject->ClassName,
359+
json_encode($relationClassName, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES, 2)
360+
));
361+
return null;
362+
}
363+
}
364+
365+
// If it's neither string nor array, something's wrong
366+
$this->addWarning(sprintf(
367+
'Relationship "%s" in class "%s" has unexpected type "%s": %s',
368+
$relationName,
369+
$dataObject->ClassName,
370+
gettype($relationClassName),
371+
json_encode($relationClassName, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES, 2)
372+
));
373+
return null;
374+
}
375+
345376
/**
346377
* @throws Exception
347378
*/

0 commit comments

Comments
 (0)