Skip to content

Commit

Permalink
Fix _get_list_table extension (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Jun 16, 2023
1 parent 7af1af8 commit 679adfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
28 changes: 25 additions & 3 deletions src/GetListTableDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@

class GetListTableDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
private const CORE_CLASSES = [
'WP_Posts_List_Table',
'WP_Media_List_Table',
'WP_Terms_List_Table',
'WP_Users_List_Table',
'WP_Comments_List_Table',
'WP_Post_Comments_List_Table',
'WP_Links_List_Table',
'WP_Plugin_Install_List_Table',
'WP_Themes_List_Table',
'WP_Theme_Install_List_Table',
'WP_Plugins_List_Table',
'WP_Application_Passwords_List_Table',
'WP_MS_Sites_List_Table',
'WP_MS_Users_List_Table',
'WP_MS_Themes_List_Table',
'WP_Privacy_Data_Export_Requests_List_Table',
'WP_Privacy_Data_Removal_Requests_List_Table',
];

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === '_get_list_table';
Expand All @@ -30,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

// Called without $class argument
if (count($args) < 1) {
return new ConstantBooleanType(false);
return null;
}

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

$types = [new ConstantBooleanType(false)];
$types = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
$types[] = new ObjectType($constantString->getValue());
$types[] = in_array($constantString->getValue(), self::CORE_CLASSES, true)
? new ObjectType($constantString->getValue())
: new ConstantBooleanType(false);
}

return TypeCombinator::union(...$types);
Expand Down
5 changes: 3 additions & 2 deletions tests/data/_get_list_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use function _get_list_table;
use function PHPStan\Testing\assertType;

// Known class name
assertType('WP_Posts_List_Table|false', _get_list_table('WP_Posts_List_Table'));
assertType('Unknown_Table|false', _get_list_table('Unknown_Table'));
assertType('WP_Posts_List_Table', _get_list_table('WP_Posts_List_Table'));
assertType('false', _get_list_table('Unknown_Table'));

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

0 comments on commit 679adfe

Please sign in to comment.