Skip to content

Commit

Permalink
more named property funtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 8, 2025
1 parent 812c081 commit 48204e5
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 439 deletions.
115 changes: 45 additions & 70 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,21 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu
*/
public static function propertyDeclarationHint(Version $version, Property $property, bool $nullable): string
{
// if this proprety is a collection, the type hint must be a potentially nullable array
if ($property->isCollection()) {
return sprintf('%sarray', $nullable ? 'null|' : '');
return $nullable ? 'null|array' : 'array';
}

// first, check to see if there is a FHIR type for this property value
$t = $property->getValueFHIRType();
$pt = $property->getValueFHIRType();

// if null, the (hopefully) only possibility is that this is a value property for a primitive type
if (null === $t) {
if (null === $pt) {
return self::primitivePHPValueTypeHint(
$version,
$property->getMemberOf()->getPrimitiveType(),
$nullable,
);
}

// otherwise, hint as the underlying type
return self::typeHint($version, $t, $nullable);
return ($nullable ? 'null|' : '') . $pt->getClassName();
}

/**
Expand All @@ -217,30 +213,27 @@ public static function propertyDeclarationHint(Version $version, Property $prope
* @param bool $nullable
* @return string
*/
public static function propertyGetterTypeDocHint(Version $version,
Property $property,
bool $nullable): string
public static function propertyGetterDocHint(Version $version,
Property $property,
bool $nullable): string
{
$t = $property->getValueFHIRType();
if (null === $t) {
$pt = $property->getValueFHIRType();

if (null === $pt) {
return self::primitivePHPValueTypeHint(
$version,
$property->getMemberOf()->getPrimitiveType(),
$nullable,
);
}

$hintTypes = self::buildBaseHintParts($version, $t, false);
$hint = ($nullable ? 'null|' : '') . $pt->getFullyQualifiedClassName(true);

if ($property->isCollection()) {
$hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes);
return "{$hint}[]";
}

if ($nullable) {
array_unshift($hintTypes, 'null');
}

return implode('|', $hintTypes);
return $hint;
}

/**
Expand All @@ -249,35 +242,34 @@ public static function propertyGetterTypeDocHint(Version $version,
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Property $property
* @param bool $nullable
* @param bool $ignoreCollection
* @return string
*/
public static function buildConstructorParameterDocHint(Version $version,
Property $property,
bool $nullable): string
public static function buildSetterParameterDocHint(Version $version,
Property $property,
bool $nullable,
bool $ignoreCollection = false): string
{
$pt = $property->getValueFHIRType();

if ($property->isValueProperty()) {
if ($property->getMemberOf()->getKind() !== TypeKindEnum::PRIMITIVE_CONTAINER) {
return self::primitivePHPValueTypeHint(
version: $version,
primitiveType: $property->getMemberOf()->getPrimitiveType(),
nullable: $nullable,
stringable: true,
);
}
$hintTypes = [
'null',
];
} else {
$hintTypes = self::buildBaseHintParts($version, $pt, true);
if (null === $pt) {
return self::primitivePHPValueTypeHint(
version: $version,
primitiveType: $property->getMemberOf()->getPrimitiveType(),
nullable: $nullable,
stringable: true,
);
}


$hintTypes = self::buildBaseHintParts($version, $pt, true);

$ptk = $pt->getKind();

if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE_CONTAINER)) {
if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
$hintTypes[] = $pt->getFullyQualifiedClassName(true);
}

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand All @@ -286,7 +278,7 @@ public static function buildConstructorParameterDocHint(Version $version,
);
}

if ($property->isCollection()) {
if (!$ignoreCollection && $property->isCollection()) {
$hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes);
}

Expand All @@ -301,13 +293,16 @@ public static function buildConstructorParameterDocHint(Version $version,
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Property $property
* @param bool $nullable
* @param bool $ignoreCollection
* @return string
*/
public static function buildConstructorParameterHint(Version $version,
Property $property,
bool $nullable): string
public static function buildSetterParameterHint(Version $version,
Property $property,
bool $nullable,
bool $ignoreCollection = false): string
{
$pt = $property->getValueFHIRType();

if (null === $pt) {
return self::primitivePHPValueTypeHint(
version: $version,
Expand All @@ -317,15 +312,19 @@ public static function buildConstructorParameterHint(Version $version,
);
}

if ($property->isCollection()) {
return $nullable ? 'null|array' : 'array';
if (!$ignoreCollection && $property->isCollection()) {
return $nullable ? 'null|iterable' : 'iterable';
}

$hintTypes = self::buildBaseHintParts($version, $pt, false);

$ptk = $pt->getKind();

if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE_CONTAINER)) {
if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
$hintTypes[] = $pt->getClassName();
}

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand All @@ -334,34 +333,10 @@ public static function buildConstructorParameterHint(Version $version,
);
}

if ($property->isCollection()) {
$hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes);
}

if ($nullable) {
array_unshift($hintTypes, 'null');
}

return implode('|', $hintTypes);
}

/**
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Property $property
* @param bool $nullable
* @return string
*/
public static function propertySetterTypeHint(Version $version, Property $property, bool $nullable): string
{
$pt = $property->getValueFHIRType();
$ptk = $pt->getKind();

$hint = self::typeSetterTypeHint($version, $pt, $nullable);

if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) {
return sprintf('%s|%s', $hint, $pt->getClassName());
}

return $hint;
}
}
32 changes: 32 additions & 0 deletions src/Version/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

use DCarbone\PHPFHIR\Enum\PropertyUseEnum;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Utilities\NameUtils;

class Property
Expand Down Expand Up @@ -493,6 +494,37 @@ public function getOverloadedProperty(): null|Property
return $this->_overloads ?? null;
}

/**
* This method will panic if this is a primitive type. Deal with it, also make it better later.
*
* @return bool
*/
public function requiresXMLLocation(): bool
{
return $this->_memberOf->hasPrimitiveParent()
|| $this->_memberOf->getKind()->isOneOf(
TypeKindEnum::PRIMITIVE_CONTAINER,
TypeKindEnum::PRIMITIVE,
TypeKindEnum::LIST,
);
}

/**
* @return string
*/
public function defaultXMLLocationEnumValue(): string
{
if (!$this->requiresXMLLocation()) {
throw new \RuntimeException(sprintf(
'Type "%s" property "%s" does not support XML location setting',
$this->getMemberOf()->getFHIRName(),
$this->getName(),
));
}

return $this->_memberOf->isValueContainer() ? 'ELEMENT' : 'ATTRIBUTE';
}

/**
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
foreach ($type->getProperties()->getGenerator() as $property) : ?>
/**
<?php echo DocumentationUtils::compilePropertyDocumentation($property, 5, true); ?>
* @var <?php echo TypeHintUtils::propertyGetterTypeDocHint($version, $property, true); ?>
* @var <?php echo TypeHintUtils::propertyGetterDocHint($version, $property, true); ?>

*/
protected <?php echo TypeHintUtils::propertyDeclarationHint($version, $property, true); ?> $<?php echo $property->getName(); ?> = <?php echo $property->isCollection() ? '[]' : 'null'; ?>;
Expand Down
Loading

0 comments on commit 48204e5

Please sign in to comment.