From adc62c8c77230e041d8ddbd8857fcba3cf2b980a Mon Sep 17 00:00:00 2001 From: Laetitia Fesselier Date: Tue, 21 Jul 2020 15:05:38 -0400 Subject: [PATCH] [Tools] Enable PHPCS for manage_modules.php (#6787) See also #5977 --- test/run-php-linter.sh | 1 + tools/manage_modules.php | 46 ++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/test/run-php-linter.sh b/test/run-php-linter.sh index 3a75f548eb6..26709e13cd6 100755 --- a/test/run-php-linter.sh +++ b/test/run-php-linter.sh @@ -42,6 +42,7 @@ declare -a tools_list=( 'generate_tables_sql.php' 'lorisform_parser.php' 'populate_visit_windows.php' + 'manage_modules.php' ) vendor/bin/phpcs --standard=test/LorisCS.xml --extensions=php,inc \ diff --git a/tools/manage_modules.php b/tools/manage_modules.php index d47f4d6ec95..911d265fc84 100755 --- a/tools/manage_modules.php +++ b/tools/manage_modules.php @@ -33,12 +33,12 @@ $dryrun = isset($flags['n']) || isset($flags['dry-run']); $help = isset($flags['h']) || isset($flags['help']); -if($help) { +if ($help) { usage(); exit(0); } -if(!isset($flags['add']) && !isset($flags['remove'])) { +if (!isset($flags['add']) && !isset($flags['remove'])) { usage(); fwrite(STDERR, "\nMust specify --add or --remove flag\n"); exit(1); @@ -49,13 +49,13 @@ "SELECT Name FROM modules", [] ); -if(isset($flags['remove'])) { - foreach($currentModules as $module) { +if (isset($flags['remove'])) { + foreach ($currentModules as $module) { try { Module::factory($module); } catch(\LorisNoSuchModuleException | \LorisModuleMissingException $e) { print "Removing $module\n"; - if($dryrun) { + if ($dryrun) { continue; } $DB->delete("modules", [ 'Name' => $module]); @@ -63,38 +63,50 @@ } } -if(isset($flags['add'])) { +if (isset($flags['add'])) { addDir(__DIR__ . "/../modules"); addDir(__DIR__ . "/../project/modules"); } -function addDir(string $moduledir) { +/** + * Adds a new entry to the `modules` table for any subdirectories under $moduledir + * that are valid modules + * + * @param string $moduledir The path to a directory containing code for a + * LORIS module. + * + * @return void + */ +function addDir(string $moduledir): void +{ global $DB; global $dryrun; global $currentModules; $modules = scandir($moduledir); foreach ($modules as $module) { - if(in_array($module, [".", ".."], true)) { + if (in_array($module, [".", ".."], true)) { continue; } - if(!in_array($module, $currentModules, true)) { - if(is_dir("$moduledir/$module")) { + if (!in_array($module, $currentModules, true)) { + if (is_dir("$moduledir/$module")) { try { Module::factory($module); - } catch(\LorisNoSuchModuleException | \LorisModuleMissingException $e) { + } catch(\LorisNoSuchModuleException + | \LorisModuleMissingException $e + ) { // Wasn't a valid module, so don't add it. continue; } print "Adding $module\n"; - if($dryrun) { + if ($dryrun) { continue; } $DB->insert( "modules", [ - 'Name' => $module, + 'Name' => $module, 'Active' => 'Y' ] ); @@ -103,7 +115,13 @@ function addDir(string $moduledir) { } } -function usage() { +/** + * Prints help text for this tool. + * + * @return void + */ +function usage(): void +{ global $argv; print <<