Skip to content

Commit 7be4a9f

Browse files
committed
Improve type inference in Plugins::getPlugins method
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 6fcb30d commit 7be4a9f

4 files changed

Lines changed: 34 additions & 256 deletions

File tree

libraries/classes/Plugins.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ public static function getPlugin(string $type, string $format, $param = null): ?
8383

8484
/**
8585
* @param string $type server|database|table|raw
86+
* @psalm-param 'server'|'database'|'table'|'raw' $type
8687
*
8788
* @return ExportPlugin[]
89+
* @psalm-return list<ExportPlugin>
8890
*/
8991
public static function getExport(string $type, bool $singleTable): array
9092
{
@@ -95,8 +97,10 @@ public static function getExport(string $type, bool $singleTable): array
9597

9698
/**
9799
* @param string $type server|database|table
100+
* @psalm-param 'server'|'database'|'table' $type
98101
*
99102
* @return ImportPlugin[]
103+
* @psalm-return list<ImportPlugin>
100104
*/
101105
public static function getImport(string $type): array
102106
{
@@ -107,6 +111,7 @@ public static function getImport(string $type): array
107111

108112
/**
109113
* @return SchemaPlugin[]
114+
* @psalm-return list<SchemaPlugin>
110115
*/
111116
public static function getSchema(): array
112117
{
@@ -120,6 +125,11 @@ public static function getSchema(): array
120125
* @psalm-param 'Export'|'Import'|'Schema' $type
121126
*
122127
* @return Plugin[] list of plugin instances
128+
* @psalm-return (
129+
* $type is 'Export'
130+
* ? list<ExportPlugin>
131+
* : ($type is 'Import' ? list<ImportPlugin> : list<SchemaPlugin>)
132+
* )
123133
*/
124134
private static function getPlugins(string $type): array
125135
{
@@ -146,24 +156,17 @@ private static function getPlugins(string $type): array
146156
continue;
147157
}
148158

149-
if ($type === 'Export') {
150-
/**
151-
* @psalm-suppress InvalidArrayOffset, MixedAssignment, MixedMethodCall
152-
*/
153-
$plugin = new $class(
159+
if ($type === 'Export' && is_subclass_of($class, ExportPlugin::class)) {
160+
$plugins[] = new $class(
154161
$GLOBALS['containerBuilder']->get('relation'),
155162
$GLOBALS['containerBuilder']->get('export'),
156163
$GLOBALS['containerBuilder']->get('transformations')
157164
);
158-
} else {
159-
$plugin = new $class();
165+
} elseif ($type === 'Import' && is_subclass_of($class, ImportPlugin::class)) {
166+
$plugins[] = new $class();
167+
} elseif ($type === 'Schema' && is_subclass_of($class, SchemaPlugin::class)) {
168+
$plugins[] = new $class();
160169
}
161-
162-
if (! ($plugin instanceof Plugin) || ! $plugin->isAvailable()) {
163-
continue;
164-
}
165-
166-
$plugins[] = $plugin;
167170
}
168171

169172
usort($plugins, static function (Plugin $plugin1, Plugin $plugin2): int {

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,21 +5470,6 @@ parameters:
54705470
count: 1
54715471
path: libraries/classes/Plugins.php
54725472

5473-
-
5474-
message: "#^Method PhpMyAdmin\\\\Plugins\\:\\:getExport\\(\\) should return array\\<PhpMyAdmin\\\\Plugins\\\\ExportPlugin\\> but returns array\\<PhpMyAdmin\\\\Plugins\\\\Plugin\\>\\.$#"
5475-
count: 1
5476-
path: libraries/classes/Plugins.php
5477-
5478-
-
5479-
message: "#^Method PhpMyAdmin\\\\Plugins\\:\\:getImport\\(\\) should return array\\<PhpMyAdmin\\\\Plugins\\\\ImportPlugin\\> but returns array\\<PhpMyAdmin\\\\Plugins\\\\Plugin\\>\\.$#"
5480-
count: 1
5481-
path: libraries/classes/Plugins.php
5482-
5483-
-
5484-
message: "#^Method PhpMyAdmin\\\\Plugins\\:\\:getSchema\\(\\) should return array\\<PhpMyAdmin\\\\Plugins\\\\SchemaPlugin\\> but returns array\\<PhpMyAdmin\\\\Plugins\\\\Plugin\\>\\.$#"
5485-
count: 1
5486-
path: libraries/classes/Plugins.php
5487-
54885473
-
54895474
message: "#^Parameter \\#2 \\$str2 of function strcmp expects string, class\\-string\\|false given\\.$#"
54905475
count: 1

0 commit comments

Comments
 (0)