From 119330c56f764d65d1f69cf7fc4b2a9a62561ba2 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 17 Jan 2025 22:51:06 +0800 Subject: [PATCH] refactor: Use `strtolower` with `str_contains`/`str_**_with` as replacement for `stripos` (#9414) * Use `modernize_stripos` option * Fix impossible conditional in test --- .php-cs-fixer.dist.php | 12 +----------- system/Database/Forge.php | 2 +- system/Database/OCI8/Forge.php | 2 +- system/Database/Postgre/Forge.php | 2 +- system/Database/SQLSRV/Forge.php | 4 ++-- system/Database/SQLite3/Forge.php | 2 +- system/Helpers/form_helper.php | 14 +++++++------- system/Test/CIUnitTestCase.php | 2 +- system/Test/Fabricator.php | 4 ++-- tests/system/Cache/CacheFactoryTest.php | 4 ++-- .../method.alreadyNarrowedType.neon | 7 +------ 11 files changed, 20 insertions(+), 35 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index c9c24c7a06..e9240c5909 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -40,17 +40,7 @@ $finder = Finder::create() ]); $overrides = [ - 'get_class_to_class_keyword' => true, - 'trailing_comma_in_multiline' => [ - 'after_heredoc' => true, - 'elements' => [ - 'arguments', - 'array_destructuring', - 'arrays', - 'match', - 'parameters', - ], - ], + 'modernize_strpos' => ['modernize_stripos' => true], ]; $options = [ diff --git a/system/Database/Forge.php b/system/Database/Forge.php index 83cf1b88d9..6bf5748189 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -1061,7 +1061,7 @@ class Forge protected function _attributeAutoIncrement(array &$attributes, array &$field) { if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true - && stripos($field['type'], 'int') !== false + && str_contains(strtolower($field['type']), 'int') ) { $field['auto_increment'] = ' AUTO_INCREMENT'; } diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 4b09372d77..f761045d97 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -184,7 +184,7 @@ class Forge extends BaseForge protected function _attributeAutoIncrement(array &$attributes, array &$field) { if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true - && stripos($field['type'], 'NUMBER') !== false + && str_contains(strtolower($field['type']), 'number') && version_compare($this->db->getVersion(), '12.1', '>=') ) { $field['auto_increment'] = ' GENERATED BY DEFAULT ON NULL AS IDENTITY'; diff --git a/system/Database/Postgre/Forge.php b/system/Database/Postgre/Forge.php index 704f3e5760..b0a38c90ed 100644 --- a/system/Database/Postgre/Forge.php +++ b/system/Database/Postgre/Forge.php @@ -154,7 +154,7 @@ class Forge extends BaseForge protected function _attributeType(array &$attributes) { // Reset field lengths for data types that don't support it - if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== false) { + if (isset($attributes['CONSTRAINT']) && str_contains(strtolower($attributes['TYPE']), 'int')) { $attributes['CONSTRAINT'] = null; } diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index f187b9981f..7b1da6e85b 100644 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -360,7 +360,7 @@ class Forge extends BaseForge protected function _attributeType(array &$attributes) { // Reset field lengths for data types that don't support it - if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== false) { + if (isset($attributes['CONSTRAINT']) && str_contains(strtolower($attributes['TYPE']), 'int')) { $attributes['CONSTRAINT'] = null; } @@ -412,7 +412,7 @@ class Forge extends BaseForge */ protected function _attributeAutoIncrement(array &$attributes, array &$field) { - if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true && stripos($field['type'], 'INT') !== false) { + if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true && str_contains(strtolower($field['type']), strtolower('INT'))) { $field['auto_increment'] = ' IDENTITY(1,1)'; } } diff --git a/system/Database/SQLite3/Forge.php b/system/Database/SQLite3/Forge.php index 03120dd7a0..31c2a7ed58 100644 --- a/system/Database/SQLite3/Forge.php +++ b/system/Database/SQLite3/Forge.php @@ -226,7 +226,7 @@ class Forge extends BaseForge if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true - && stripos($field['type'], 'int') !== false + && str_contains(strtolower($field['type']), 'int') ) { $field['type'] = 'INTEGER PRIMARY KEY'; $field['default'] = ''; diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index ae4505c265..7b89bb9ec1 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -49,10 +49,10 @@ if (! function_exists('form_open')) { $attributes = stringify_attributes($attributes); - if (stripos($attributes, 'method=') === false) { + if (! str_contains(strtolower($attributes), 'method=')) { $attributes .= ' method="post"'; } - if (stripos($attributes, 'accept-charset=') === false) { + if (! str_contains(strtolower($attributes), 'accept-charset=')) { $config = config(App::class); $attributes .= ' accept-charset="' . strtolower($config->charset) . '"'; } @@ -62,7 +62,7 @@ if (! function_exists('form_open')) { // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites $before = service('filters')->getFilters()['before']; - if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && stripos($form, 'method="get"') === false) { + if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && ! str_contains(strtolower($form), strtolower('method="get"'))) { $form .= csrf_field($csrfId ?? null); } @@ -223,11 +223,11 @@ if (! function_exists('form_textarea')) { } // Unsets default rows and cols if defined in extra field as array or string. - if ((is_array($extra) && array_key_exists('rows', $extra)) || (is_string($extra) && stripos(preg_replace('/\s+/', '', $extra), 'rows=') !== false)) { + if ((is_array($extra) && array_key_exists('rows', $extra)) || (is_string($extra) && str_contains(strtolower(preg_replace('/\s+/', '', $extra)), 'rows='))) { unset($defaults['rows']); } - if ((is_array($extra) && array_key_exists('cols', $extra)) || (is_string($extra) && stripos(preg_replace('/\s+/', '', $extra), 'cols=') !== false)) { + if ((is_array($extra) && array_key_exists('cols', $extra)) || (is_string($extra) && str_contains(strtolower(preg_replace('/\s+/', '', $extra)), 'cols='))) { unset($defaults['cols']); } @@ -248,7 +248,7 @@ if (! function_exists('form_multiselect')) { { $extra = stringify_attributes($extra); - if (stripos($extra, 'multiple') === false) { + if (! str_contains(strtolower($extra), strtolower('multiple'))) { $extra .= ' multiple="multiple"'; } @@ -305,7 +305,7 @@ if (! function_exists('form_dropdown')) { } $extra = stringify_attributes($extra); - $multiple = (count($selected) > 1 && stripos($extra, 'multiple') === false) ? ' multiple="multiple"' : ''; + $multiple = (count($selected) > 1 && ! str_contains(strtolower($extra), 'multiple')) ? ' multiple="multiple"' : ''; $form = '