Skip to content

Commit f62b097

Browse files
authored
Normalize whitespace in rendered template output (#195)
1 parent 2286b62 commit f62b097

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

src/Render/Templates.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class Templates
3838
public static function renderCoreFile(Config $config, CoreFile $coreFile, array $kwargs): string
3939
{
4040
extract($kwargs);
41-
return require $coreFile->getTemplateFile();
41+
return self::normalize(require $coreFile->getTemplateFile());
4242
}
4343

4444
/**
@@ -48,7 +48,7 @@ public static function renderCoreFile(Config $config, CoreFile $coreFile, array
4848
*/
4949
public static function renderVersionXHTMLTypeClass(Version $version, Type $type): string
5050
{
51-
return require sprintf('%s/class_xhtml.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
51+
return self::normalize(require sprintf('%s/class_xhtml.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
5252
}
5353

5454
/**
@@ -59,11 +59,11 @@ public static function renderVersionXHTMLTypeClass(Version $version, Type $type)
5959
public static function renderVersionTypeClass(Version $version, Type $type): string
6060
{
6161
if ($type->getKind()->isResourceContainer($version)) {
62-
return require sprintf('%s/class_resource_container.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
62+
return self::normalize(require sprintf('%s/class_resource_container.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
6363
} else if ($type->isPrimitiveType() && !$type->hasPrimitiveTypeParent()) {
64-
return require sprintf('%s/class_primitive.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
64+
return self::normalize(require sprintf('%s/class_primitive.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
6565
}
66-
return require sprintf('%s/class_default.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
66+
return self::normalize(require sprintf('%s/class_default.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
6767
}
6868

6969
/**
@@ -73,6 +73,24 @@ public static function renderVersionTypeClass(Version $version, Type $type): str
7373
*/
7474
public static function renderVersionTypeClassTest(Version $version, Type $type): string
7575
{
76-
return require sprintf('%s/class.php', PHPFHIR_TEMPLATE_TESTS_VERSIONS_TYPES_DIR);
76+
return self::normalize(require sprintf('%s/class.php', PHPFHIR_TEMPLATE_TESTS_VERSIONS_TYPES_DIR));
7777
}
78-
}
78+
79+
/**
80+
* Normalize whitespace in rendered template output.
81+
*
82+
* Templates are PHP files rendered via output buffering, so indented inline control
83+
* structures (e.g. " <?php endif; ?>") emit their leading indentation as whitespace-only
84+
* or trailing-whitespace lines. Normalizing here, at the single render chokepoint, keeps
85+
* every generated file free of trailing whitespace and ending in exactly one newline,
86+
* without editing each template.
87+
*
88+
* @param string $rendered
89+
* @return string
90+
*/
91+
private static function normalize(string $rendered): string
92+
{
93+
$rendered = preg_replace('/[ \t]+$/m', '', $rendered);
94+
return rtrim($rendered, "\r\n") . "\n";
95+
}
96+
}

0 commit comments

Comments
 (0)