Skip to content

Commit 489e92f

Browse files
authored
Merge pull request #62 from johnbillion/_get_list_table
2 parents f7ff658 + 1925c94 commit 489e92f

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ services:
77
class: SzepeViktor\PHPStan\WordPress\WpThemeMagicPropertiesClassReflectionExtension
88
tags:
99
- phpstan.broker.propertiesClassReflectionExtension
10+
-
11+
class: SzepeViktor\PHPStan\WordPress\GetListTableDynamicFunctionReturnTypeExtension
12+
tags:
13+
- phpstan.broker.dynamicFunctionReturnTypeExtension
1014
-
1115
class: SzepeViktor\PHPStan\WordPress\RedirectCanonicalDynamicFunctionReturnTypeExtension
1216
tags:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Set return type of get_post().
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace SzepeViktor\PHPStan\WordPress;
10+
11+
use PhpParser\Node\Expr\FuncCall;
12+
use PHPStan\Analyser\Scope;
13+
use PHPStan\Reflection\FunctionReflection;
14+
use PHPStan\Reflection\ParametersAcceptorSelector;
15+
use PHPStan\Type\Type;
16+
use PHPStan\Type\ObjectType;
17+
use PHPStan\Type\Constant\ConstantBooleanType;
18+
use PHPStan\Type\TypeCombinator;
19+
use PHPStan\Type\Constant\ConstantStringType;
20+
21+
class GetListTableDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
22+
{
23+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
24+
{
25+
return $functionReflection->getName() === '_get_list_table';
26+
}
27+
28+
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
29+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
30+
{
31+
// Called without $class argument
32+
if (count($functionCall->args) < 1) {
33+
return new ConstantBooleanType(false);
34+
}
35+
36+
$argumentType = $scope->getType($functionCall->args[0]->value);
37+
38+
// When called with a $class that isn't a constant string, return default return type
39+
if (! $argumentType instanceof ConstantStringType) {
40+
return ParametersAcceptorSelector::selectFromArgs(
41+
$scope,
42+
$functionCall->args,
43+
$functionReflection->getVariants()
44+
)->getReturnType();
45+
}
46+
47+
return TypeCombinator::union(
48+
new ObjectType($argumentType->getValue()),
49+
new ConstantBooleanType(false)
50+
);
51+
}
52+
}

tests/data/_get_list_table.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SzepeViktor\PHPStan\WordPress\Tests;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
// 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'));
12+
13+
// Unknown class name
14+
assertType('\WP_List_Table|false', _get_list_table(_GET['foo']));

tests/testTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class DynamicReturnTypeExtensionTests extends TypeInferenceTestCase
1515
public function dataFileAsserts(): iterable
1616
{
1717
// path to a file with actual asserts of expected types:
18+
yield from $this->gatherAssertTypes(__DIR__ . '/data/_get_list_table.php');
1819
yield from $this->gatherAssertTypes(__DIR__ . '/data/current_time.php');
1920
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_comment.php');
2021
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_object_taxonomies.php');

0 commit comments

Comments
 (0)