From 6a93167d39e3820fba272f184afbb7ef589df4d8 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 20 Sep 2024 18:48:30 +0100 Subject: [PATCH] Create getForeignKeysData Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 126 +------------------------------ src/ConfigStorage/Relation.php | 50 ++++++------ src/Database/Designer/Common.php | 5 -- 3 files changed, 30 insertions(+), 151 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5b4c9807b2..36a461ee02 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4280,26 +4280,6 @@ parameters: count: 1 path: src/Controllers/Table/Partition/RepairController.php - - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 1 - path: src/Controllers/Table/RelationController.php - - - - message: "#^Cannot access offset 'foreign_db' on mixed\\.$#" - count: 1 - path: src/Controllers/Table/RelationController.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Controllers/Table/RelationController.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 1 - path: src/Controllers/Table/RelationController.php - - message: "#^Cannot access offset 'ref_db_name' on mixed\\.$#" count: 1 @@ -4385,11 +4365,6 @@ parameters: count: 1 path: src/Controllers/Table/RelationController.php - - - message: "#^Parameter \\#7 \\$existrelForeign of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateForeignKeys\\(\\) expects array, mixed given\\.$#" - count: 1 - path: src/Controllers/Table/RelationController.php - - message: "#^Argument of an invalid type array\\|string supplied for foreach, only iterables are supported\\.$#" count: 1 @@ -5722,7 +5697,7 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 3 + count: 2 path: src/Database/Designer/Common.php - @@ -5730,21 +5705,6 @@ parameters: count: 1 path: src/Database/Designer/Common.php - - - message: "#^Cannot access offset 'foreign_db' on mixed\\.$#" - count: 1 - path: src/Database/Designer/Common.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Database/Designer/Common.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 1 - path: src/Database/Designer/Common.php - - message: "#^Cannot access offset 'index_list' on mixed\\.$#" count: 1 @@ -6337,7 +6297,7 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 6 + count: 5 path: src/Display/Results.php - @@ -6350,21 +6310,6 @@ parameters: count: 6 path: src/Display/Results.php - - - message: "#^Cannot access offset 'foreign_db' on mixed\\.$#" - count: 2 - path: src/Display/Results.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Display/Results.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 2 - path: src/Display/Results.php - - message: "#^Cannot access offset 'geoOption' on mixed\\.$#" count: 8 @@ -8925,21 +8870,6 @@ parameters: count: 4 path: src/Normalization.php - - - message: "#^Cannot access offset 'foreign_db' on mixed\\.$#" - count: 4 - path: src/Operations.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 3 - path: src/Operations.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 4 - path: src/Operations.php - - message: "#^Cannot access offset 0 on mixed\\.$#" count: 6 @@ -10255,16 +10185,6 @@ parameters: count: 1 path: src/Plugins/Export/Helpers/Pdf.php - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Plugins/Export/Helpers/Pdf.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 1 - path: src/Plugins/Export/Helpers/Pdf.php - - message: "#^Cannot access offset 'olm' on mixed\\.$#" count: 2 @@ -10792,16 +10712,6 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 2 - path: src/Plugins/Schema/Dia/DiaRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Plugins/Schema/Dia/DiaRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" count: 1 path: src/Plugins/Schema/Dia/DiaRelationSchema.php @@ -10867,16 +10777,6 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 2 - path: src/Plugins/Schema/Eps/EpsRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Plugins/Schema/Eps/EpsRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" count: 1 path: src/Plugins/Schema/Eps/EpsRelationSchema.php @@ -10992,7 +10892,7 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 2 + count: 1 path: src/Plugins/Schema/Pdf/PdfRelationSchema.php - @@ -11005,16 +10905,6 @@ parameters: count: 10 path: src/Plugins/Schema/Pdf/PdfRelationSchema.php - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Plugins/Schema/Pdf/PdfRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" - count: 1 - path: src/Plugins/Schema/Pdf/PdfRelationSchema.php - - message: "#^Cannot access offset 'index_list' on mixed\\.$#" count: 1 @@ -11187,16 +11077,6 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 2 - path: src/Plugins/Schema/Svg/SvgRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_field' on mixed\\.$#" - count: 1 - path: src/Plugins/Schema/Svg/SvgRelationSchema.php - - - - message: "#^Cannot access offset 'foreign_table' on mixed\\.$#" count: 1 path: src/Plugins/Schema/Svg/SvgRelationSchema.php diff --git a/src/ConfigStorage/Relation.php b/src/ConfigStorage/Relation.php index b03e4f328b..4aa6071f2b 100644 --- a/src/ConfigStorage/Relation.php +++ b/src/ConfigStorage/Relation.php @@ -37,7 +37,6 @@ use function is_string; use function ksort; use function mb_check_encoding; use function mb_strlen; -use function mb_strtolower; use function mb_strtoupper; use function mb_substr; use function natcasesort; @@ -46,6 +45,7 @@ use function sprintf; use function str_contains; use function str_replace; use function strnatcasecmp; +use function strtolower; use function trim; use function uksort; use function usort; @@ -370,8 +370,9 @@ class Relation * @param string $table the name of the table to check for * @param string $column the name of the column to check for * @param string $source the source for foreign key information + * @psalm-param 'both'|'internal'|'foreign' $source * - * @return mixed[] db,table,column + * @return array> */ public function getForeigners(string $db, string $table, string $column = '', string $source = 'both'): array { @@ -389,40 +390,27 @@ class Relation $relQuery .= ' AND `master_field` = ' . $this->dbi->quoteString($column); } + /** @var array> */ $foreign = $this->dbi->fetchResult($relQuery, 'master_field', null, ConnectionType::ControlUser); } if (($source === 'both' || $source === 'foreign') && $table !== '') { - $tableObj = new Table($table, $db, $this->dbi); - $showCreateTable = $tableObj->showCreate(); - if ($showCreateTable !== '') { - $parser = new Parser($showCreateTable); - $stmt = $parser->statements[0]; - $foreign['foreign_keys_data'] = []; - if ($stmt instanceof CreateStatement) { - $foreign['foreign_keys_data'] = TableUtils::getForeignKeys($stmt); - } - } + $foreign['foreign_keys_data'] = $this->getForeignKeysData($table, $db); } /** * Emulating relations for some information_schema tables */ - $isInformationSchema = mb_strtolower($db) === 'information_schema'; - $isMysql = mb_strtolower($db) === 'mysql'; - if (($isInformationSchema || $isMysql) && ($source === 'internal' || $source === 'both')) { - if ($isInformationSchema) { - $internalRelations = InternalRelations::INFORMATION_SCHEMA; - } else { - $internalRelations = InternalRelations::MYSQL; - } + if (in_array(strtolower($db), ['information_schema', 'mysql'], true) && ($source === 'internal' || $source === 'both')) { + $internalRelations = strtolower($db) === 'information_schema' + ? InternalRelations::INFORMATION_SCHEMA + : InternalRelations::MYSQL; if (isset($internalRelations[$table])) { foreach ($internalRelations[$table] as $field => $relations) { if ( - ($column !== '' && $column != $field) - || (isset($foreign[$field]) - && $foreign[$field] != '') + ($column !== '' && $column !== $field) + || (isset($foreign[$field]) && $foreign[$field] != '') ) { continue; } @@ -435,6 +423,22 @@ class Relation return $foreign; } + /** @return list|string|null>> */ + private function getForeignKeysData(string $table, string $db): array + { + $tableObj = new Table($table, $db, $this->dbi); + $showCreateTable = $tableObj->showCreate(); + if ($showCreateTable !== '') { + $parser = new Parser($showCreateTable); + $stmt = $parser->statements[0]; + if ($stmt instanceof CreateStatement) { + return TableUtils::getForeignKeys($stmt); + } + } + + return []; + } + /** * Gets the display field of a table * diff --git a/src/Database/Designer/Common.php b/src/Database/Designer/Common.php index 1370c569c4..d4e77ff07e 100644 --- a/src/Database/Designer/Common.php +++ b/src/Database/Designer/Common.php @@ -130,11 +130,6 @@ class Common $row = $this->relation->getForeigners(Current::$database, $val, '', 'foreign'); - // We do not have access to the foreign keys if the user has partial access to the columns - if (! isset($row['foreign_keys_data'])) { - continue; - } - foreach ($row['foreign_keys_data'] as $oneKey) { foreach ($oneKey['index_list'] as $index => $oneField) { $con['C_NAME'][$i] = rawurlencode($oneKey['constraint']);