Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use strtolower with str_contains/str_**_with as replacement for stripos #9414

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@
]);

$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 = [
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ protected function _attributeUnique(array &$attributes, array &$field)
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';
}
Expand Down
2 changes: 1 addition & 1 deletion system/Database/OCI8/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function _alterTable(string $alterType, string $table, $processedField
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';
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Postgre/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function _processColumn(array $processedField): string
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;
}

Expand Down
4 changes: 2 additions & 2 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ protected function _processColumn(array $processedField): string
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;
}

Expand Down Expand Up @@ -412,7 +412,7 @@ protected function _attributeType(array &$attributes)
*/
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)';
}
}
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ 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['type'] = 'INTEGER PRIMARY KEY';
$field['default'] = '';
Expand Down
14 changes: 7 additions & 7 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s

$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) . '"';
}
Expand All @@ -62,7 +62,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
// 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);
}

Expand Down Expand Up @@ -223,11 +223,11 @@ function form_textarea($data = '', string $value = '', $extra = ''): string
}

// 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']);
}

Expand All @@ -248,7 +248,7 @@ function form_multiselect($name = '', array $options = [], array $selected = [],
{
$extra = stringify_attributes($extra);

if (stripos($extra, 'multiple') === false) {
if (! str_contains(strtolower($extra), strtolower('multiple'))) {
$extra .= ' multiple="multiple"';
}

Expand Down Expand Up @@ -305,7 +305,7 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
}

$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 = '<select ' . rtrim(parse_form_attributes($data, $defaults)) . $extra . $multiple . ">\n";

foreach ($options as $key => $val) {
Expand Down
2 changes: 1 addition & 1 deletion system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ protected function getHeaderEmitted(string $header, bool $ignoreCase = false, st

foreach (xdebug_get_headers() as $emittedHeader) {
$found = $ignoreCase
? (stripos($emittedHeader, $header) === 0)
? (str_starts_with(strtolower($emittedHeader), strtolower($header)))
: (str_starts_with($emittedHeader, $header));

if ($found) {
Expand Down
4 changes: 2 additions & 2 deletions system/Test/Fabricator.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ protected function guessFormatter($field): string

// Check some common partials
foreach (['email', 'name', 'title', 'text', 'date', 'url'] as $term) {
if (stripos($field, $term) !== false) {
if (str_contains(strtolower($field), strtolower($term))) {
return $term;
}
}

if (stripos($field, 'phone') !== false) {
if (str_contains(strtolower($field), 'phone')) {
return 'phoneNumber';
}

Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public function testHandlesBadHandler(): void

$this->config->handler = 'dummy';

if (stripos('win', php_uname()) === 0) {
$this->assertTrue(true); // can't test properly if we are on Windows
if (is_windows()) {
$this->markTestSkipped('Cannot test this properly on Windows.');
} else {
$this->assertInstanceOf(DummyHandler::class, $this->cacheFactory->getHandler($this->config, 'wincache', 'wincache'));
}
Expand Down
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/method.alreadyNarrowedType.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 17 errors
# total 16 errors

parameters:
ignoreErrors:
Expand All @@ -12,11 +12,6 @@ parameters:
count: 2
path: ../../tests/system/CLI/CLITest.php

-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
count: 1
path: ../../tests/system/Cache/CacheFactoryTest.php

-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
count: 1
Expand Down
Loading