From fe0fb4c463ae38250656ec21f402de7e52755450 Mon Sep 17 00:00:00 2001 From: mfettig Date: Tue, 3 May 2022 15:49:52 -0400 Subject: [PATCH] address PHP 8.1 'explode', 'number_format', and 'replace' deprecations --- libs/plugins/modifier.explode.php | 25 ++++++++ libs/plugins/modifier.number_format.php | 26 ++++++++ libs/plugins/shared.mb_str_replace.php | 2 +- .../PluginModifierExplodeTest.php | 56 +++++++++++++++++ .../PluginModifierNumberFormatTest.php | 60 +++++++++++++++++++ .../PluginModifierReplaceTest.php | 54 +++++++++++++++++ 6 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 libs/plugins/modifier.explode.php create mode 100644 libs/plugins/modifier.number_format.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php diff --git a/libs/plugins/modifier.explode.php b/libs/plugins/modifier.explode.php new file mode 100644 index 000000000..5186fde3d --- /dev/null +++ b/libs/plugins/modifier.explode.php @@ -0,0 +1,25 @@ +=8.1 + return explode($separator, $string ?? '', $limit ?? PHP_INT_MAX); +} diff --git a/libs/plugins/modifier.number_format.php b/libs/plugins/modifier.number_format.php new file mode 100644 index 000000000..8c612601f --- /dev/null +++ b/libs/plugins/modifier.number_format.php @@ -0,0 +1,26 @@ +=8.1 + return number_format($num ?? 0.0, $decimals, $decimal_separator, $thousands_separator); +} diff --git a/libs/plugins/shared.mb_str_replace.php b/libs/plugins/shared.mb_str_replace.php index 226d9035d..4a682114f 100644 --- a/libs/plugins/shared.mb_str_replace.php +++ b/libs/plugins/shared.mb_str_replace.php @@ -44,7 +44,7 @@ function smarty_mb_str_replace($search, $replace, $subject, &$count = 0) } } } else { - $parts = mb_split(preg_quote($search), $subject) ?: array(); + $parts = mb_split(preg_quote($search), $subject ?? "") ?: array(); $count = count($parts) - 1; $subject = implode($replace, $parts); } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php new file mode 100644 index 000000000..67e23c45c --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php @@ -0,0 +1,56 @@ +setUpSmarty(__DIR__); + } + + /** + * @return void + * @throws \SmartyException + * + * @dataProvider explodeDataProvider + */ + public function testExplode($template, $subject, $expectedString) + { + $this->smarty->setDebugging(true); + $this->smarty->assign('subject', $subject); + + $tpl = $this->smarty->createTemplate($template); + $res = $this->smarty->fetch($tpl); + + $this->assertEquals($expectedString, $res); + } + + public function explodeDataProvider() + { + return [ + 'default' => [ + 'template' => 'string:{","|explode:$subject|json_encode}', + 'subject' => 'a,b,c,d', + 'expectedString' => '["a","b","c","d"]', + ], + 'withNoDelimiterFound' => [ + 'template' => 'string:{","|explode:$subject|json_encode}', + 'subject' => 'abcd', + 'expectedString' => '["abcd"]', + ], + 'withNull' => [ + 'template' => 'string:{","|explode:$subject|json_encode}', + 'subject' => null, + 'expectedString' => '[""]', + ], + ]; + } +} diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php new file mode 100644 index 000000000..d7b7c1ce2 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php @@ -0,0 +1,60 @@ +setUpSmarty(__DIR__); + } + + /** + * @return void + * @throws \SmartyException + * + * @dataProvider numberFormatDataProvider + */ + public function testNumberFormat($template, $subject, $expectedString) + { + $this->smarty->setDebugging(true); + $this->smarty->assign('subject', $subject); + + $tpl = $this->smarty->createTemplate($template); + + $this->assertEquals($expectedString, $this->smarty->fetch($tpl)); + } + + public function numberFormatDataProvider() + { + return [ + 'default' => [ + 'template' => 'string:{$subject|number_format}', + 'subject' => 12345, + 'expectedString' => "12,345", + ], + 'withDecimalDefault' => [ + 'template' => 'string:{$subject|number_format}', + 'subject' => 12345.6789, + 'expectedString' => "12,346", + ], + 'withDecimalAndExtras' => [ + 'template' => 'string:{$subject|number_format:2:\'-\':\'~\'}', + 'subject' => 12345.6789, + 'expectedString' => "12~345-68", + ], + 'withNull' => [ + 'template' => 'string:{$subject|number_format}', + 'subject' => null, + 'expectedString' => 0, + ], + ]; + } +} diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php new file mode 100644 index 000000000..a4b6a12e7 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php @@ -0,0 +1,54 @@ +setUpSmarty(__DIR__); + } + + /** + * @return void + * @throws \SmartyException + * + * @dataProvider replaceDataProvider + */ + public function testReplace($template, $subject, $expectedString) + { + $this->smarty->assign('subject', $subject); + + $tpl = $this->smarty->createTemplate($template); + + $this->assertEquals($expectedString, $this->smarty->fetch($tpl)); + } + + public function replaceDataProvider() + { + return [ + 'default' => [ + 'template' => 'string:{$subject|replace:",":"-"}', + 'subject' => "a,b,c,d", + 'expectedString' => "a-b-c-d", + ], + 'doNothing' => [ + 'template' => 'string:{$subject|replace:"":""}', + 'subject' => "a,b,c,d", + 'expectedString' => "a,b,c,d", + ], + 'withNull' => [ + 'template' => 'string:{$subject|replace:"":""}', + 'subject' => null, + 'expectedString' => "", + ], + ]; + } +}