From da85ca5d8e84b7a16d447fd4ea886aaf1621d9cd Mon Sep 17 00:00:00 2001 From: benjamin Date: Thu, 12 Sep 2024 21:13:23 +0100 Subject: [PATCH] getContentTemplate - move if case into switch, carefully --- CRM/Utils/System/Base.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index fd7171be47a8..efe37aee9940 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -76,25 +76,32 @@ abstract public function loadBootStrap($params = [], $loadUser = TRUE, $throwErr * @var int|string $print * Should match a CRM_Core_Smarty::PRINT_* constant, * or equal 0 if not in print mode. + * + * @todo when php7.4 is no more, switch the `switch` to a `match` */ public static function getContentTemplate($print = 0): string { - if ($print === CRM_Core_Smarty::PRINT_JSON) { - return 'CRM/common/snippet.tpl'; - } - - switch ($print) { - case 0: + // I fear some callers of this function may still pass FALSE + // let's make sure any falsey value is exactly 0 + $print = $print ?: 0; + + // switch uses lazy type comparison + // on php < 8 this causes strange results when comparing + // string like 'json' with integer 0 + // so we use this workaround + switch (TRUE) { + case ($print === 0): // Not a print context. $config = CRM_Core_Config::singleton(); return 'CRM/common/' . strtolower($config->userFramework) . '.tpl'; - case CRM_Core_Smarty::PRINT_PAGE: + case ($print === CRM_Core_Smarty::PRINT_PAGE): return 'CRM/common/print.tpl'; - case 'xls': - case 'doc': + case ($print === 'xls'): + case ($print === 'doc'): return 'CRM/Contact/Form/Task/Excel.tpl'; + case ($print === CRM_Core_Smarty::PRINT_JSON): default: return 'CRM/common/snippet.tpl'; }