Skip to content

Commit 2137664

Browse files
Merge pull request #18116 from kamil-tekiela/Remove-strlen
Remove strlen when used in a condition
2 parents e8c5eef + dcc912f commit 2137664

7 files changed

Lines changed: 23 additions & 28 deletions

File tree

libraries/classes/Controllers/Server/DatabasesController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use function in_array;
2525
use function mb_strtolower;
2626
use function str_contains;
27-
use function strlen;
2827

2928
/**
3029
* Handles viewing and creating and deleting databases
@@ -214,10 +213,10 @@ private function getDatabases($primaryInfo, $replicaInfo): array
214213
$key = array_search($database['SCHEMA_NAME'], $primaryInfo['Ignore_DB']);
215214
$replication['primary']['is_replicated'] = false;
216215

217-
if (strlen((string) $key) === 0) {
216+
if ((string) $key === '') {
218217
$key = array_search($database['SCHEMA_NAME'], $primaryInfo['Do_DB']);
219218

220-
if (strlen((string) $key) > 0 || count($primaryInfo['Do_DB']) === 0) {
219+
if ((string) $key !== '' || count($primaryInfo['Do_DB']) === 0) {
221220
$replication['primary']['is_replicated'] = true;
222221
}
223222
}
@@ -227,10 +226,10 @@ private function getDatabases($primaryInfo, $replicaInfo): array
227226
$key = array_search($database['SCHEMA_NAME'], $replicaInfo['Ignore_DB']);
228227
$replication['replica']['is_replicated'] = false;
229228

230-
if (strlen((string) $key) === 0) {
229+
if ((string) $key === '') {
231230
$key = array_search($database['SCHEMA_NAME'], $replicaInfo['Do_DB']);
232231

233-
if (strlen((string) $key) > 0 || count($replicaInfo['Do_DB']) === 0) {
232+
if ((string) $key !== '' || count($replicaInfo['Do_DB']) === 0) {
234233
$replication['replica']['is_replicated'] = true;
235234
}
236235
}

libraries/classes/Navigation/NavigationTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function __construct(private $template, private DatabaseInterface $dbi)
216216
*/
217217
private function getNavigationDbPos(): int
218218
{
219-
if (strlen($GLOBALS['db'] ?? '') === 0) {
219+
if (($GLOBALS['db'] ?? '') === '') {
220220
return 0;
221221
}
222222

libraries/classes/Operations.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use function is_string;
2222
use function mb_strtolower;
2323
use function str_replace;
24-
use function strlen;
2524
use function strtolower;
2625
use function urldecode;
2726

@@ -710,7 +709,7 @@ public function getTableAltersArray(
710709
$newRowFormatLower = mb_strtolower($newRowFormat);
711710
if (
712711
$pmaTable->isEngine(['MYISAM', 'ARIA', 'INNODB', 'PBXT'])
713-
&& (strlen($rowFormat) === 0
712+
&& ($rowFormat === ''
714713
|| $newRowFormatLower !== mb_strtolower($rowFormat))
715714
) {
716715
$tableAlters[] = 'ROW_FORMAT = '
@@ -882,14 +881,14 @@ public function moveOrCopyTable($db, $table): Message
882881
* (when there are many databases, no drop-down)
883882
*/
884883
$targetDb = $db;
885-
if (isset($_POST['target_db']) && is_string($_POST['target_db']) && strlen($_POST['target_db']) > 0) {
884+
if (isset($_POST['target_db']) && is_string($_POST['target_db']) && $_POST['target_db'] !== '') {
886885
$targetDb = $_POST['target_db'];
887886
}
888887

889888
/**
890889
* A target table name has been sent to this script -> do the work
891890
*/
892-
if (isset($_POST['new_name']) && is_scalar($_POST['new_name']) && strlen((string) $_POST['new_name']) > 0) {
891+
if (isset($_POST['new_name']) && is_scalar($_POST['new_name']) && (string) $_POST['new_name'] !== '') {
893892
if ($db == $targetDb && $table == $_POST['new_name']) {
894893
if (isset($_POST['submit_move'])) {
895894
$message = Message::error(__('Can\'t move table to same one!'));

libraries/classes/Plugins/Import/ImportCsv.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public function doImport(File|null $importHandle = null): array
614614
* Otherwise, check if user provided the database name in the request,
615615
* if not, set the default name
616616
*/
617-
if (isset($_REQUEST['csv_new_db_name']) && strlen($_REQUEST['csv_new_db_name']) > 0) {
617+
if (isset($_REQUEST['csv_new_db_name']) && (string) $_REQUEST['csv_new_db_name'] !== '') {
618618
$newDb = $_REQUEST['csv_new_db_name'];
619619
} else {
620620
$result = $GLOBALS['dbi']->fetchResult('SHOW DATABASES');
@@ -660,7 +660,7 @@ private function buildErrorsForParams(
660660
$GLOBALS['message'] ??= null;
661661

662662
$param_error = false;
663-
if (strlen($csvTerminated) === 0) {
663+
if ($csvTerminated === '') {
664664
$GLOBALS['message'] = Message::error(
665665
__('Invalid parameter for CSV import: %s')
666666
);
@@ -725,7 +725,7 @@ private function getTableNameFromImport(string $databaseName): string
725725
$importFileName = (string) preg_replace('/[^a-zA-Z0-9_]/', '_', $importFileName);
726726

727727
// get new table name, if user didn't provide one, set the default name
728-
if (isset($_REQUEST['csv_new_tbl_name']) && strlen($_REQUEST['csv_new_tbl_name']) > 0) {
728+
if (isset($_REQUEST['csv_new_tbl_name']) && (string) $_REQUEST['csv_new_tbl_name'] !== '') {
729729
return $_REQUEST['csv_new_tbl_name'];
730730
}
731731

libraries/classes/SqlQueryForm.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use function htmlspecialchars;
2222
use function sprintf;
2323
use function str_contains;
24-
use function strlen;
2524

2625
/**
2726
* PhpMyAdmin\SqlQueryForm class
@@ -67,10 +66,10 @@ public function getHtml(
6766
}
6867
}
6968

70-
if (strlen($db) === 0) {
69+
if ($db === '') {
7170
// prepare for server related
7271
$goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/server/sql') : $GLOBALS['goto'];
73-
} elseif (strlen($table) === 0) {
72+
} elseif ($table === '') {
7473
// prepare for db related
7574
$goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/database/sql') : $GLOBALS['goto'];
7675
} else {
@@ -135,7 +134,7 @@ public function getHtml(
135134
public function init($query): array
136135
{
137136
$columns_list = [];
138-
if (strlen($GLOBALS['db']) === 0) {
137+
if ($GLOBALS['db'] === '') {
139138
// prepare for server related
140139
$legend = sprintf(
141140
__('Run SQL query/queries on server “%s”'),
@@ -145,7 +144,7 @@ public function init($query): array
145144
: $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
146145
)
147146
);
148-
} elseif (strlen($GLOBALS['table']) === 0) {
147+
} elseif ($GLOBALS['table'] === '') {
149148
// prepare for db related
150149
$db = $GLOBALS['db'];
151150
// if you want navigation:

libraries/classes/Table.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
use function str_contains;
5151
use function str_replace;
5252
use function stripos;
53-
use function strlen;
5453
use function strtolower;
5554
use function strtoupper;
5655
use function substr;
@@ -435,7 +434,7 @@ public function getCreateOptions(): array
435434
}
436435

437436
// we need explicit DEFAULT value here (different from '0')
438-
$hasPackKeys = isset($createOptions['pack_keys']) && strlen($createOptions['pack_keys']) > 0;
437+
$hasPackKeys = isset($createOptions['pack_keys']) && $createOptions['pack_keys'] !== '';
439438
$createOptions['pack_keys'] = $hasPackKeys ? $createOptions['pack_keys'] : 'DEFAULT';
440439

441440
return $createOptions;
@@ -485,7 +484,6 @@ public static function generateFieldSpec(
485484
$columnsWithIndex = null,
486485
$oldColumnName = null
487486
): string {
488-
$strLength = strlen($length);
489487
$isTimestamp = mb_stripos($type, 'TIMESTAMP') !== false;
490488

491489
$query = Util::backquote($name) . ' ' . $type;
@@ -498,7 +496,7 @@ public static function generateFieldSpec(
498496
$pattern = '@^(DATE|TINYBLOB|TINYTEXT|BLOB|TEXT|'
499497
. 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN|UUID|JSON)$@i';
500498
if (
501-
$strLength !== 0
499+
$length !== ''
502500
&& ! preg_match($pattern, $type)
503501
&& Compatibility::isIntegersSupportLength($type, $length, $GLOBALS['dbi'])
504502
) {
@@ -511,7 +509,7 @@ public static function generateFieldSpec(
511509
if ($attribute != '') {
512510
$query .= ' ' . $attribute;
513511

514-
if ($isTimestamp && stripos($attribute, 'TIMESTAMP') !== false && $strLength !== 0) {
512+
if ($isTimestamp && stripos($attribute, 'TIMESTAMP') !== false && $length !== '') {
515513
$query .= '(' . $length . ')';
516514
}
517515
}
@@ -592,7 +590,7 @@ public static function generateFieldSpec(
592590
$query .= ' DEFAULT ' . $defaultType;
593591

594592
if (
595-
$strLength !== 0
593+
$length !== ''
596594
&& $isTimestamp
597595
&& $defaultType !== 'NULL' // Not to be added in case of NULL
598596
) {
@@ -971,7 +969,7 @@ public static function moveCopy(
971969

972970
// If the target database is not specified, the operation is taking
973971
// place in the same database.
974-
if (! isset($targetDb) || strlen($targetDb) === 0) {
972+
if (! isset($targetDb) || $targetDb === '') {
975973
$targetDb = $sourceDb;
976974
}
977975

@@ -1381,7 +1379,7 @@ public static function isValidName($tableName, $isBackquoted = false): bool
13811379
return false;
13821380
}
13831381

1384-
if (strlen($tableName) === 0) {
1382+
if ($tableName === '') {
13851383
// zero length
13861384
return false;
13871385
}

psalm-baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11769,11 +11769,11 @@
1176911769
<code>$columnNames</code>
1177011770
</MixedReturnStatement>
1177111771
<PossiblyInvalidArgument>
11772-
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>
11773-
<code><![CDATA[$_REQUEST['csv_new_tbl_name']]]></code>
1177411772
<code>$newDb</code>
1177511773
</PossiblyInvalidArgument>
1177611774
<PossiblyInvalidCast>
11775+
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>
11776+
<code><![CDATA[$_REQUEST['csv_new_tbl_name']]]></code>
1177711777
<code>$newDb</code>
1177811778
</PossiblyInvalidCast>
1177911779
<PossiblyInvalidOperand>

0 commit comments

Comments
 (0)