Skip to content

Commit

Permalink
more named parameter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 8, 2025
1 parent 48204e5 commit aced606
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
14 changes: 13 additions & 1 deletion src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu
$ptp = $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType();
$hintTypes = $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints();
array_merge($hintTypes, self::buildBaseHintParts($version, $ptp, $fullyQualified));
} else if ($tk->isOneOf(TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER)) {
} else if ($tk->isResourceContainer($version)) {
$hintTypes = [
match ($fullyQualified) {
true => $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE),
Expand Down Expand Up @@ -202,6 +202,10 @@ public static function propertyDeclarationHint(Version $version, Property $prope
);
}

if ($pt->getKind()->isResourceContainer($version)) {
return ($nullable ? 'null|' : '') . PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE;
}

return ($nullable ? 'null|' : '') . $pt->getClassName();
}

Expand All @@ -227,6 +231,14 @@ public static function propertyGetterDocHint(Version $version,
);
}

if ($pt->getKind()->isResourceContainer($version)) {
$versionCoreFiles = $version->getCoreFiles();
$containedTypeInterface = $versionCoreFiles->getCoreFileByEntityName(PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE);
return ($nullable ? 'null|' : '')
. $containedTypeInterface->getFullyQualifiedName(true)
. ($property->isCollection() ? '[]' : '');
}

$hint = ($nullable ? 'null|' : '') . $pt->getFullyQualifiedClassName(true);

if ($property->isCollection()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,12 @@

/** @var \DCarbone\PHPFHIR\Version\Definition\Property $property */

$propertyType = $property->getValueFHIRType();
$propertyVarName = "\${$property->getName()}";
$propertyTypeClassName = $propertyType->getClassName();
$propertyFieldConst = $property->getFieldConstantName();
$propertyFieldConstExt = $property->getFieldConstantExtensionName();
$setter = $property->getSetterName();

$requireArgs = [
'property' => $property
];

ob_start(); ?>
if (null !== <?php echo $propertyVarName; if ($property->isCollection()) : ?> && [] !== <?php echo $propertyVarName; endif; ?>) {
if (null !== $<?php echo $property->getName(); ?>) {
<?php if ($property->isCollection()) : ?>
$_values = [];
foreach(<?php echo $propertyVarName; ?> as $i => $v) {
if (!($v instanceof <?php echo $property->getValueFHIRType()->getClassName(); ?>)) {
$_values[] = new <?php echo $property->getValueFHIRType()->getClassName(); ?>($v);
} else {
$_values[] = $v;
}
}
$this->set<?php echo $property->getName(); ?>(...$_values);
$this->set<?php echo $property->getName(); ?>(...$<?php echo $property->getName(); ?>);
<?php else : ?>
$this-><?php echo $setter; ?>(<?php echo $propertyVarName; ?>);
$this-><?php echo $property->getSetterName(); ?>($<?php echo $property->getName(); ?>);
<?php endif; ?>
}
<?php
Expand Down
20 changes: 10 additions & 10 deletions template/versions/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public function get<?php echo ucfirst($propertyName); ?>Generator(): \Generator
*/
public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUtils::buildSetterParameterHint($version, $property, false, true); ?> $<?php echo $property; ?>): self
{
<?php if ($propertyType->isValueContainer() || $propertyTypeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) :
?>
<?php if ($propertyTypeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) : ?>
if (!($<?php echo $propertyName; ?> instanceof <?php echo $propertyTypeClassName; ?>)) {
$<?php echo $propertyName; ?> = new <?php echo $propertyTypeClassName; ?>($<?php echo $propertyName; ?>);
}
Expand All @@ -124,17 +123,18 @@ public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUti
*/
public function set<?php echo ucfirst($propertyName); ?>(<?php echo TypeHintUtils::buildSetterParameterHint($version, $property, false, true); ?> ...$<?php echo $propertyName; ?>): self
{
if ([] !== $this-><?php echo $propertyName; ?>) {
$this-><?php echo $propertyName; ?> = [];
}
<?php if ($propertyType->isValueContainer()) : ?>
$this-><?php echo $propertyName; ?> = [];
foreach($<?php echo $propertyName; ?> as $v) {
<?php if ($propertyType->isValueContainer()) : ?> if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
<?php endif; ?> $this-><?php echo $propertyName; echo $isCollection ? '[]' : ''; ?> = $v;
<?php if ($propertyType->isValueContainer()) : ?> } else {
$this-><?php echo $propertyName; echo $isCollection ? '[]' : ''; ?> = new <?php echo $propertyTypeClassName; ?>($v);
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $propertyName; ?>[] = $v;
} else {
$this-><?php echo $propertyName; ?>[] = new <?php echo $propertyTypeClassName; ?>($v);
}
<?php endif; ?>
}
<?php else : ?>
$this-><?php echo $propertyName; ?> = $<?php echo $propertyName; ?>;
<?php endif; ?>
return $this;
}
<?php endif;
Expand Down

0 comments on commit aced606

Please sign in to comment.