From 77aa609bdbf41c0ed8d1b4cccc6a205bf8ff1dc6 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 17 Jan 2026 15:10:00 +0100 Subject: [PATCH] Remove PHP 8+ polyfills and use native string functions - Remove starts_with() and ends_with() helper functions - Replace with native str_starts_with() and str_ends_with() - Use Str::startsWith() for array-needle cases (HtmlBuilder, SyntaxModelTrait) --- init/functions.php | 26 ------------------- src/Auth/Models/Role.php | 12 ++++----- src/Auth/Models/User.php | 12 ++++----- src/Database/Attach/File.php | 2 +- .../Providers/ExecutionContextProvider.php | 2 +- src/Halcyon/Traits/Validation.php | 4 +-- src/Html/HtmlBuilder.php | 3 ++- src/Parse/Syntax/SyntaxModelTrait.php | 3 ++- src/Scaffold/Console/CreateFactory.php | 2 +- src/Scaffold/Console/CreateTest.php | 2 +- 10 files changed, 22 insertions(+), 46 deletions(-) diff --git a/init/functions.php b/init/functions.php index c872ac456..d6a50dd2a 100644 --- a/init/functions.php +++ b/init/functions.php @@ -563,19 +563,6 @@ function camel_case($value) } } -if (!function_exists('ends_with')) { - /** - * ends_with determines if a given string ends with a given substring - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function ends_with($haystack, $needles) - { - return Str::endsWith($haystack, $needles); - } -} - if (!function_exists('kebab_case')) { /** * kebab_case converts a string to kebab case @@ -601,19 +588,6 @@ function snake_case($value, $delimiter = '_') } } -if (!function_exists('starts_with')) { - /** - * starts_with determines if a given string starts with a given substring - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function starts_with($haystack, $needles) - { - return Str::startsWith($haystack, $needles); - } -} - if (!function_exists('str_after')) { /** * str_after returns the remainder of a string after a given value diff --git a/src/Auth/Models/Role.php b/src/Auth/Models/Role.php index fb73d1f00..b9ebd7622 100644 --- a/src/Auth/Models/Role.php +++ b/src/Auth/Models/Role.php @@ -77,7 +77,7 @@ public function hasAccess($permissions, $all = true) // Now, let's check if the permission ends in a wildcard "*" symbol. // If it does, we'll check through all the merged permissions to see // if a permission exists which matches the wildcard. - if ((strlen($permission) > 1) && ends_with($permission, '*')) { + if ((strlen($permission) > 1) && str_ends_with($permission, '*')) { $matched = false; foreach ($rolePermissions as $rolePermission => $value) { @@ -88,7 +88,7 @@ public function hasAccess($permissions, $all = true) // exactly match our permission, but starts with it. if ( $checkPermission !== $rolePermission && - starts_with($rolePermission, $checkPermission) && + str_starts_with($rolePermission, $checkPermission) && (int) $value === 1 ) { $matched = true; @@ -99,7 +99,7 @@ public function hasAccess($permissions, $all = true) // Now, let's check if the permission starts in a wildcard "*" symbol. // If it does, we'll check through all the merged permissions to see // if a permission exists which matches the wildcard. - elseif ((strlen($permission) > 1) && starts_with($permission, '*')) { + elseif ((strlen($permission) > 1) && str_starts_with($permission, '*')) { $matched = false; foreach ($rolePermissions as $rolePermission => $value) { @@ -110,7 +110,7 @@ public function hasAccess($permissions, $all = true) // exactly match our permission, but ends with it. if ( $checkPermission !== $rolePermission && - ends_with($rolePermission, $checkPermission) && + str_ends_with($rolePermission, $checkPermission) && (int) $value === 1 ) { $matched = true; @@ -123,7 +123,7 @@ public function hasAccess($permissions, $all = true) foreach ($rolePermissions as $rolePermission => $value) { // This time check if the rolePermission ends in wildcard "*" symbol. - if ((strlen($rolePermission) > 1) && ends_with($rolePermission, '*')) { + if ((strlen($rolePermission) > 1) && str_ends_with($rolePermission, '*')) { $matched = false; // Strip the '*' off the end of the permission. @@ -133,7 +133,7 @@ public function hasAccess($permissions, $all = true) // exactly match our permission, but starts with it. if ( $checkGroupPermission !== $permission && - starts_with($permission, $checkGroupPermission) && + str_starts_with($permission, $checkGroupPermission) && (int) $value === 1 ) { $matched = true; diff --git a/src/Auth/Models/User.php b/src/Auth/Models/User.php index 2b350aae0..7c7cfb1a9 100644 --- a/src/Auth/Models/User.php +++ b/src/Auth/Models/User.php @@ -542,7 +542,7 @@ public function hasPermission($permissions, $all = true) // Now, let's check if the permission ends in a wildcard "*" symbol. // If it does, we'll check through all the merged permissions to see // if a permission exists which matches the wildcard. - if ((strlen($permission) > 1) && ends_with($permission, '*')) { + if ((strlen($permission) > 1) && str_ends_with($permission, '*')) { $matched = false; foreach ($mergedPermissions as $mergedPermission => $value) { @@ -553,7 +553,7 @@ public function hasPermission($permissions, $all = true) // exactly match our permission, but starts with it. if ( $checkPermission !== $mergedPermission && - starts_with($mergedPermission, $checkPermission) && + str_starts_with($mergedPermission, $checkPermission) && (int) $value === 1 ) { $matched = true; @@ -561,7 +561,7 @@ public function hasPermission($permissions, $all = true) } } } - elseif ((strlen($permission) > 1) && starts_with($permission, '*')) { + elseif ((strlen($permission) > 1) && str_starts_with($permission, '*')) { $matched = false; foreach ($mergedPermissions as $mergedPermission => $value) { @@ -572,7 +572,7 @@ public function hasPermission($permissions, $all = true) // exactly match our permission, but ends with it. if ( $checkPermission !== $mergedPermission && - ends_with($mergedPermission, $checkPermission) && + str_ends_with($mergedPermission, $checkPermission) && (int) $value === 1 ) { $matched = true; @@ -585,7 +585,7 @@ public function hasPermission($permissions, $all = true) foreach ($mergedPermissions as $mergedPermission => $value) { // This time check if the mergedPermission ends in wildcard "*" symbol. - if ((strlen($mergedPermission) > 1) && ends_with($mergedPermission, '*')) { + if ((strlen($mergedPermission) > 1) && str_ends_with($mergedPermission, '*')) { $matched = false; // Strip the '*' off the end of the permission. @@ -595,7 +595,7 @@ public function hasPermission($permissions, $all = true) // exactly match our permission, but starts with it. if ( $checkMergedPermission !== $permission && - starts_with($permission, $checkMergedPermission) && + str_starts_with($permission, $checkMergedPermission) && (int) $value === 1 ) { $matched = true; diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index 3e029014a..ce2d168d7 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -730,7 +730,7 @@ public function deleteThumbs() $allFiles = $this->storageCmd('files', $directory); $collection = []; foreach ($allFiles as $file) { - if (starts_with(basename($file), $pattern)) { + if (str_starts_with(basename($file), $pattern)) { $collection[] = $file; } } diff --git a/src/Foundation/Providers/ExecutionContextProvider.php b/src/Foundation/Providers/ExecutionContextProvider.php index 344f5d3a6..04c9ce56a 100644 --- a/src/Foundation/Providers/ExecutionContextProvider.php +++ b/src/Foundation/Providers/ExecutionContextProvider.php @@ -39,7 +39,7 @@ protected function determineContext($app): string $backendUri = $this->normalizeUrl($app['config']->get('backend.uri', 'backend')); - if (starts_with($requestPath, $backendUri)) { + if (str_starts_with($requestPath, $backendUri)) { return 'backend'; } diff --git a/src/Halcyon/Traits/Validation.php b/src/Halcyon/Traits/Validation.php index 7e890cd7e..7a0c6dfef 100644 --- a/src/Halcyon/Traits/Validation.php +++ b/src/Halcyon/Traits/Validation.php @@ -235,10 +235,10 @@ protected function processValidationRules($rules) // Analyse each rule individually foreach ($ruleParts as $key => $rulePart) { // Look for required:create and required:update rules - if (starts_with($rulePart, 'required:create') && $this->exists) { + if (str_starts_with($rulePart, 'required:create') && $this->exists) { unset($ruleParts[$key]); } - elseif (starts_with($rulePart, 'required:update') && !$this->exists) { + elseif (str_starts_with($rulePart, 'required:update') && !$this->exists) { unset($ruleParts[$key]); } } diff --git a/src/Html/HtmlBuilder.php b/src/Html/HtmlBuilder.php index 0db1f3125..89a08ba1d 100644 --- a/src/Html/HtmlBuilder.php +++ b/src/Html/HtmlBuilder.php @@ -1,5 +1,6 @@ getPath(); } - if (!starts_with($path, ['//', 'http://', 'https://'])) { + if (!Str::startsWith($path, ['//', 'http://', 'https://'])) { $path = Request::getSchemeAndHttpHost() . $path; } diff --git a/src/Scaffold/Console/CreateFactory.php b/src/Scaffold/Console/CreateFactory.php index 078748f01..f3293806f 100644 --- a/src/Scaffold/Console/CreateFactory.php +++ b/src/Scaffold/Console/CreateFactory.php @@ -30,7 +30,7 @@ class CreateFactory extends GeneratorCommandBase */ public function handle() { - if (!ends_with($this->argument('name'), 'Factory')) { + if (!str_ends_with($this->argument('name'), 'Factory')) { $this->components->error('Factory classes names must end in "Factory"'); return; } diff --git a/src/Scaffold/Console/CreateTest.php b/src/Scaffold/Console/CreateTest.php index d956b29aa..ada19a81c 100644 --- a/src/Scaffold/Console/CreateTest.php +++ b/src/Scaffold/Console/CreateTest.php @@ -30,7 +30,7 @@ class CreateTest extends GeneratorCommandBase */ public function handle() { - if (!ends_with($this->argument('name'), 'Test')) { + if (!str_ends_with($this->argument('name'), 'Test')) { $this->components->error('Test classes names must end in "Test"'); return; }