MOBILE-4081 chore: PHP Code smell fixes

main
Pau Ferrer Ocaña 2022-09-01 10:45:49 +02:00
parent 44cbee9683
commit 4d6962043c
8 changed files with 36 additions and 43 deletions

View File

@ -283,7 +283,7 @@ class behat_app extends behat_app_helper {
// Wait until the main page appears. // Wait until the main page appears.
$this->spin( $this->spin(
function($context, $args) { function($context) {
$initialpage = $context->getSession()->getPage()->find('xpath', '//page-core-mainmenu') ?? $initialpage = $context->getSession()->getPage()->find('xpath', '//page-core-mainmenu') ??
$context->getSession()->getPage()->find('xpath', '//page-core-login-change-password') ?? $context->getSession()->getPage()->find('xpath', '//page-core-login-change-password') ??
$context->getSession()->getPage()->find('xpath', '//page-core-user-complete-profile'); $context->getSession()->getPage()->find('xpath', '//page-core-user-complete-profile');
@ -780,7 +780,7 @@ class behat_app extends behat_app_helper {
if (!is_null($urlpattern)) { if (!is_null($urlpattern)) {
$this->getSession()->switchToWindow($windowNames[1]); $this->getSession()->switchToWindow($windowNames[1]);
$windowurl = $this->getSession()->getCurrentUrl(); $windowurl = $this->getSession()->getCurrentUrl();
$windowhaspattern = !!preg_match("/$urlpattern/", $windowurl); $windowhaspattern = (bool)preg_match("/$urlpattern/", $windowurl);
$this->getSession()->switchToWindow($windowNames[0]); $this->getSession()->switchToWindow($windowNames[0]);
if ($not === $windowhaspattern) { if ($not === $windowhaspattern) {
@ -885,6 +885,8 @@ class behat_app extends behat_app_helper {
case 'offline': case 'offline':
$this->runtime_js("network.setForceConnectionMode('none');"); $this->runtime_js("network.setForceConnectionMode('none');");
break; break;
default:
break;
} }
} }

View File

@ -412,12 +412,9 @@ class behat_app_helper extends behat_base {
preg_match_all("/\\$\\{([^:}]+):([^}]+)\\}/", $text, $matches); preg_match_all("/\\$\\{([^:}]+):([^}]+)\\}/", $text, $matches);
foreach ($matches[0] as $index => $match) { foreach ($matches[0] as $index => $match) {
switch ($matches[2][$index]) { if ($matches[2][$index] == 'cmid') {
case 'cmid': $coursemodule = $DB->get_record('course_modules', ['idnumber' => $matches[1][$index]]);
$coursemodule = $DB->get_record('course_modules', ['idnumber' => $matches[1][$index]]); $text = str_replace($match, $coursemodule->id, $text);
$text = str_replace($match, $coursemodule->id, $text);
break;
} }
} }
@ -550,7 +547,7 @@ class behat_app_helper extends behat_base {
if (!empty($successXPath)) { if (!empty($successXPath)) {
// Wait until the page appears. // Wait until the page appears.
$this->spin( $this->spin(
function($context, $args) use ($successXPath) { function($context) use ($successXPath) {
$found = $context->getSession()->getPage()->find('xpath', $successXPath); $found = $context->getSession()->getPage()->find('xpath', $successXPath);
if ($found) { if ($found) {
return true; return true;
@ -597,7 +594,6 @@ class behat_app_helper extends behat_base {
$cmfrom = $cmtable->get_from_sql(); $cmfrom = $cmtable->get_from_sql();
$acttable = new \core\dml\table($activity, 'a', 'a'); $acttable = new \core\dml\table($activity, 'a', 'a');
$actselect = $acttable->get_field_select();
$actfrom = $acttable->get_from_sql(); $actfrom = $acttable->get_from_sql();
$sql = <<<EOF $sql = <<<EOF
@ -662,7 +658,7 @@ EOF;
// Set up relevant tags for each version. // Set up relevant tags for each version.
$usedtags = array_keys($usedtags); $usedtags = array_keys($usedtags);
foreach ($usedtags as $usedtag) { foreach ($usedtags as $usedtag) {
if (!preg_match('~^lms_(from|upto)([0-9]+(?:\.[0-9]+)*)$~', $usedtag, $matches)) { if (!preg_match('~^lms_(from|upto)(\d+(?:\.\d+)*)$~', $usedtag, $matches)) {
// No match, ignore. // No match, ignore.
continue; continue;
} }

View File

@ -78,10 +78,8 @@ class behat_performance extends behat_base {
$spaceindex = strpos($text, ' '); $spaceindex = strpos($text, ' ');
$value = floatval(substr($text, 0, $spaceindex)); $value = floatval(substr($text, 0, $spaceindex));
switch (substr($text, $spaceindex + 1)) { if (substr($text, $spaceindex + 1) == 'seconds') {
case 'seconds': $value *= 1000;
$value *= 1000;
break;
} }
return $value; return $value;
@ -108,6 +106,8 @@ class behat_performance extends behat_base {
return function ($a, $b) { return function ($a, $b) {
return $a === $b; return $a === $b;
}; };
default:
break;
} }
} }

View File

@ -247,13 +247,13 @@ class performance_measure implements behat_app_listener {
* Analyse long tasks. * Analyse long tasks.
*/ */
private function analyseLongTasks(): void { private function analyseLongTasks(): void {
$blocking = 0; $blockingDuration = 0;
foreach ($this->longTasks as $longTask) { foreach ($this->longTasks as $longTask) {
$blocking += $longTask['duration'] - 50; $blockingDuration += $longTask['duration'] - 50;
} }
$this->blocking = $blocking; $this->blocking = $blockingDuration;
} }
/** /**
@ -269,15 +269,15 @@ class performance_measure implements behat_app_listener {
private function analysePerformanceLogs(): void { private function analysePerformanceLogs(): void {
global $CFG; global $CFG;
$scripting = 0; $scriptingDuration = 0;
$styling = 0; $stylingDuration = 0;
$networking = 0; $networkingCount = 0;
$logs = $this->getPerformanceLogs(); $logs = $this->getPerformanceLogs();
foreach ($logs as $log) { foreach ($logs as $log) {
// TODO this should filter by end time as well, but it seems like the timestamps are not // TODO this should filter by end time as well, but it seems like the timestamps are not
// working as expected. // working as expected.
if (($log['timestamp'] < $this->start)) { if ($log['timestamp'] < $this->start) {
continue; continue;
} }
@ -285,27 +285,27 @@ class performance_measure implements behat_app_listener {
$messagename = $message->params->name ?? ''; $messagename = $message->params->name ?? '';
if (in_array($messagename, ['FunctionCall', 'GCEvent', 'MajorGC', 'MinorGC', 'EvaluateScript'])) { if (in_array($messagename, ['FunctionCall', 'GCEvent', 'MajorGC', 'MinorGC', 'EvaluateScript'])) {
$scripting += $message->params->dur; $scriptingDuration += $message->params->dur;
continue; continue;
} }
if (in_array($messagename, ['UpdateLayoutTree', 'RecalculateStyles', 'ParseAuthorStyleSheet'])) { if (in_array($messagename, ['UpdateLayoutTree', 'RecalculateStyles', 'ParseAuthorStyleSheet'])) {
$styling += $message->params->dur; $stylingDuration += $message->params->dur;
continue; continue;
} }
if (in_array($messagename, ['XHRLoad']) && !str_starts_with($message->params->args->data->url, $CFG->behat_ionic_wwwroot)) { if (in_array($messagename, ['XHRLoad']) && !str_starts_with($message->params->args->data->url, $CFG->behat_ionic_wwwroot)) {
$networking++; $networkingCount++;
continue; continue;
} }
} }
$this->scripting = round($scripting / 1000); $this->scripting = round($scriptingDuration / 1000);
$this->styling = round($styling / 1000); $this->styling = round($stylingDuration / 1000);
$this->networking = $networking; $this->networking = $networkingCount;
} }
/** /**

View File

@ -42,7 +42,7 @@ $versions = array('master', '310', '39', '38', '37', '36', '35', '34', '33', '32
$moodlespath = $argv[1]; $moodlespath = $argv[1];
$wsname = $argv[2]; $wsname = $argv[2];
$useparams = !!(isset($argv[3]) && $argv[3]); $useparams = (bool)(isset($argv[3]) && $argv[3]);
$pathseparator = '/'; $pathseparator = '/';
// Get the path to the script. // Get the path to the script.

View File

@ -39,7 +39,7 @@ if (!defined('SERIALIZED')) {
$moodlepath = $argv[1]; $moodlepath = $argv[1];
$wsname = $argv[2]; $wsname = $argv[2];
$useparams = !!(isset($argv[3]) && $argv[3]); $useparams = (bool)(isset($argv[3]) && $argv[3]);
define('CLI_SCRIPT', true); define('CLI_SCRIPT', true);

View File

@ -302,7 +302,7 @@ function build_lang($lang) {
$text = str_replace(['{{{', '}}}'], ['{{', '}}'], $text); $text = str_replace(['{{{', '}}}'], ['{{', '}}'], $text);
} else { } else {
// @TODO: Remove that line when core.cannotconnect and core.login.invalidmoodleversion are completelly changed to use $a // @TODO: Remove that line when core.cannotconnect and core.login.invalidmoodleversion are completelly changed to use $a
if (($appkey == 'core.cannotconnect' || $appkey == 'core.login.invalidmoodleversion') && strpos($text, '2.4') != false) { if (($appkey == 'core.cannotconnect' || $appkey == 'core.login.invalidmoodleversion') && strpos($text, '2.4')) {
$text = str_replace('2.4', '{{$a}}', $text); $text = str_replace('2.4', '{{$a}}', $text);
} }
$local++; $local++;
@ -471,13 +471,10 @@ function override_component_lang_files($translations) {
} }
switch($type) { switch($type) {
case 'core': case 'core':
switch($component) { if ($component == 'moodle') {
case 'moodle': $path .= 'core/lang.json';
$path .= 'core/lang.json'; } else {
break; $path .= 'core/features/'.str_replace('_', '/', $component).'/lang.json';
default:
$path .= 'core/features/'.str_replace('_', '/', $component).'/lang.json';
break;
} }
break; break;
case 'addon': case 'addon':

View File

@ -70,7 +70,7 @@ function fix_comment($desc) {
if (count($lines) > 1) { if (count($lines) > 1) {
$desc = array_shift($lines)."\n"; $desc = array_shift($lines)."\n";
foreach ($lines as $i => $line) { foreach ($lines as $line) {
$spaces = strlen($line) - strlen(ltrim($line)); $spaces = strlen($line) - strlen(ltrim($line));
$desc .= str_repeat(' ', $spaces - 3) . '// '. ltrim($line)."\n"; $desc .= str_repeat(' ', $spaces - 3) . '// '. ltrim($line)."\n";
} }
@ -138,9 +138,7 @@ function convert_to_ts($key, $value, $boolisnumber = false, $indentation = '', $
$type = 'number'; $type = 'number';
} }
$result = convert_key_type($key, $type, $value->required, $indentation); return convert_key_type($key, $type, $value->required, $indentation);
return $result;
} else if ($value instanceof external_single_structure) { } else if ($value instanceof external_single_structure) {
// It's an object. // It's an object.
@ -278,7 +276,7 @@ function remove_default_closures($value) {
} else if ($value instanceof external_single_structure) { } else if ($value instanceof external_single_structure) {
foreach ($value->keys as $key => $subvalue) { foreach ($value->keys as $subvalue) {
remove_default_closures($subvalue); remove_default_closures($subvalue);
} }