From fa6890f82abc3bee296ca8ad4733221ea76d3491 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 10 Nov 2022 13:13:46 -0500 Subject: [PATCH] Simplify removing file extension when using basename function The basename function has a native way of removing file extensions, so the use of str_replace or preg_replace is usually unnecessary. --- CRM/Upgrade/Incremental/Base.php | 2 +- mixin/ang-php@1/mixin.php | 2 +- mixin/case-xml@1/mixin.php | 2 +- mixin/theme-php@1/mixin.php | 2 +- .../shimmy/tests/phpunit/E2E/Shimmy/LifecycleTest.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index a52d0aaa25..2152b11c24 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -52,7 +52,7 @@ class CRM_Upgrade_Incremental_Base { $sqlGlob = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'sql', $this->getMajorMinor() . '.*.mysql.tpl']); $sqlFiles = glob($sqlGlob);; foreach ($sqlFiles as $file) { - $revList[] = str_replace('.mysql.tpl', '', basename($file)); + $revList[] = basename($file, '.mysql.tpl'); } $c = new ReflectionClass(static::class); diff --git a/mixin/ang-php@1/mixin.php b/mixin/ang-php@1/mixin.php index c75326b5dd..4a340bb58d 100644 --- a/mixin/ang-php@1/mixin.php +++ b/mixin/ang-php@1/mixin.php @@ -26,7 +26,7 @@ return function ($mixInfo, $bootCache) { $files = (array) glob($mixInfo->getPath('ang/*.ang.php')); foreach ($files as $file) { - $name = preg_replace(':\.ang\.php$:', '', basename($file)); + $name = basename($file, '.ang.php'); $module = include $file; if (empty($module['ext'])) { $module['ext'] = $mixInfo->longName; diff --git a/mixin/case-xml@1/mixin.php b/mixin/case-xml@1/mixin.php index d1d0d6b5ab..34eb9ea069 100644 --- a/mixin/case-xml@1/mixin.php +++ b/mixin/case-xml@1/mixin.php @@ -26,7 +26,7 @@ return function ($mixInfo, $bootCache) { } foreach ((array) glob($mixInfo->getPath('xml/case/*.xml')) as $file) { - $name = preg_replace('/\.xml$/', '', basename($file)); + $name = basename($file, '.xml'); if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) { $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name)); throw new CRM_Core_Exception($errorMessage); diff --git a/mixin/theme-php@1/mixin.php b/mixin/theme-php@1/mixin.php index 726d2187ad..88e7524589 100644 --- a/mixin/theme-php@1/mixin.php +++ b/mixin/theme-php@1/mixin.php @@ -27,7 +27,7 @@ return function ($mixInfo, $bootCache) { foreach ($files as $file) { $themeMeta = include $file; if (empty($themeMeta['name'])) { - $themeMeta['name'] = preg_replace(':\.theme\.php$:', '', basename($file)); + $themeMeta['name'] = basename($file, '.theme.php'); } if (empty($themeMeta['ext'])) { $themeMeta['ext'] = $mixInfo->longName; diff --git a/tests/extensions/shimmy/tests/phpunit/E2E/Shimmy/LifecycleTest.php b/tests/extensions/shimmy/tests/phpunit/E2E/Shimmy/LifecycleTest.php index dc1a7b8bc9..d823af8c32 100644 --- a/tests/extensions/shimmy/tests/phpunit/E2E/Shimmy/LifecycleTest.php +++ b/tests/extensions/shimmy/tests/phpunit/E2E/Shimmy/LifecycleTest.php @@ -28,7 +28,7 @@ class E2E_Shimmy_LifecycleTest extends \PHPUnit\Framework\TestCase implements \C $mixinTestFiles = (array) glob($this->getPath('/tests/mixin/*Test.php')); foreach ($mixinTestFiles as $file) { require_once $file; - $class = '\\Civi\Shimmy\\Mixins\\' . preg_replace(';\.php$;', '', basename($file)); + $class = '\\Civi\Shimmy\\Mixins\\' . basename($file, '.php'); $this->mixinTests[] = new $class(); } } -- 2.25.1