Skip to content

Commit 119330c

Browse files
authored
refactor: Use strtolower with str_contains/str_**_with as replacement for stripos (#9414)
* Use `modernize_stripos` option * Fix impossible conditional in test
1 parent 512cd92 commit 119330c

File tree

11 files changed

+20
-35
lines changed

11 files changed

+20
-35
lines changed

.php-cs-fixer.dist.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,7 @@
4040
]);
4141

4242
$overrides = [
43-
'get_class_to_class_keyword' => true,
44-
'trailing_comma_in_multiline' => [
45-
'after_heredoc' => true,
46-
'elements' => [
47-
'arguments',
48-
'array_destructuring',
49-
'arrays',
50-
'match',
51-
'parameters',
52-
],
53-
],
43+
'modernize_strpos' => ['modernize_stripos' => true],
5444
];
5545

5646
$options = [

system/Database/Forge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ protected function _attributeUnique(array &$attributes, array &$field)
10611061
protected function _attributeAutoIncrement(array &$attributes, array &$field)
10621062
{
10631063
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true
1064-
&& stripos($field['type'], 'int') !== false
1064+
&& str_contains(strtolower($field['type']), 'int')
10651065
) {
10661066
$field['auto_increment'] = ' AUTO_INCREMENT';
10671067
}

system/Database/OCI8/Forge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected function _alterTable(string $alterType, string $table, $processedField
184184
protected function _attributeAutoIncrement(array &$attributes, array &$field)
185185
{
186186
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true
187-
&& stripos($field['type'], 'NUMBER') !== false
187+
&& str_contains(strtolower($field['type']), 'number')
188188
&& version_compare($this->db->getVersion(), '12.1', '>=')
189189
) {
190190
$field['auto_increment'] = ' GENERATED BY DEFAULT ON NULL AS IDENTITY';

system/Database/Postgre/Forge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected function _processColumn(array $processedField): string
154154
protected function _attributeType(array &$attributes)
155155
{
156156
// Reset field lengths for data types that don't support it
157-
if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== false) {
157+
if (isset($attributes['CONSTRAINT']) && str_contains(strtolower($attributes['TYPE']), 'int')) {
158158
$attributes['CONSTRAINT'] = null;
159159
}
160160

system/Database/SQLSRV/Forge.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ protected function _processColumn(array $processedField): string
360360
protected function _attributeType(array &$attributes)
361361
{
362362
// Reset field lengths for data types that don't support it
363-
if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== false) {
363+
if (isset($attributes['CONSTRAINT']) && str_contains(strtolower($attributes['TYPE']), 'int')) {
364364
$attributes['CONSTRAINT'] = null;
365365
}
366366

@@ -412,7 +412,7 @@ protected function _attributeType(array &$attributes)
412412
*/
413413
protected function _attributeAutoIncrement(array &$attributes, array &$field)
414414
{
415-
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true && stripos($field['type'], 'INT') !== false) {
415+
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true && str_contains(strtolower($field['type']), strtolower('INT'))) {
416416
$field['auto_increment'] = ' IDENTITY(1,1)';
417417
}
418418
}

system/Database/SQLite3/Forge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected function _attributeAutoIncrement(array &$attributes, array &$field)
226226
if (
227227
! empty($attributes['AUTO_INCREMENT'])
228228
&& $attributes['AUTO_INCREMENT'] === true
229-
&& stripos($field['type'], 'int') !== false
229+
&& str_contains(strtolower($field['type']), 'int')
230230
) {
231231
$field['type'] = 'INTEGER PRIMARY KEY';
232232
$field['default'] = '';

system/Helpers/form_helper.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
4949

5050
$attributes = stringify_attributes($attributes);
5151

52-
if (stripos($attributes, 'method=') === false) {
52+
if (! str_contains(strtolower($attributes), 'method=')) {
5353
$attributes .= ' method="post"';
5454
}
55-
if (stripos($attributes, 'accept-charset=') === false) {
55+
if (! str_contains(strtolower($attributes), 'accept-charset=')) {
5656
$config = config(App::class);
5757
$attributes .= ' accept-charset="' . strtolower($config->charset) . '"';
5858
}
@@ -62,7 +62,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
6262
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
6363
$before = service('filters')->getFilters()['before'];
6464

65-
if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && stripos($form, 'method="get"') === false) {
65+
if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && ! str_contains(strtolower($form), strtolower('method="get"'))) {
6666
$form .= csrf_field($csrfId ?? null);
6767
}
6868

@@ -223,11 +223,11 @@ function form_textarea($data = '', string $value = '', $extra = ''): string
223223
}
224224

225225
// Unsets default rows and cols if defined in extra field as array or string.
226-
if ((is_array($extra) && array_key_exists('rows', $extra)) || (is_string($extra) && stripos(preg_replace('/\s+/', '', $extra), 'rows=') !== false)) {
226+
if ((is_array($extra) && array_key_exists('rows', $extra)) || (is_string($extra) && str_contains(strtolower(preg_replace('/\s+/', '', $extra)), 'rows='))) {
227227
unset($defaults['rows']);
228228
}
229229

230-
if ((is_array($extra) && array_key_exists('cols', $extra)) || (is_string($extra) && stripos(preg_replace('/\s+/', '', $extra), 'cols=') !== false)) {
230+
if ((is_array($extra) && array_key_exists('cols', $extra)) || (is_string($extra) && str_contains(strtolower(preg_replace('/\s+/', '', $extra)), 'cols='))) {
231231
unset($defaults['cols']);
232232
}
233233

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

251-
if (stripos($extra, 'multiple') === false) {
251+
if (! str_contains(strtolower($extra), strtolower('multiple'))) {
252252
$extra .= ' multiple="multiple"';
253253
}
254254

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

307307
$extra = stringify_attributes($extra);
308-
$multiple = (count($selected) > 1 && stripos($extra, 'multiple') === false) ? ' multiple="multiple"' : '';
308+
$multiple = (count($selected) > 1 && ! str_contains(strtolower($extra), 'multiple')) ? ' multiple="multiple"' : '';
309309
$form = '<select ' . rtrim(parse_form_attributes($data, $defaults)) . $extra . $multiple . ">\n";
310310

311311
foreach ($options as $key => $val) {

system/Test/CIUnitTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ protected function getHeaderEmitted(string $header, bool $ignoreCase = false, st
516516

517517
foreach (xdebug_get_headers() as $emittedHeader) {
518518
$found = $ignoreCase
519-
? (stripos($emittedHeader, $header) === 0)
519+
? (str_starts_with(strtolower($emittedHeader), strtolower($header)))
520520
: (str_starts_with($emittedHeader, $header));
521521

522522
if ($found) {

system/Test/Fabricator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ protected function guessFormatter($field): string
379379

380380
// Check some common partials
381381
foreach (['email', 'name', 'title', 'text', 'date', 'url'] as $term) {
382-
if (stripos($field, $term) !== false) {
382+
if (str_contains(strtolower($field), strtolower($term))) {
383383
return $term;
384384
}
385385
}
386386

387-
if (stripos($field, 'phone') !== false) {
387+
if (str_contains(strtolower($field), 'phone')) {
388388
return 'phoneNumber';
389389
}
390390

tests/system/Cache/CacheFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public function testHandlesBadHandler(): void
9696

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

99-
if (stripos('win', php_uname()) === 0) {
100-
$this->assertTrue(true); // can't test properly if we are on Windows
99+
if (is_windows()) {
100+
$this->markTestSkipped('Cannot test this properly on Windows.');
101101
} else {
102102
$this->assertInstanceOf(DummyHandler::class, $this->cacheFactory->getHandler($this->config, 'wincache', 'wincache'));
103103
}

utils/phpstan-baseline/method.alreadyNarrowedType.neon

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 17 errors
1+
# total 16 errors
22

33
parameters:
44
ignoreErrors:
@@ -12,11 +12,6 @@ parameters:
1212
count: 2
1313
path: ../../tests/system/CLI/CLITest.php
1414

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

0 commit comments

Comments
 (0)