Skip to content

Commit

Permalink
Remove some else conditions by using early exits or moving code
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Apr 28, 2021
1 parent db01d6a commit d005ea8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 70 deletions.
66 changes: 34 additions & 32 deletions libraries/classes/Config/ServerConfigChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,12 @@ protected function performConfigChecksServersZipdump()
* @param bool $cookieAuthUsed Cookie auth is used
* @param bool $blowfishSecretSet Blowfish secret set
* @param string $blowfishSecret Blowfish secret
*
* @return void
*/
protected function performConfigChecksCookieAuthUsed(
$cookieAuthUsed,
$blowfishSecretSet,
$blowfishSecret
) {
): void {
// $cfg['blowfish_secret']
// it's required for 'cookie' authentication
if (! $cookieAuthUsed) {
Expand All @@ -375,38 +373,42 @@ protected function performConfigChecksCookieAuthUsed(
. 'remember it.'
))
);
} else {
$blowfishWarnings = [];
// check length
if (strlen($blowfishSecret) < 32) {
// too short key
$blowfishWarnings[] = __(
'Key is too short, it should have at least 32 characters.'
);
}

// check used characters
$hasDigits = (bool) preg_match('/\d/', $blowfishSecret);
$hasChars = (bool) preg_match('/\S/', $blowfishSecret);
$hasNonword = (bool) preg_match('/\W/', $blowfishSecret);
if (! $hasDigits || ! $hasChars || ! $hasNonword) {
$blowfishWarnings[] = Sanitize::sanitizeMessage(
__(
'Key should contain letters, numbers [em]and[/em] '
. 'special characters.'
)
);
}
return;
}

if (! empty($blowfishWarnings)) {
SetupIndex::messagesSet(
'error',
'blowfish_warnings' . count($blowfishWarnings),
Descriptions::get('blowfish_secret'),
implode('<br>', $blowfishWarnings)
);
}
$blowfishWarnings = [];
// check length
if (strlen($blowfishSecret) < 32) {
// too short key
$blowfishWarnings[] = __(
'Key is too short, it should have at least 32 characters.'
);
}

// check used characters
$hasDigits = (bool) preg_match('/\d/', $blowfishSecret);
$hasChars = (bool) preg_match('/\S/', $blowfishSecret);
$hasNonword = (bool) preg_match('/\W/', $blowfishSecret);
if (! $hasDigits || ! $hasChars || ! $hasNonword) {
$blowfishWarnings[] = Sanitize::sanitizeMessage(
__(
'Key should contain letters, numbers [em]and[/em] '
. 'special characters.'
)
);
}

if (empty($blowfishWarnings)) {
return;
}

SetupIndex::messagesSet(
'error',
'blowfish_warnings' . count($blowfishWarnings),
Descriptions::get('blowfish_secret'),
implode('<br>', $blowfishWarnings)
);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions libraries/classes/Controllers/LicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ public function index(): void
// Check if the file is available, some distributions remove these.
if (@is_readable($filename)) {
readfile($filename);
} else {
printf(
__(
'The %s file is not available on this system, please visit ' .
'%s for more information.'
),
$filename,
'https://www.phpmyadmin.net/'
);

return;
}

printf(
__(
'The %s file is not available on this system, please visit ' .
'%s for more information.'
),
$filename,
'https://www.phpmyadmin.net/'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ public function index(): void

if ($this->response->isAjax()) {
$this->response->addJSON('disableNaviSettings', true);
} else {
define('PMA_DISABLE_NAVI_SETTINGS', true);

return;
}

define('PMA_DISABLE_NAVI_SETTINGS', true);
}
}
4 changes: 2 additions & 2 deletions libraries/classes/Plugins/Import/Upload/UploadProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public static function getUploadStatus($id)
}

if ($status) {
$ret['finished'] = false;

if ($status['bytes_uploaded'] == $status['bytes_total']) {
$ret['finished'] = true;
} else {
$ret['finished'] = false;
}

$ret['total'] = $status['bytes_total'];
Expand Down
6 changes: 3 additions & 3 deletions libraries/classes/Plugins/ImportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ abstract protected function setProperties();
*/
protected function getDbnameAndOptions($currentDb, $defaultDb)
{
$db_name = $defaultDb;
$options = null;

if (strlen((string) $currentDb) > 0) {
$db_name = $currentDb;
$options = ['create_db' => false];
} else {
$db_name = $defaultDb;
$options = null;
}

return [
Expand Down
4 changes: 2 additions & 2 deletions libraries/classes/Plugins/Schema/Dia/Dia.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function startDiaDoc(
$rightMargin,
$orientation
) {
$isPortrait = 'false';

if ($orientation === 'P') {
$isPortrait = 'true';
} else {
$isPortrait = 'false';
}

$this->startElement('dia:diagram');
Expand Down
10 changes: 6 additions & 4 deletions libraries/classes/Server/SysInfo/WindowsNt.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function __construct()
{
if (! class_exists('COM')) {
$this->wmi = null;
} else {
// initialize the wmi object
$objLocator = new COM('WbemScripting.SWbemLocator');
$this->wmi = $objLocator->ConnectServer();

return;
}

// initialize the wmi object
$objLocator = new COM('WbemScripting.SWbemLocator');
$this->wmi = $objLocator->ConnectServer();
}

/**
Expand Down
32 changes: 16 additions & 16 deletions libraries/classes/Setup/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ private static function getVarExport($var_name, $var_value, $crlf)
. var_export($var_value, true) . ';' . $crlf;
}

$ret = '';
if (self::isZeroBasedArray($var_value)) {
$ret = "\$cfg['" . $var_name . "'] = "
return "\$cfg['" . $var_name . "'] = "
. self::exportZeroBasedArray($var_value, $crlf)
. ';' . $crlf;
} else {
// string keys: $cfg[key][subkey] = value
foreach ($var_value as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['" . $var_name . "']['" . $k . "'] = "
. var_export($v, true) . ';' . $crlf;
}
}

$ret = '';
// string keys: $cfg[key][subkey] = value
foreach ($var_value as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['" . $var_name . "']['" . $k . "'] = "
. var_export($v, true) . ';' . $crlf;
}

return $ret;
Expand Down Expand Up @@ -152,13 +152,13 @@ private static function exportZeroBasedArray(array $array, $crlf)
$ret = 'array(';
if (count($retv) <= 4) {
// up to 4 values - one line
$ret .= implode(', ', $retv);
} else {
// more than 4 values - value per line
$imax = count($retv);
for ($i = 0; $i < $imax; $i++) {
$ret .= ($i > 0 ? ',' : '') . $crlf . ' ' . $retv[$i];
}
return $ret . implode(', ', $retv) . ')';
}

// more than 4 values - value per line
$imax = count($retv);
for ($i = 0; $i < $imax; $i++) {
$ret .= ($i > 0 ? ',' : '') . $crlf . ' ' . $retv[$i];
}

return $ret . ')';
Expand Down

0 comments on commit d005ea8

Please sign in to comment.