Skip to content

Commit 679adfe

Browse files
authored
Fix _get_list_table extension (#190)
1 parent 7af1af8 commit 679adfe

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/GetListTableDynamicFunctionReturnTypeExtension.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818

1919
class GetListTableDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2020
{
21+
private const CORE_CLASSES = [
22+
'WP_Posts_List_Table',
23+
'WP_Media_List_Table',
24+
'WP_Terms_List_Table',
25+
'WP_Users_List_Table',
26+
'WP_Comments_List_Table',
27+
'WP_Post_Comments_List_Table',
28+
'WP_Links_List_Table',
29+
'WP_Plugin_Install_List_Table',
30+
'WP_Themes_List_Table',
31+
'WP_Theme_Install_List_Table',
32+
'WP_Plugins_List_Table',
33+
'WP_Application_Passwords_List_Table',
34+
'WP_MS_Sites_List_Table',
35+
'WP_MS_Users_List_Table',
36+
'WP_MS_Themes_List_Table',
37+
'WP_Privacy_Data_Export_Requests_List_Table',
38+
'WP_Privacy_Data_Removal_Requests_List_Table',
39+
];
40+
2141
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2242
{
2343
return $functionReflection->getName() === '_get_list_table';
@@ -30,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3050

3151
// Called without $class argument
3252
if (count($args) < 1) {
33-
return new ConstantBooleanType(false);
53+
return null;
3454
}
3555

3656
$argumentType = $scope->getType($args[0]->value);
@@ -40,9 +60,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4060
return null;
4161
}
4262

43-
$types = [new ConstantBooleanType(false)];
63+
$types = [];
4464
foreach ($argumentType->getConstantStrings() as $constantString) {
45-
$types[] = new ObjectType($constantString->getValue());
65+
$types[] = in_array($constantString->getValue(), self::CORE_CLASSES, true)
66+
? new ObjectType($constantString->getValue())
67+
: new ConstantBooleanType(false);
4668
}
4769

4870
return TypeCombinator::union(...$types);

tests/data/_get_list_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace SzepeViktor\PHPStan\WordPress\Tests;
66

7+
use function _get_list_table;
78
use function PHPStan\Testing\assertType;
89

910
// Known class name
10-
assertType('WP_Posts_List_Table|false', _get_list_table('WP_Posts_List_Table'));
11-
assertType('Unknown_Table|false', _get_list_table('Unknown_Table'));
11+
assertType('WP_Posts_List_Table', _get_list_table('WP_Posts_List_Table'));
12+
assertType('false', _get_list_table('Unknown_Table'));
1213

1314
// Unknown class name
1415
assertType('WP_List_Table|false', _get_list_table($_GET['foo']));

0 commit comments

Comments
 (0)