Skip to content

Commit 09b9406

Browse files
committed
rethinking xhml type, will require further rethinking.
1 parent 86e64ed commit 09b9406

File tree

13 files changed

+194
-286
lines changed

13 files changed

+194
-286
lines changed

src/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ public function renderFHIRClasses(): void
9595
foreach ($types->getIterator() as $type) {
9696
$log->debug("Generating class for type {$type}...");
9797

98-
// TODO: eventually merge "raw" into typical workflow?
98+
// TODO(@dcarbone): revisit with template system refactor
9999
if (PHPFHIR_XHTML_TYPE_NAME === $type->getFHIRName()) {
100-
$classDefinition = Templates::renderRawTypeClass($this->config, $types, $type);
100+
$classDefinition = Templates::renderXhtmlTypeClass($this->config, $types, $type);
101101
} else {
102102
$classDefinition = Templates::renderTypeClass($this->config, $types, $type);
103103
}
104104
$filepath = FileUtils::buildTypeFilePath($this->config, $type);
105-
if (!(bool)file_put_contents($filepath, $classDefinition)) {
105+
if (!file_put_contents($filepath, $classDefinition)) {
106106
throw new RuntimeException(
107107
sprintf(
108108
'Unable to write Type %s class definition to file %s',
@@ -170,7 +170,7 @@ public function renderTestClasses(): void
170170
$log->debug("Generated {$testType} test class for type {$type}...");
171171
$classDefinition = Templates::renderTypeTestClass($this->config, $types, $type, $testType);
172172
$filepath = FileUtils::buildTypeTestFilePath($this->config, $type, $testType);
173-
if (!(bool)file_put_contents($filepath, $classDefinition)) {
173+
if (false === file_put_contents($filepath, $classDefinition)) {
174174
throw new RuntimeException(
175175
sprintf(
176176
'Unable to write Type %s class definition to file %s',

src/Definition/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public function buildValidationMap(): array
447447
*/
448448
public function setOverloaded(bool $overloaded): Property
449449
{
450-
$this->overloaded = (bool)$overloaded;
450+
$this->overloaded = $overloaded;
451451
return $this;
452452
}
453453

src/Definition/Type.php

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Type
4545
private string $fhirName;
4646

4747
/** @var \DCarbone\PHPFHIR\Enum\TypeKind|null */
48-
private ?TypeKind $kind = null;
48+
private null|TypeKind $kind = null;
4949

5050
/** @var string */
5151
private string $className;
@@ -54,19 +54,19 @@ class Type
5454
private Properties $localProperties;
5555

5656
/** @var null|string */
57-
private ?string $parentTypeName = null;
57+
private null|string $parentTypeName = null;
5858
/** @var null|\DCarbone\PHPFHIR\Definition\Type */
59-
private ?Type $parentType = null;
59+
private null|Type $parentType = null;
6060

6161
/** @var int */
6262
private int $minLength = 0;
6363
/** @var int */
6464
private int $maxLength = PHPFHIR_UNLIMITED;
6565
/** @var null|string */
66-
private ?string $pattern = null;
66+
private null|string $pattern = null;
6767

6868
/** @var null|\DCarbone\PHPFHIR\Definition\Type */
69-
private ?Type $componentOfType = null;
69+
private null|Type $componentOfType = null;
7070

7171
/** @var \DCarbone\PHPFHIR\Definition\Enumeration */
7272
private Enumeration $enumeration;
@@ -78,10 +78,10 @@ class Type
7878
private PrimitiveType $primitiveType;
7979

8080
/** @var null|string */
81-
private ?string $restrictionBaseFHIRName = null;
81+
private null|string $restrictionBaseFHIRName = null;
8282

8383
/** @var null|\DCarbone\PHPFHIR\Definition\Type */
84-
private ?Type $restrictionBaseFHIRType = null;
84+
private null|Type $restrictionBaseFHIRType = null;
8585

8686
/** @var bool */ // TODO: what the hell is this...?
8787
private bool $mixed = false;
@@ -156,15 +156,19 @@ public function getImports(): TypeImports
156156
}
157157

158158
/**
159-
* @param bool $withClass
159+
* @param bool $withConstClass
160160
* @param string $prefix
161161
* @return string
162162
*/
163-
public function getConstName(bool $withClass, string $prefix = ''): string
163+
public function getConstName(bool $withConstClass, string $prefix = ''): string
164164
{
165-
return ($withClass ? PHPFHIR_CLASSNAME_CONSTANTS . '::' : '') . strtoupper($prefix) . NameUtils::getConstName(
166-
$this->getFHIRName()
167-
);
165+
if ($withConstClass) {
166+
$cn = sprintf('%s::', PHPFHIR_CLASSNAME_CONSTANTS);
167+
} else {
168+
$cn = '';
169+
}
170+
171+
return sprintf('%s%s%s', $cn, strtoupper($prefix), NameUtils::getConstName($this->getFHIRName()));
168172
}
169173

170174
/**
@@ -188,7 +192,7 @@ public function getClassNameConst(bool $withClass): string
188192
/**
189193
* @return \DCarbone\PHPFHIR\Enum\TypeKind|null
190194
*/
191-
public function getKind(): ?TypeKind
195+
public function getKind(): null|TypeKind
192196
{
193197
return $this->kind;
194198
}
@@ -271,9 +275,12 @@ public function getFullyQualifiedNamespace(bool $leadingSlash): string
271275
$ns = $this->getConfig()->getNamespace(false);
272276
$typeNS = $this->getTypeNamespace();
273277
if ('' !== $typeNS) {
274-
$ns = "{$ns}\\{$typeNS}";
278+
$ns = sprintf('%s\\%s', $ns, $typeNS);
275279
}
276-
return $leadingSlash ? "\\{$ns}" : $ns;
280+
return match ($leadingSlash) {
281+
true => sprintf('\\%s', $ns),
282+
false => $ns,
283+
};
277284
}
278285

279286
/**
@@ -286,9 +293,12 @@ public function getFullyQualifiedTestNamespace($testType, bool $leadingSlash): s
286293
$ns = $this->getConfig()->getTestsNamespace($testType, false);
287294
$typeNS = $this->getTypeNamespace();
288295
if ('' !== $typeNS) {
289-
$ns = "{$ns}\\{$typeNS}";
296+
$ns = sprintf('%s\\%s', $ns, $typeNS);
290297
}
291-
return $leadingSlash ? "\\{$ns}" : $ns;
298+
return match ($leadingSlash) {
299+
true => sprintf('\\%s', $ns),
300+
false => $ns,
301+
};
292302
}
293303

294304
/**
@@ -301,9 +311,12 @@ public function getFullyQualifiedClassName(bool $leadingSlash): string
301311
if ('' === $cn) {
302312
$cn = $this->getClassName();
303313
} else {
304-
$cn .= "\\{$this->getClassName()}";
314+
$cn = sprintf('%s\\%s', $cn, $this->getClassName());
305315
}
306-
return $leadingSlash ? "\\{$cn}" : $cn;
316+
return match ($leadingSlash) {
317+
true => sprintf('\\%s', $cn),
318+
false => $cn,
319+
};
307320
}
308321

309322
/**
@@ -321,13 +334,16 @@ public function getTestClassName(): string
321334
*/
322335
public function getFullyQualifiedTestClassName($testType, bool $leadingSlash): string
323336
{
324-
$ns = $this->getFullyQualifiedTestNamespace($testType, false);
325-
if ('' === $ns) {
337+
$cn = $this->getFullyQualifiedTestNamespace($testType, false);
338+
if ('' === $cn) {
326339
$cn = $this->getTestClassName();
327340
} else {
328-
$cn = "{$ns}\\{$this->getTestClassName()}";
341+
$cn = sprintf('%s\\%s', $cn, $this->getTestClassName());
329342
}
330-
return $leadingSlash ? "\\{$cn}" : $ns;
343+
return match ($leadingSlash) {
344+
true => sprintf('\\%s', $cn),
345+
false => $cn,
346+
};
331347
}
332348

333349
/**
@@ -404,7 +420,7 @@ public function getRootType(): Type
404420
/**
405421
* @return \DCarbone\PHPFHIR\Definition\Type|null
406422
*/
407-
public function getParentType(): ?Type
423+
public function getParentType(): null|Type
408424
{
409425
return $this->parentType;
410426
}
@@ -441,7 +457,7 @@ public function setParentType(Type $type): Type
441457
/**
442458
* @return null|string
443459
*/
444-
public function getParentTypeName(): ?string
460+
public function getParentTypeName(): null|string
445461
{
446462
return $this->parentTypeName;
447463
}
@@ -450,7 +466,7 @@ public function getParentTypeName(): ?string
450466
* @param string|null $parentTypeName
451467
* @return \DCarbone\PHPFHIR\Definition\Type
452468
*/
453-
public function setParentTypeName(?string $parentTypeName): Type
469+
public function setParentTypeName(null|string $parentTypeName): Type
454470
{
455471
$this->parentTypeName = $parentTypeName;
456472
return $this;
@@ -539,7 +555,7 @@ public function setMaxLength(int $maxLength): Type
539555
/**
540556
* @return string|null
541557
*/
542-
public function getPattern(): ?string
558+
public function getPattern(): null|string
543559
{
544560
return $this->pattern;
545561
}
@@ -548,7 +564,7 @@ public function getPattern(): ?string
548564
* @param string|null $pattern
549565
* @return \DCarbone\PHPFHIR\Definition\Type
550566
*/
551-
public function setPattern(?string $pattern): Type
567+
public function setPattern(null|string $pattern): Type
552568
{
553569
$this->pattern = $pattern;
554570
return $this;
@@ -557,7 +573,7 @@ public function setPattern(?string $pattern): Type
557573
/**
558574
* @return \DCarbone\PHPFHIR\Definition\Type|null
559575
*/
560-
public function getComponentOfType(): ?Type
576+
public function getComponentOfType(): null|Type
561577
{
562578
return $this->componentOfType;
563579
}
@@ -627,7 +643,7 @@ public function setUnionOf(array $unionOf): Type
627643
/**
628644
* @return string|null
629645
*/
630-
public function getRestrictionBaseFHIRName(): ?string
646+
public function getRestrictionBaseFHIRName(): null|string
631647
{
632648
return $this->restrictionBaseFHIRName;
633649
}
@@ -645,7 +661,7 @@ public function setRestrictionBaseFHIRName(string $restrictionBaseFHIRName): Typ
645661
/**
646662
* @return \DCarbone\PHPFHIR\Definition\Type|null
647663
*/
648-
public function getRestrictionBaseFHIRType(): ?Type
664+
public function getRestrictionBaseFHIRType(): null|Type
649665
{
650666
return $this->restrictionBaseFHIRType;
651667
}
@@ -758,13 +774,32 @@ public function getDirectlyUsedTraits(): array
758774
{
759775
$traits = [];
760776
$parentType = $this->getParentType();
777+
761778
if (null === $parentType) {
779+
// if this type has no parent(s), try to add all traits
780+
762781
if ($this->isCommentContainer()) {
763782
$traits[] = PHPFHIR_TRAIT_COMMENT_CONTAINER;
764783
}
765-
$traits[] = PHPFHIR_TRAIT_VALIDATION_ASSERTIONS;
766-
$traits[] = PHPFHIR_TRAIT_CHANGE_TRACKING;
767-
$traits[] = PHPFHIR_TRAIT_XMLNS;
784+
785+
// these must only be added if the type has local properties
786+
if ($this->hasLocalProperties()) {
787+
array_push(
788+
$traits,
789+
PHPFHIR_TRAIT_VALIDATION_ASSERTIONS,
790+
PHPFHIR_TRAIT_CHANGE_TRACKING,
791+
PHPFHIR_TRAIT_XMLNS,
792+
);
793+
}
794+
} else if (!$parentType->hasLocalProperties()) {
795+
// if this type _does_ have a parent, only add these traits if the parent does not have local properties
796+
797+
array_push(
798+
$traits,
799+
PHPFHIR_TRAIT_VALIDATION_ASSERTIONS,
800+
PHPFHIR_TRAIT_CHANGE_TRACKING,
801+
PHPFHIR_TRAIT_XMLNS,
802+
);
768803
}
769804

770805
return $traits;
@@ -789,7 +824,7 @@ public function hasCommentContainerParent(): bool
789824
*/
790825
public function setCommentContainer(bool $commentContainer): Type
791826
{
792-
$this->commentContainer = (bool)$commentContainer;
827+
$this->commentContainer = $commentContainer;
793828
return $this;
794829
}
795830

src/Definition/Types.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ class Types implements Countable
4949
public function __construct(VersionConfig $config)
5050
{
5151
$this->config = $config;
52-
$rt = new Type($config, PHPFHIR_XHTML_TYPE_NAME);
53-
$rt->setKind(TypeKind::PHPFHIR_XHTML);
54-
$rt->addDocumentationFragment(PHPFHIR_XHTML_TYPE_DESCRIPTION);
55-
$this->addType($rt);
52+
53+
// construct and add "XHTML" type
54+
// TODO(dcarbone): this sucks.
55+
$xt = new Type($config, PHPFHIR_XHTML_TYPE_NAME);
56+
$xt->setKind(TypeKind::PHPFHIR_XHTML);
57+
$xt->addDocumentationFragment(PHPFHIR_XHTML_TYPE_DESCRIPTION);
58+
59+
$this->addType($xt);
5660
}
5761

5862
/**

src/Render/Templates.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function renderPHPFHIRResponseParserClass(VersionConfig $config, T
121121
*/
122122
public static function renderTypeClass(VersionConfig $config, Types $types, Type $type): string
123123
{
124-
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class.php';
124+
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_default.php';
125125
}
126126

127127
/**
@@ -130,9 +130,9 @@ public static function renderTypeClass(VersionConfig $config, Types $types, Type
130130
* @param \DCarbone\PHPFHIR\Definition\Type $type
131131
* @return string
132132
*/
133-
public static function renderRawTypeClass(VersionConfig $config, Types $types, Type $type): string
133+
public static function renderXhtmlTypeClass(VersionConfig $config, Types $types, Type $type): string
134134
{
135-
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'xhtml.php';
135+
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_xhtml.php';
136136
}
137137

138138
/**
@@ -194,6 +194,6 @@ public static function renderTypeMapTestClass(VersionConfig $config, Types $type
194194
*/
195195
public static function renderTypeTestClass(VersionConfig $config, Types $types, Type $type, string $testType): string
196196
{
197-
return require PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR .'class.php';
197+
return require PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR .'class_default.php';
198198
}
199199
}

src/Utilities/FileUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function mkdirRecurse(VersionConfig $config, string $pathSuffix):
5050
$config->getLogger()->debug(sprintf('Directory at path "%s" already exists.', $path));
5151
} else {
5252
$config->getLogger()->info(sprintf('Attempting to create directory at path "%s"...', $path));
53-
if (!(bool)mkdir($path)) {
53+
if (!mkdir($path)) {
5454
$msg = 'Unable to create directory at path "' . $path . '"';
5555
$config->getLogger()->critical($msg);
5656
throw new RuntimeException($msg);
File renamed without changes.

0 commit comments

Comments
 (0)