From 7a72d35e1a999b1ed3e091fe6b7457ef93d10d52 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Wed, 24 Jan 2018 07:03:08 +1000 Subject: [PATCH] Issue #2935215 by alexpott: Apply coding standards to Drupal 8 PHP code in core/scripts --- core/phpcs.xml.dist | 5 ++ core/scripts/drupal.sh | 6 ++- core/scripts/password-hash.sh | 4 +- core/scripts/run-tests.sh | 85 ++++++++++++++++---------------- core/scripts/update-countries.sh | 20 +++++--- 5 files changed, 66 insertions(+), 54 deletions(-) diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist index 7dcb3ba813ff..ef47c8709e23 100644 --- a/core/phpcs.xml.dist +++ b/core/phpcs.xml.dist @@ -2,6 +2,11 @@ <ruleset name="drupal_core"> <description>Default PHP CodeSniffer configuration for Drupal core.</description> <file>.</file> + <file>scripts/drupal.sh</file> + <file>scripts/password-hash.sh</file> + <file>scripts/rebuild_token_calculator.sh</file> + <file>scripts/run-tests.sh</file> + <file>scripts/update-countries.sh</file> <arg name="extensions" value="inc,install,module,php,profile,test,theme"/> <!--Exclude third party code.--> diff --git a/core/scripts/drupal.sh b/core/scripts/drupal.sh index de8e864b62f1..904c9ce0c292 100755 --- a/core/scripts/drupal.sh +++ b/core/scripts/drupal.sh @@ -2,6 +2,7 @@ <?php /** + * @file * Drupal shell execution script * * Check for your PHP interpreter - on Windows you'll probably have to @@ -11,6 +12,7 @@ * @param path Drupal's absolute root directory in local file system (optional). * @param URI A URI to execute, including HTTP protocol prefix. */ + $script = basename(array_shift($_SERVER['argv'])); if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) { @@ -69,10 +71,10 @@ // toggle verbose mode if (in_array('--verbose', $_SERVER['argv'])) { - $_verbose_mode = true; + $_verbose_mode = TRUE; } else { - $_verbose_mode = false; + $_verbose_mode = FALSE; } // parse invocation arguments diff --git a/core/scripts/password-hash.sh b/core/scripts/password-hash.sh index cf5e9e382468..611ee533084e 100755 --- a/core/scripts/password-hash.sh +++ b/core/scripts/password-hash.sh @@ -2,6 +2,7 @@ <?php /** + * @file * Drupal hash script - to generate a hash from a plaintext password * * @param password1 [password2 [password3 ...]] @@ -64,7 +65,6 @@ $password_hasher = $kernel->getContainer()->get('password'); foreach ($passwords as $password) { - print("\npassword: $password \t\thash: ". $password_hasher->hash($password) ."\n"); + print("\npassword: $password \t\thash: " . $password_hasher->hash($password) . "\n"); } print("\n"); - diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index ae33bb5410f7..5a8ecd99c21d 100644 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -9,8 +9,8 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Timer; use Drupal\Component\Uuid\Php; +use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Database\Database; -use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Test\TestRunnerKernel; @@ -98,9 +98,10 @@ $test_discovery = NULL; try { $test_discovery = \Drupal::service('test_discovery'); - } catch (Exception $e) { + } + catch (Exception $e) { error_log((string) $e); - echo (string)$e; + echo (string) $e; exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION); } // TestDiscovery::findAllClassFiles() gives us a classmap similar to a @@ -113,7 +114,7 @@ } // Output the list of files. else { - foreach(array_values($test_classes) as $test_class) { + foreach (array_values($test_classes) as $test_class) { echo $test_class . "\n"; } } @@ -147,7 +148,7 @@ drupal_set_time_limit(0); simpletest_script_reporter_init(); -$tests_to_run = array(); +$tests_to_run = []; for ($i = 0; $i < $args['repeat']; $i++) { $tests_to_run = array_merge($tests_to_run, $test_list); } @@ -348,7 +349,7 @@ function simpletest_script_help() { */ function simpletest_script_parse_args() { // Set default values. - $args = array( + $args = [ 'script' => '', 'help' => FALSE, 'list' => FALSE, @@ -370,7 +371,7 @@ function simpletest_script_parse_args() { 'verbose' => FALSE, 'keep-results' => FALSE, 'keep-results-table' => FALSE, - 'test_names' => array(), + 'test_names' => [], 'repeat' => 1, 'die-on-fail' => FALSE, 'suppress-deprecations' => FALSE, @@ -380,7 +381,7 @@ function simpletest_script_parse_args() { 'execute-test' => '', 'xml' => '', 'non-html' => FALSE, - ); + ]; // Override with set values. $args['script'] = basename(array_shift($_SERVER['argv'])); @@ -403,7 +404,7 @@ function simpletest_script_parse_args() { $args[$matches[1]] = array_shift($_SERVER['argv']); } // Clear extraneous values. - $args['test_names'] = array(); + $args['test_names'] = []; $count++; } else { @@ -428,7 +429,7 @@ function simpletest_script_parse_args() { if ($args['browser']) { $args['keep-results'] = TRUE; } - return array($args, $count); + return [$args, $count]; } /** @@ -596,13 +597,13 @@ function simpletest_script_setup_database($new = FALSE) { else { $sqlite = DRUPAL_ROOT . '/' . $args['sqlite']; } - $databases['test-runner']['default'] = array( + $databases['test-runner']['default'] = [ 'driver' => 'sqlite', 'database' => $sqlite, - 'prefix' => array( + 'prefix' => [ 'default' => '', - ), - ); + ], + ]; // Create the test runner SQLite database, unless it exists already. if ($new && !file_exists($sqlite)) { if (!is_dir(dirname($sqlite))) { @@ -664,7 +665,7 @@ function simpletest_script_execute_batch($test_classes) { $total_status = SIMPLETEST_SCRIPT_EXIT_SUCCESS; // Multi-process execution. - $children = array(); + $children = []; while (!empty($test_classes) || !empty($children)) { while (count($children) < $args['concurrency']) { if (empty($test_classes)) { @@ -674,7 +675,7 @@ function simpletest_script_execute_batch($test_classes) { try { $test_id = Database::getConnection('default', 'test-runner') ->insert('simpletest_test_id') - ->useDefaults(array('test_id')) + ->useDefaults(['test_id']) ->execute(); } catch (Exception $e) { @@ -686,7 +687,7 @@ function simpletest_script_execute_batch($test_classes) { $test_class = array_shift($test_classes); // Fork a child process. $command = simpletest_script_command($test_id, $test_class); - $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE)); + $process = proc_open($command, [], $pipes, NULL, NULL, ['bypass_shell' => TRUE]); if (!is_resource($process)) { echo "Unable to fork test process. Aborting.\n"; @@ -694,12 +695,12 @@ function simpletest_script_execute_batch($test_classes) { } // Register our new child. - $children[] = array( + $children[] = [ 'process' => $process, 'test_id' => $test_id, 'class' => $test_class, 'pipes' => $pipes, - ); + ]; } // Wait for children every 200ms. @@ -760,7 +761,7 @@ function simpletest_script_run_phpunit($test_id, $class) { set_time_limit($reflection->getStaticPropertyValue('runLimit')); } - $results = simpletest_run_phpunit_tests($test_id, array($class), $status); + $results = simpletest_run_phpunit_tests($test_id, [$class], $status); simpletest_process_phpunit_results($results); // Map phpunit results to a data structure we can pass to @@ -786,7 +787,7 @@ function simpletest_script_run_one_test($test_id, $test_class) { else { $class_name = $test_class; // Use empty array to run all the test methods. - $methods = array(); + $methods = []; } $test = new $class_name($test_id); if ($args['suppress-deprecations']) { @@ -849,7 +850,7 @@ function simpletest_script_command($test_id, $test_class) { } $command .= ' --php ' . escapeshellarg($php); $command .= " --test-id $test_id"; - foreach (array('verbose', 'keep-results', 'color', 'die-on-fail', 'suppress-deprecations') as $arg) { + foreach (['verbose', 'keep-results', 'color', 'die-on-fail', 'suppress-deprecations'] as $arg) { if ($args[$arg]) { $command .= ' --' . $arg; } @@ -902,7 +903,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) { // Do not output verbose cleanup messages in case of a positive exitcode. $output = !empty($exitcode); - $messages = array(); + $messages = []; $messages[] = "- Found database prefix '$db_prefix' for test ID $test_id."; @@ -931,7 +932,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) { // simpletest_clean_temporary_directories() cannot be used here, since it // would also delete file directories of other tests that are potentially // running concurrently. - file_unmanaged_delete_recursive($test_directory, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback')); + file_unmanaged_delete_recursive($test_directory, ['Drupal\simpletest\TestBase', 'filePreDeleteCallback']); $messages[] = "- Removed test site directory."; } @@ -972,7 +973,7 @@ function simpletest_script_get_test_list() { global $args; $types_processed = empty($args['types']); - $test_list = array(); + $test_list = []; if ($args['all'] || $args['module']) { try { $groups = simpletest_test_get_all($args['module'], $args['types']); @@ -982,7 +983,7 @@ function simpletest_script_get_test_list() { echo (string) $e; exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION); } - $all_tests = array(); + $all_tests = []; foreach ($groups as $group => $tests) { $all_tests = array_merge($all_tests, array_keys($tests)); } @@ -990,7 +991,7 @@ function simpletest_script_get_test_list() { } else { if ($args['class']) { - $test_list = array(); + $test_list = []; foreach ($args['test_names'] as $test_class) { list($class_name) = explode('::', $test_class, 2); if (class_exists($class_name)) { @@ -1004,7 +1005,7 @@ function simpletest_script_get_test_list() { echo (string) $e; exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION); } - $all_classes = array(); + $all_classes = []; foreach ($groups as $group) { $all_classes = array_merge($all_classes, array_keys($group)); } @@ -1051,7 +1052,7 @@ function simpletest_script_get_test_list() { // minimal conditions only; i.e., a '*.php' file that has '/Tests/' in // its path. // Ignore anything from third party vendors. - $ignore = array('.', '..', 'vendor'); + $ignore = ['.', '..', 'vendor']; $files = []; if ($args['directory'][0] === '/') { $directory = $args['directory']; @@ -1141,11 +1142,11 @@ function simpletest_script_get_test_list() { function simpletest_script_reporter_init() { global $args, $test_list, $results_map; - $results_map = array( + $results_map = [ 'pass' => 'Pass', 'fail' => 'Fail', 'exception' => 'Exception', - ); + ]; echo "\n"; echo "Drupal test run\n"; @@ -1186,13 +1187,13 @@ function simpletest_script_reporter_display_summary($class, $results) { // Output all test results vertically aligned. // Cut off the class name after 60 chars, and pad each group with 3 digits // by default (more than 999 assertions are rare). - $output = vsprintf('%-60.60s %10s %9s %14s %12s', array( + $output = vsprintf('%-60.60s %10s %9s %14s %12s', [ $class, $results['#pass'] . ' passes', !$results['#fail'] ? '' : $results['#fail'] . ' fails', !$results['#exception'] ? '' : $results['#exception'] . ' exceptions', !$results['#debug'] ? '' : $results['#debug'] . ' messages', - )); + ]); $status = ($results['#fail'] || $results['#exception'] ? 'fail' : 'pass'); simpletest_script_print($output . "\n", simpletest_script_color_code($status)); @@ -1213,7 +1214,7 @@ function simpletest_script_reporter_write_xml_results() { } $test_class = ''; - $xml_files = array(); + $xml_files = []; foreach ($results as $result) { if (isset($results_map[$result->status])) { @@ -1229,7 +1230,7 @@ function simpletest_script_reporter_write_xml_results() { $doc = new DomDocument('1.0'); $root = $doc->createElement('testsuite'); $root = $doc->appendChild($root); - $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root); + $xml_files[$test_class] = ['doc' => $doc, 'suite' => $root]; } } @@ -1427,7 +1428,7 @@ function simpletest_script_color_code($status) { * @see http://php.net/manual/function.levenshtein.php */ function simpletest_script_print_alternatives($string, $array, $degree = 4) { - $alternatives = array(); + $alternatives = []; foreach ($array as $item) { $lev = levenshtein($string, $item); if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) { @@ -1455,7 +1456,7 @@ function simpletest_script_print_alternatives($string, $array, $degree = 4) { */ function simpletest_script_load_messages_by_test_id($test_ids) { global $args; - $results = array(); + $results = []; // Sqlite has a maximum number of variables per query. If required, the // database query is split into chunks. @@ -1463,15 +1464,15 @@ function simpletest_script_load_messages_by_test_id($test_ids) { $test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT); } else { - $test_id_chunks = array($test_ids); + $test_id_chunks = [$test_ids]; } foreach ($test_id_chunks as $test_id_chunk) { try { $result_chunk = Database::getConnection('default', 'test-runner') - ->query("SELECT * FROM {simpletest} WHERE test_id IN ( :test_ids[] ) ORDER BY test_class, message_id", array( + ->query("SELECT * FROM {simpletest} WHERE test_id IN ( :test_ids[] ) ORDER BY test_class, message_id", [ ':test_ids[]' => $test_id_chunk, - ))->fetchAll(); + ])->fetchAll(); } catch (Exception $e) { echo (string) $e; @@ -1507,12 +1508,12 @@ function simpletest_script_open_browser() { } // Get the results form. - $form = array(); + $form = []; SimpletestResultsForm::addResultForm($form, $results); // Get the assets to make the details element collapsible and theme the result // form. - $assets = new \Drupal\Core\Asset\AttachedAssets(); + $assets = new AttachedAssets(); $assets->setLibraries([ 'core/drupal.collapse', 'system/admin', diff --git a/core/scripts/update-countries.sh b/core/scripts/update-countries.sh index 6a8921d5fc94..abcc6994d45c 100755 --- a/core/scripts/update-countries.sh +++ b/core/scripts/update-countries.sh @@ -9,6 +9,8 @@ * and in the right human-readable format. */ +use Drupal\Core\Locale\CountryManager; + // Determine DRUPAL_ROOT. $dir = dirname(__FILE__); while (!defined('DRUPAL_ROOT')) { @@ -45,7 +47,7 @@ function t($string) { // @see https://www.drupal.org/node/1436754 require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManagerInterface.php'; require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php'; -$existing_countries = \Drupal\Core\Locale\CountryManager::getStandardList(); +$existing_countries = CountryManager::getStandardList(); $countries = $existing_countries; // Parse the source data into an array. @@ -53,19 +55,21 @@ function t($string) { foreach ($data->main->en->localeDisplayNames->territories as $code => $name) { // Use any alternate codes the Drupal community wishes to. - $alt_codes = array( + $alt_codes = [ // 'CI-alt-variant', // Use CI-alt-variant instead of the CI entry. - ); + ]; if (in_array($code, $alt_codes)) { // Just use the first 2 character part of the alt code. $code = strtok($code, '-'); } // Skip any codes we wish to exclude from our country list. - $exclude_codes = array( - 'EU', // The European Union is not a country. - 'ZZ', // Don't allow "Unknown Region". - ); + $exclude_codes = [ + // The European Union is not a country. + 'EU', + // Don't allow "Unknown Region". + 'ZZ', + ]; if (in_array($code, $exclude_codes)) { continue; } @@ -95,5 +99,5 @@ function t($string) { // Replace the actual PHP code in standard.inc. $file = DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php'; $content = file_get_contents($file); -$content = preg_replace('/(\$countries = array\(\n)(.+?)(^\s+\);)/ms', '$1' . $out . '$3', $content); +$content = preg_replace('/(\$countries = \[\n)(.+?)(^\s+\];)/ms', '$1' . $out . '$3', $content, -1, $count); file_put_contents($file, $content); -- GitLab