Skip to content

Commit

Permalink
#126: replace null checks with isset in all cases; replace string con…
Browse files Browse the repository at this point in the history
…catenation with sprintf
  • Loading branch information
Chris Riley committed May 25, 2023
1 parent c202991 commit ccc680c
Showing 1 changed file with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use function array_merge;
use function implode;
use function reset;
use function sprintf;
use function var_export;

/**
Expand Down Expand Up @@ -107,19 +108,13 @@ private function generatePropertyHydrateCall(ObjectProperty $property, string $i
$escapedName = var_export($propertyName, true);

if ($property->allowsNull && ! $property->hasDefault) {
return ['$object->' . $propertyName . ' = ' . $inputArrayName . '[' . $escapedName . '] ?? null;'];
}

$nullCheck = ' || $object->' . $propertyName . ' !== null && \\array_key_exists(' . $escapedName . ', ' . $inputArrayName . ')';
if ($property->hasType) {
$nullCheck = ' || isset($object->' . $propertyName . ') && $object->' . $propertyName . ' !== null && \\array_key_exists(' . $escapedName . ', ' . $inputArrayName . ')';
return [sprintf('$object->%s = %s[%s] ?? null;', $propertyName, $inputArrayName, $escapedName)];
}

return [
'if (isset(' . $inputArrayName . '[' . $escapedName . '])',
$nullCheck,
sprintf('if (isset(%s[%s]) || isset($object->%s) && \\array_key_exists(%2$s, %1$s)', $inputArrayName, $escapedName, $propertyName),
') {',
' $object->' . $propertyName . ' = ' . $inputArrayName . '[' . $escapedName . '];',
sprintf(' $object->%s = %s[%s];', $propertyName, $inputArrayName, $escapedName),
'}',
];
}
Expand All @@ -132,17 +127,17 @@ private function generatePropertyHydrateCall(ObjectProperty $property, string $i
private function generatePropertyExtractCall(ObjectProperty $property): array
{
$propertyName = $property->name;
$assignmentStatement = "\$values['" . $propertyName . "'] = \$object->" . $propertyName . ';';
$assignmentStatement = sprintf(' $values[\'%s\'] = $object->%1$s;', $propertyName);
$requiresGuard = $property->hasType && !($property->hasDefault || $property->allowsNull);

if (!$requiresGuard) {
return [' ' . $assignmentStatement];
return [$assignmentStatement];
}

return [
' if (isset($object->' . $propertyName . ')) {',
' ' . $assignmentStatement,
' }'
sprintf(' if (isset($object->%s)) {', $propertyName),
$assignmentStatement,
' }',
];
}

Expand All @@ -162,15 +157,15 @@ private function replaceConstructor(ClassMethod $method): void
$bodyParts = array_merge($bodyParts, $this->generatePropertyHydrateCall($property, '$values'));
}

$bodyParts[] = '}, null, ' . var_export($className, true) . ');' . "\n";
$bodyParts[] = sprintf("}, null, %s);\n", var_export($className, true));

// Extract closures
$bodyParts[] = '$this->extractCallbacks[] = \\Closure::bind(static function ($object, &$values) {';
foreach ($properties as $property) {
$bodyParts = array_merge($bodyParts, $this->generatePropertyExtractCall($property));
}

$bodyParts[] = '}, null, ' . var_export($className, true) . ');' . "\n";
$bodyParts[] = sprintf("}, null, %s);\n", var_export($className, true));
}

$method->stmts = (new ParserFactory())
Expand Down

0 comments on commit ccc680c

Please sign in to comment.