Skip to content

Commit

Permalink
Remove strlen when used in a condition
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
  • Loading branch information
kamil-tekiela committed Feb 24, 2023
1 parent 4d9df42 commit dcc912f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 28 deletions.
9 changes: 4 additions & 5 deletions libraries/classes/Controllers/Server/DatabasesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use function in_array;
use function mb_strtolower;
use function str_contains;
use function strlen;

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

if (strlen((string) $key) === 0) {
if ((string) $key === '') {
$key = array_search($database['SCHEMA_NAME'], $primaryInfo['Do_DB']);

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

if (strlen((string) $key) === 0) {
if ((string) $key === '') {
$key = array_search($database['SCHEMA_NAME'], $replicaInfo['Do_DB']);

if (strlen((string) $key) > 0 || count($replicaInfo['Do_DB']) === 0) {
if ((string) $key !== '' || count($replicaInfo['Do_DB']) === 0) {
$replication['replica']['is_replicated'] = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Navigation/NavigationTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function __construct(private $template, private DatabaseInterface $dbi)
*/
private function getNavigationDbPos(): int
{
if (strlen($GLOBALS['db'] ?? '') === 0) {
if (($GLOBALS['db'] ?? '') === '') {
return 0;
}

Expand Down
7 changes: 3 additions & 4 deletions libraries/classes/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function is_string;
use function mb_strtolower;
use function str_replace;
use function strlen;
use function strtolower;
use function urldecode;

Expand Down Expand Up @@ -710,7 +709,7 @@ public function getTableAltersArray(
$newRowFormatLower = mb_strtolower($newRowFormat);
if (
$pmaTable->isEngine(['MYISAM', 'ARIA', 'INNODB', 'PBXT'])
&& (strlen($rowFormat) === 0
&& ($rowFormat === ''
|| $newRowFormatLower !== mb_strtolower($rowFormat))
) {
$tableAlters[] = 'ROW_FORMAT = '
Expand Down Expand Up @@ -882,14 +881,14 @@ public function moveOrCopyTable($db, $table): Message
* (when there are many databases, no drop-down)
*/
$targetDb = $db;
if (isset($_POST['target_db']) && is_string($_POST['target_db']) && strlen($_POST['target_db']) > 0) {
if (isset($_POST['target_db']) && is_string($_POST['target_db']) && $_POST['target_db'] !== '') {
$targetDb = $_POST['target_db'];
}

/**
* A target table name has been sent to this script -> do the work
*/
if (isset($_POST['new_name']) && is_scalar($_POST['new_name']) && strlen((string) $_POST['new_name']) > 0) {
if (isset($_POST['new_name']) && is_scalar($_POST['new_name']) && (string) $_POST['new_name'] !== '') {
if ($db == $targetDb && $table == $_POST['new_name']) {
if (isset($_POST['submit_move'])) {
$message = Message::error(__('Can\'t move table to same one!'));
Expand Down
6 changes: 3 additions & 3 deletions libraries/classes/Plugins/Import/ImportCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public function doImport(File|null $importHandle = null): array
* Otherwise, check if user provided the database name in the request,
* if not, set the default name
*/
if (isset($_REQUEST['csv_new_db_name']) && strlen($_REQUEST['csv_new_db_name']) > 0) {
if (isset($_REQUEST['csv_new_db_name']) && (string) $_REQUEST['csv_new_db_name'] !== '') {
$newDb = $_REQUEST['csv_new_db_name'];
} else {
$result = $GLOBALS['dbi']->fetchResult('SHOW DATABASES');
Expand Down Expand Up @@ -660,7 +660,7 @@ private function buildErrorsForParams(
$GLOBALS['message'] ??= null;

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

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

Expand Down
9 changes: 4 additions & 5 deletions libraries/classes/SqlQueryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function htmlspecialchars;
use function sprintf;
use function str_contains;
use function strlen;

/**
* PhpMyAdmin\SqlQueryForm class
Expand Down Expand Up @@ -67,10 +66,10 @@ public function getHtml(
}
}

if (strlen($db) === 0) {
if ($db === '') {
// prepare for server related
$goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/server/sql') : $GLOBALS['goto'];
} elseif (strlen($table) === 0) {
} elseif ($table === '') {
// prepare for db related
$goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/database/sql') : $GLOBALS['goto'];
} else {
Expand Down Expand Up @@ -135,7 +134,7 @@ public function getHtml(
public function init($query): array
{
$columns_list = [];
if (strlen($GLOBALS['db']) === 0) {
if ($GLOBALS['db'] === '') {
// prepare for server related
$legend = sprintf(
__('Run SQL query/queries on server “%s”'),
Expand All @@ -145,7 +144,7 @@ public function init($query): array
: $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
)
);
} elseif (strlen($GLOBALS['table']) === 0) {
} elseif ($GLOBALS['table'] === '') {
// prepare for db related
$db = $GLOBALS['db'];
// if you want navigation:
Expand Down
14 changes: 6 additions & 8 deletions libraries/classes/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
use function str_contains;
use function str_replace;
use function stripos;
use function strlen;
use function strtolower;
use function strtoupper;
use function substr;
Expand Down Expand Up @@ -435,7 +434,7 @@ public function getCreateOptions(): array
}

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

return $createOptions;
Expand Down Expand Up @@ -485,7 +484,6 @@ public static function generateFieldSpec(
$columnsWithIndex = null,
$oldColumnName = null
): string {
$strLength = strlen($length);
$isTimestamp = mb_stripos($type, 'TIMESTAMP') !== false;

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

if ($isTimestamp && stripos($attribute, 'TIMESTAMP') !== false && $strLength !== 0) {
if ($isTimestamp && stripos($attribute, 'TIMESTAMP') !== false && $length !== '') {
$query .= '(' . $length . ')';
}
}
Expand Down Expand Up @@ -592,7 +590,7 @@ public static function generateFieldSpec(
$query .= ' DEFAULT ' . $defaultType;

if (
$strLength !== 0
$length !== ''
&& $isTimestamp
&& $defaultType !== 'NULL' // Not to be added in case of NULL
) {
Expand Down Expand Up @@ -971,7 +969,7 @@ public static function moveCopy(

// If the target database is not specified, the operation is taking
// place in the same database.
if (! isset($targetDb) || strlen($targetDb) === 0) {
if (! isset($targetDb) || $targetDb === '') {
$targetDb = $sourceDb;
}

Expand Down Expand Up @@ -1381,7 +1379,7 @@ public static function isValidName($tableName, $isBackquoted = false): bool
return false;
}

if (strlen($tableName) === 0) {
if ($tableName === '') {
// zero length
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11772,11 +11772,11 @@
<code>$columnNames</code>
</MixedReturnStatement>
<PossiblyInvalidArgument>
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>
<code><![CDATA[$_REQUEST['csv_new_tbl_name']]]></code>
<code>$newDb</code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>
<code><![CDATA[$_REQUEST['csv_new_tbl_name']]]></code>
<code>$newDb</code>
</PossiblyInvalidCast>
<PossiblyInvalidOperand>
Expand Down

0 comments on commit dcc912f

Please sign in to comment.