From 8ae27222ac45588d4ce637a8b77d7c5543f5e050 Mon Sep 17 00:00:00 2001 From: Jefferson Casimir Date: Mon, 27 Jan 2025 14:37:51 -0500 Subject: [PATCH] satisfy php linter --- .../php/instrument_data.class.inc | 51 +++++++++++------ .../php/abstractserverprocess.class.inc | 4 +- .../php/mriuploadserverprocess.class.inc | 10 +++- ...parseinstrumentdataserverprocess.class.inc | 49 +++++++++++------ .../php/serverprocessesmonitor.class.inc | 4 +- .../php/serverprocessfactory.class.inc | 12 ++-- .../php/serverprocesslauncher.class.inc | 8 +-- php/libraries/InstrumentDataParser.class.inc | 55 ++++++++++--------- .../NDB_BVL_Instrument_LINST.class.inc | 4 +- tools/monitor_instrument_data.php | 38 +++++++++---- tools/parse_instrument_data.php | 21 +++---- 11 files changed, 160 insertions(+), 96 deletions(-) diff --git a/modules/instrument_manager/php/instrument_data.class.inc b/modules/instrument_manager/php/instrument_data.class.inc index 14b5d2647b1..1d1cc6d9e92 100644 --- a/modules/instrument_manager/php/instrument_data.class.inc +++ b/modules/instrument_manager/php/instrument_data.class.inc @@ -10,7 +10,13 @@ use LORIS\server_processes_manager as SP; /** * Upload instrument data * - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * PHP Version 8 + * + * @category Main + * @package LORIS + * @author Loris Team + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://www.github.com/aces/Loris/ */ class Instrument_Data extends \NDB_Page { @@ -109,27 +115,28 @@ class Instrument_Data extends \NDB_Page // Ensure the user is allowed to upload. if (!$request->getAttribute('user')->hasPermission( 'instrument_manager_write' - )) { + ) + ) { return new \LORIS\Http\Response\JSON\Forbidden(); } - $requestBody = (array) $request->getParsedBody(); + $requestBody = (array) $request->getParsedBody(); if (isset($requestBody['instrument'])) { $instrument = $requestBody['instrument']; if ($this->instrumentExists($instrument)) { - $dataFile = $request->getUploadedFiles()['data_file']; - $userID = $request->getAttribute('user')->getUsername(); + $dataFile = $request->getUploadedFiles()['data_file']; + $userID = $request->getAttribute('user')->getUsername(); $uploadFolder = $this->loris ->getConfiguration()->getSetting('instrumentDataPath'); $filePrefix = $userID . '_' . time() . '_'; - $fileLocation = $uploadFolder . "/$filePrefix" + $fileLocation = $uploadFolder . "/$filePrefix" . $dataFile->getClientFilename(); $dataFile->moveTo($fileLocation); $fileInfo = new SplFileInfo($fileLocation); - $this->insertCSVFileEntry($fileInfo->getFilename()); + $this->_insertCSVFileEntry($fileInfo->getFilename()); if ($fileInfo->getSize() > self::MAX_FILE_BYTES) { // Run as background task @@ -141,9 +148,10 @@ class Instrument_Data extends \NDB_Page $result = [ 'success' => true, - 'message' => "Due to the relatively large size of the uploaded file, " . - "the process has been sent to the background task queue. " . - "You will receive an email once completed." + 'message' => "Due to the relatively large size of the " . + "uploaded file, the process has been sent to the " . + "background task queue. You will receive an email " . + "once completed." ]; return new \LORIS\Http\Response\JSON\Created($result); @@ -155,8 +163,8 @@ class Instrument_Data extends \NDB_Page $instrument, $fileInfo, ); - $data = $dataParser->parseCSV($this->loris); - $validData = $dataParser->validateData( + $data = $dataParser->parseCSV($this->loris); + $validData = $dataParser->validateData( $data, [ 'UserID' => $userID, @@ -213,11 +221,20 @@ class Instrument_Data extends \NDB_Page return $count > 0; } - private function insertCSVFileEntry($filename) + /** + * Insert entry for CSV file + * + * @param string $filename The file name + * + * @return void + */ + private function _insertCSVFileEntry($filename) { - // Create db entry for csv file - $this->loris->getDatabaseConnection()->insert('instrument_data_files', [ - 'FilePath' => $filename, - ]); + $this->loris->getDatabaseConnection()->insert( + 'instrument_data_files', + [ + 'FilePath' => $filename, + ] + ); } } diff --git a/modules/server_processes_manager/php/abstractserverprocess.class.inc b/modules/server_processes_manager/php/abstractserverprocess.class.inc index e75f0694990..e22f25806b4 100644 --- a/modules/server_processes_manager/php/abstractserverprocess.class.inc +++ b/modules/server_processes_manager/php/abstractserverprocess.class.inc @@ -582,7 +582,7 @@ abstract class AbstractServerProcess /** * Gets directory where all temporary files created by this task should go * - * @return string full path to the temporary directory + * @return string full path to the temporary directory * @abstract */ abstract public function getTmpDir(); @@ -590,6 +590,7 @@ abstract class AbstractServerProcess /** * Change file permissions * + * @return void * @abstract */ abstract public function changeFilePermissions(); @@ -597,6 +598,7 @@ abstract class AbstractServerProcess /** * Perform action once process is completed * + * @return void * @abstract */ abstract public function performPostAction(); diff --git a/modules/server_processes_manager/php/mriuploadserverprocess.class.inc b/modules/server_processes_manager/php/mriuploadserverprocess.class.inc index afa7895efc9..f7b60d1df9f 100644 --- a/modules/server_processes_manager/php/mriuploadserverprocess.class.inc +++ b/modules/server_processes_manager/php/mriuploadserverprocess.class.inc @@ -223,14 +223,20 @@ class MriUploadServerProcess extends AbstractServerProcess /** * Change file permissions * + * @return void */ - public function changeFilePermissions() {} + public function changeFilePermissions() + { + } /** * Perform action once process is completed * + * @return void */ - public function performPostAction() {} + public function performPostAction() + { + } } diff --git a/modules/server_processes_manager/php/parseinstrumentdataserverprocess.class.inc b/modules/server_processes_manager/php/parseinstrumentdataserverprocess.class.inc index 8cdb2d52b4d..964db812b95 100644 --- a/modules/server_processes_manager/php/parseinstrumentdataserverprocess.class.inc +++ b/modules/server_processes_manager/php/parseinstrumentdataserverprocess.class.inc @@ -48,8 +48,9 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess /** * Builds a new ParseInstrumentDataServerProcess * - * @param string $instrument Instrument name + * @param string $instrument Instrument name * @param string $fileLocation Location of the uploaded file + * @param ?int $id ID of the process in the database. * @param ?int $pid PID for this process * @param ?string $stdoutFile full path of file used to store the process's * stdout @@ -93,9 +94,11 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess */ public function getShellCommand() { - $parseScript = "php " . dirname(__DIR__, 3) . "/tools/parse_instrument_data.php "; + $parseScript = "php " . + dirname(__DIR__, 3) . + "/tools/parse_instrument_data.php "; $parseScript .= "$this->_instrument $this->_fileLocation "; - $parseScript .= "{$this->getUserId()} {$this->getUserId()}"; // TODO: Examiner? + $parseScript .= "{$this->getUserId()} {$this->getUserId()}"; return escapeshellarg($parseScript); } @@ -127,7 +130,8 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess if (is_numeric($exitCode) && $exitCode == 0) { return "Instrument data parse completed without errors.\n\n$exitText"; } else { - return "Instrument data parse failed with error code: $exitCode.\n\n$exitText"; + return "Instrument data parse failed with error code: $exitCode." . + "\n\n$exitText"; } } @@ -173,8 +177,10 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess /** * Send email to user who triggered the process launch * + * @return void */ - public function sendNotificationEmail() { + public function sendNotificationEmail() + { error_log("Sending email to {$this->getUserId()}"); $user = \NDB_Factory::singleton()->database()->pselectRow( @@ -183,32 +189,43 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess ); $msg_data = [ - 'name' => $user['First_name'], + 'name' => $user['First_name'], 'message' => $this->getExitText(), ]; - \Email::send($user['Email'], 'notifier_instrument_data_upload.tpl', $msg_data); + \Email::send( + $user['Email'], + 'notifier_instrument_data_upload.tpl', + $msg_data + ); } /** * Change file permissions * + * @return void */ - public function changeFilePermissions() { - array_map(function($file){ - chmod($file, 0775); - }, [ - $this->getStdoutFile(), - $this->getStderrFile(), - $this->getExitCodeFile(), - ]); + public function changeFilePermissions() + { + array_map( + function ($file) { + chmod($file, 0775); + }, + [ + $this->getStdoutFile(), + $this->getStderrFile(), + $this->getExitCodeFile(), + ] + ); } /** * Perform action once process is completed * + * @return void */ - public function performPostAction() { + public function performPostAction() + { $this->sendNotificationEmail(); } } diff --git a/modules/server_processes_manager/php/serverprocessesmonitor.class.inc b/modules/server_processes_manager/php/serverprocessesmonitor.class.inc index 0f051258d96..8a45d025045 100644 --- a/modules/server_processes_manager/php/serverprocessesmonitor.class.inc +++ b/modules/server_processes_manager/php/serverprocessesmonitor.class.inc @@ -141,7 +141,8 @@ class ServerProcessesMonitor $serverProcessFactory = new ServerProcessFactory(); $savedProcesses = []; foreach ($rows as $row) { - $savedProcesses[$row['id']] = $serverProcessFactory->getServerProcess($row); + $savedProcesses[$row['id']] + = $serverProcessFactory->getServerProcess($row); } @@ -161,7 +162,6 @@ class ServerProcessesMonitor // Find process in the database with this id (null if none) $matchingProcess = $savedProcesses[$id] ?? null; - if (is_null($matchingProcess)) { // No process with this id. Unknown id = unknown state $state = 'UNKNOWN_ID'; diff --git a/modules/server_processes_manager/php/serverprocessfactory.class.inc b/modules/server_processes_manager/php/serverprocessfactory.class.inc index d89a9bb48b7..a858fdf74f4 100644 --- a/modules/server_processes_manager/php/serverprocessfactory.class.inc +++ b/modules/server_processes_manager/php/serverprocessfactory.class.inc @@ -29,8 +29,10 @@ class ServerProcessFactory /** * Creates a server process based on the parameters passed as arguments. * - * @param array $dbRow Process database row. + * @param array $dbRow Process database row. + * * @return AbstractServerProcess the server process created + * * @throws \InvalidArgumentException if the server process type is unknown */ public function getServerProcess(array $dbRow): AbstractServerProcess @@ -50,7 +52,7 @@ class ServerProcessFactory $dbRow['start_time'], $dbRow['end_time'], $dbRow['exit_text'] - ), + ), ParseInstrumentDataServerProcess::PROCESS_TYPE => new ParseInstrumentDataServerProcess( '', @@ -65,8 +67,10 @@ class ServerProcessFactory $dbRow['start_time'], $dbRow['end_time'], $dbRow['exit_text'] - ), - default => throw new \InvalidArgumentException("Unknown server process type: ${dbRow['type']}") + ), + default => throw new \InvalidArgumentException( + "Unknown server process type: ${dbRow['type']}" + ) }; } } diff --git a/modules/server_processes_manager/php/serverprocesslauncher.class.inc b/modules/server_processes_manager/php/serverprocesslauncher.class.inc index f255efa0523..73c4c0bc662 100644 --- a/modules/server_processes_manager/php/serverprocesslauncher.class.inc +++ b/modules/server_processes_manager/php/serverprocesslauncher.class.inc @@ -161,9 +161,8 @@ class ServerProcessLauncher /** * Launch instrument data parse process * - * @param string $instrument Instrument name - * @param string $fileLocation File location - * + * @param string $instrument Instrument name + * @param string $fileLocation File location * * @return ?int ID (in the database) of the launched process or null * if the process could not be run @@ -171,8 +170,7 @@ class ServerProcessLauncher public function parseInstrumentData( string $instrument, string $fileLocation - ): ?int - { + ): ?int { $parseInstrumentDataProcess = new ParseInstrumentDataServerProcess( $instrument, $fileLocation diff --git a/php/libraries/InstrumentDataParser.class.inc b/php/libraries/InstrumentDataParser.class.inc index 102e71f8209..166b5042a4f 100644 --- a/php/libraries/InstrumentDataParser.class.inc +++ b/php/libraries/InstrumentDataParser.class.inc @@ -342,21 +342,21 @@ class InstrumentDataParser extends CSVParser // TODO: Tech debt -- can be reset between getGV and save - $previousFiles = $_FILES; + $previousFiles = $_FILES; $previousRequest = $_REQUEST; - $previousPost = $_POST; - $_FILES = []; - $_REQUEST = $rowData; - $_POST = $rowData; + $previousPost = $_POST; + $_FILES = []; + $_REQUEST = $rowData; + $_POST = $rowData; $instrument->form->getGroupValues( array_keys($rowData), $out ); - $_FILES = $previousFiles; + $_FILES = $previousFiles; $_REQUEST = $previousRequest; - $_POST = $previousPost; - $success = $instrument->save(); + $_POST = $previousPost; + $success = $instrument->save(); if (!$success) { throw new RuntimeException( @@ -367,9 +367,12 @@ class InstrumentDataParser extends CSVParser $query = "SELECT DataID FROM flag WHERE CommentID = :commentID"; - $dataID = $db->pselectOneInt($query, [ - 'commentID' => $instrument->getCommentID() - ]); + $dataID = $db->pselectOneInt( + $query, + [ + 'commentID' => $instrument->getCommentID() + ] + ); $db->update( 'instrument_data_files', @@ -397,27 +400,27 @@ class InstrumentDataParser extends CSVParser $db->commit(); error_log("Saved " . count($instrumentData) . " row(s)"); return [ - 'success' => true, - 'message' => "Saved " . count($instrumentData) . " row(s)", + 'success' => true, + 'message' => "Saved " . count($instrumentData) . " row(s)", ]; } - } catch (\Exception $e) { - error_log( - "Rolling back due to unexpected error: {$e->getMessage()}" - ); - $db->rollBack(); - return [ - 'success' => false, - 'message' => "Data error: {$e->getMessage()}" - ]; - } - } catch (DatabaseException $e) { - error_log("DB exception: {$e->getMessage()}"); + } catch (\Exception $e) { + error_log( + "Rolling back due to unexpected error: {$e->getMessage()}" + ); + $db->rollBack(); return [ 'success' => false, - 'message' => "DB Error: {$e->getMessage()}" + 'message' => "Data error: {$e->getMessage()}" ]; } + } catch (DatabaseException $e) { + error_log("DB exception: {$e->getMessage()}"); + return [ + 'success' => false, + 'message' => "DB Error: {$e->getMessage()}" + ]; + } } } diff --git a/php/libraries/NDB_BVL_Instrument_LINST.class.inc b/php/libraries/NDB_BVL_Instrument_LINST.class.inc index d4756ac5455..cd7c5b8394b 100644 --- a/php/libraries/NDB_BVL_Instrument_LINST.class.inc +++ b/php/libraries/NDB_BVL_Instrument_LINST.class.inc @@ -167,8 +167,8 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument //If this is an OR rule using two different controllers //explode it at the pipe. //ex: q_1{@}=={@}yes|q_2{@}=={@}yes - if (str_contains($rule, "|") && - stristr(substr($rule, strpos($rule, "|")), "{@}") + if (str_contains($rule, "|") + && stristr(substr($rule, strpos($rule, "|")), "{@}") ) { $rules_array = explode("|", $rule); } else { diff --git a/tools/monitor_instrument_data.php b/tools/monitor_instrument_data.php index ffa574bb1e5..c7c41c33a64 100755 --- a/tools/monitor_instrument_data.php +++ b/tools/monitor_instrument_data.php @@ -1,13 +1,16 @@ - - * @license Loris license + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 * @link https://www.github.com/aces/Loris/ */ @@ -15,15 +18,28 @@ require_once __DIR__ . "/generic_includes.php"; -require_once __DIR__ . "/../modules/server_processes_manager/php/serverprocessesmonitor.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/idatabaseprovider.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/defaultdatabaseprovider.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/serverprocessfactory.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/abstractserverprocess.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/parseinstrumentdataserverprocess.class.inc"; -require_once __DIR__ . "/../modules/server_processes_manager/php/mriuploadserverprocess.class.inc"; +$serverProcessManagerModuleDir = __DIR__ . "/../modules/server_processes_manager"; + +require_once $serverProcessManagerModuleDir . + "/php/serverprocessesmonitor.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/idatabaseprovider.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/defaultdatabaseprovider.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/serverprocessfactory.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/abstractserverprocess.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/parseinstrumentdataserverprocess.class.inc"; +require_once $serverProcessManagerModuleDir . + "/php/mriuploadserverprocess.class.inc"; $spm = new ServerProcessesMonitor(); -$d = $spm->getProcessesState(null, null, ParseInstrumentDataServerProcess::PROCESS_TYPE); +$spm->getProcessesState( + null, + null, + ParseInstrumentDataServerProcess::PROCESS_TYPE +); diff --git a/tools/parse_instrument_data.php b/tools/parse_instrument_data.php index e32ce2e9736..db7a8becae8 100644 --- a/tools/parse_instrument_data.php +++ b/tools/parse_instrument_data.php @@ -1,9 +1,10 @@ -parseCSV($lorisInstance); - $validData = $dataParser->validateData( + $data = $dataParser->parseCSV($lorisInstance); + $validData = $dataParser->validateData( $data, [ 'UserID' => $userID,