diff options
author | Simon Rettberg | 2023-11-14 14:47:55 +0100 |
---|---|---|
committer | Simon Rettberg | 2023-11-14 14:47:55 +0100 |
commit | 06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0 (patch) | |
tree | 7e5493b102074672d8cfd8fe1a61e49f080edbe8 /install.php | |
parent | Update phpstorm config (diff) | |
download | slx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.tar.gz slx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.tar.xz slx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.zip |
Add function param/return types, fix a lot more phpstorm complaints
Diffstat (limited to 'install.php')
-rw-r--r-- | install.php | 81 |
1 files changed, 42 insertions, 39 deletions
diff --git a/install.php b/install.php index d411d834..530298b1 100644 --- a/install.php +++ b/install.php @@ -19,6 +19,8 @@ * they might depend on some tables that do not exist yet. ;) */ +use JetBrains\PhpStorm\NoReturn; + /** * Report back the update status to the browser/client and terminate execution. * This has to be called by an update module at some point to signal the result @@ -27,7 +29,8 @@ * @param string $status one of the UPDATE_* status codes * @param string $message Human readable description of the status (optional) */ -function finalResponse($status, $message = '') +#[NoReturn] +function finalResponse(string $status, string $message = '') { if (!DIRECT_MODE && AJAX) { echo json_encode(array('status' => $status, 'message' => $message)); @@ -58,7 +61,7 @@ function handleUpdateResult($res) * Helper functions for dealing with the database */ -function tableHasColumn($table, $column) +function tableHasColumn(string $table, string $column): bool { return tableColumnType($table, $column) !== false; } @@ -66,7 +69,7 @@ function tableHasColumn($table, $column) /** * Get type of column, as reported by DESCRIBE <table>; */ -function tableColumnType($table, $column) +function tableColumnType(string $table, string $column) { return tableGetDescribeColumn($table, $column, 'Type'); } @@ -78,8 +81,10 @@ function tableColumnKeyType($table, $column) /** * For internal use + * @param string|string[] $column + * @return string|false */ -function tableGetDescribeColumn($table, $column, $what) +function tableGetDescribeColumn(string $table, $column, string $what) { $table = preg_replace('/\W/', '', $table); $res = Database::simpleQuery("DESCRIBE `$table`", array(), true); @@ -92,33 +97,37 @@ function tableGetDescribeColumn($table, $column, $what) return false; } -function tableGetIndex($table, $index) +/** + * Return name of index that spans all the columns given, in the same order. + * Returns false if not found + * + * @param string[] $columns + * @return false|string + */ +function tableGetIndex(string $table, array $columns) { $table = preg_replace('/\W/', '', $table); - if (!is_array($index)) { - $index = [$index]; - } $res = Database::simpleQuery("SHOW INDEX FROM `$table`", array(), true); if ($res !== false) { $matches = []; foreach ($res as $row) { $i = $row['Seq_in_index'] - 1; - if (isset($index[$i]) && $index[$i] === $row['Column_name']) { + if (isset($columns[$i]) && $columns[$i] === $row['Column_name']) { if (!isset($matches[$row['Key_name']])) { $matches[$row['Key_name']] = 0; } $matches[$row['Key_name']]++; } } - } - foreach ($matches as $key => $m) { - if ($m === count($index)) - return $key; + foreach ($matches as $key => $m) { + if ($m === count($columns)) + return $key; + } } return false; } -function tableDropColumn($table, $column) +function tableDropColumn(string $table, string $column): void { $table = preg_replace('/\W/', '', $table); $column = preg_replace('/\W/', '', $column); @@ -131,7 +140,7 @@ function tableDropColumn($table, $column) } } -function tableExists($table) +function tableExists(string $table): bool { $res = Database::simpleQuery("SHOW TABLES", array(), true); while ($row = $res->fetch(PDO::FETCH_NUM)) { @@ -141,8 +150,9 @@ function tableExists($table) return false; } -function tableRename($old, $new) { - return Database::simpleQuery("RENAME TABLE $old TO $new", []); +function tableRename(string $old, string $new): bool +{ + return Database::simpleQuery("RENAME TABLE $old TO $new", []) !== false; } @@ -155,7 +165,7 @@ function tableRename($old, $new) { * @param string $refColumn referenced column * @return false|string[] false == doesn't exist, assoc array otherwise */ -function tableGetConstraints($table, $column, $refTable, $refColumn) +function tableGetConstraints(string $table, string $column, string $refTable, string $refColumn) { $db = 'openslx'; if (defined('CONFIG_SQL_DB')) { @@ -188,21 +198,22 @@ function tableGetConstraints($table, $column, $refTable, $refColumn) * @param string $actions "ON xxx ON yyy" string * @return string UPDATE_* result code */ -function tableAddConstraint($table, $column, $refTable, $refColumn, $actions, $ignoreError = false, $name = '') +function tableAddConstraint(string $table, string $column, string $refTable, string $refColumn, string $actions, + bool $ignoreError = false, $name = ''): string { $test = tableExists($refTable) && tableHasColumn($refTable, $refColumn); if ($test === false) { - // Most likely, destination table does not exist yet or isn't up to date + // Most likely, destination table does not exist yet or isn't up-to-date return UPDATE_RETRY; } // TODO: Refactor function, make this two args $update = 'RESTRICT'; $delete = 'RESTRICT'; - if (preg_match('/on\s+update\s+(RESTRICT|SET\s+NULL|CASCADE)/ims', $actions, $out)) { - $update = preg_replace('/\s+/ms', ' ', strtoupper($out[1])); + if (preg_match('/on\s+update\s+(RESTRICT|SET\s+NULL|CASCADE)/im', $actions, $out)) { + $update = preg_replace('/\s+/m', ' ', strtoupper($out[1])); } - if (preg_match('/on\s+delete\s+(RESTRICT|SET\s+NULL|CASCADE)/ims', $actions, $out)) { - $delete = preg_replace('/\s+/ms', ' ', strtoupper($out[1])); + if (preg_match('/on\s+delete\s+(RESTRICT|SET\s+NULL|CASCADE)/im', $actions, $out)) { + $delete = preg_replace('/\s+/m', ' ', strtoupper($out[1])); } $test = tableGetConstraints($table, $column, $refTable, $refColumn); if ($test !== false) { @@ -239,9 +250,8 @@ function tableAddConstraint($table, $column, $refTable, $refColumn, $actions, $i if ($ret === false) { if ($ignoreError) { return UPDATE_FAILED; - } else { - finalResponse(UPDATE_FAILED, "Cannot add constraint $table.$column -> $refTable.$refColumn: " . Database::lastError()); } + finalResponse(UPDATE_FAILED, "Cannot add constraint $table.$column -> $refTable.$refColumn: " . Database::lastError()); } return UPDATE_DONE; } @@ -253,12 +263,12 @@ function tableAddConstraint($table, $column, $refTable, $refColumn, $actions, $i * @param string $constraint constraint name * @return bool success indicator */ -function tableDeleteConstraint($table, $constraint) +function tableDeleteConstraint(string $table, string $constraint): bool { return Database::exec("ALTER TABLE `$table` DROP FOREIGN KEY `$constraint`") !== false; } -function tableCreate($table, $structure, $fatalOnError = true) +function tableCreate(string $table, string $structure, bool $fatalOnError = true): string { if (tableExists($table)) { return UPDATE_NOOP; @@ -273,7 +283,8 @@ function tableCreate($table, $structure, $fatalOnError = true) return UPDATE_FAILED; } -function responseFromArray($array) +#[NoReturn] +function responseFromArray(array $array) { if (in_array(UPDATE_FAILED, $array)) { finalResponse(UPDATE_FAILED, 'Update failed!'); @@ -286,7 +297,6 @@ function responseFromArray($array) } finalResponse(UPDATE_NOOP, 'Everything already up to date'); - } /* @@ -329,19 +339,12 @@ if (!Database::init(true)) { // Good to go so far -/** - * @param \Module $module - * @return bool - */ -function hasUpdateScript($module) +function hasUpdateScript(Module $module): bool { return is_readable($module->getDir() . '/install.inc.php'); } -/** - * @param Module $module - */ -function runUpdateScript($module) +function runUpdateScript(Module $module): void { require_once $module->getDir() . '/install.inc.php'; } |