Skip to content

Commit 7c244d5

Browse files
authored
Merge pull request #61 from johnbillion/tests-get-post
2 parents c7fd1ac + a3c271e commit 7c244d5

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/GetPostDynamicFunctionReturnTypeExtension.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Expr\FuncCall;
1313
use PHPStan\Analyser\Scope;
1414
use PHPStan\Reflection\FunctionReflection;
15+
use PHPStan\Reflection\ParametersAcceptorSelector;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\ArrayType;
1718
use PHPStan\Type\StringType;
@@ -20,6 +21,7 @@
2021
use PHPStan\Type\ObjectType;
2122
use PHPStan\Type\NullType;
2223
use PHPStan\Type\TypeCombinator;
24+
use PHPStan\Type\Constant\ConstantStringType;
2325

2426
class GetPostDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2527
{
@@ -32,6 +34,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3234
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3335
{
3436
$output = 'OBJECT';
37+
38+
if (count($functionCall->args) >= 2) {
39+
$argumentType = $scope->getType($functionCall->args[1]->value);
40+
41+
// When called with an $output that isn't a constant string, return default return type
42+
if (! $argumentType instanceof ConstantStringType) {
43+
return ParametersAcceptorSelector::selectFromArgs(
44+
$scope,
45+
$functionCall->args,
46+
$functionReflection->getVariants()
47+
)->getReturnType();
48+
}
49+
}
50+
3551
if (count($functionCall->args) >= 2 && $functionCall->args[1]->value instanceof ConstFetch) {
3652
$output = $functionCall->args[1]->value->name->getLast();
3753
}

tests/data/get_post.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SzepeViktor\PHPStan\WordPress\Tests;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
// Default output
10+
assertType('WP_Post|null', get_post(1));
11+
assertType('WP_Post|null', get_post(1,OBJECT));
12+
assertType('WP_Post|null', get_post(1,'Hello'));
13+
14+
// Unknown output
15+
assertType('array|\WP_Post|null', get_post(1,_GET['foo']));
16+
17+
// Associative array output
18+
assertType('array<string, mixed>|null', get_post(1,ARRAY_A));
19+
20+
// Numeric array output
21+
assertType('array<int, mixed>|null', get_post(1,ARRAY_N));

tests/testTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function dataFileAsserts(): iterable
1818
yield from $this->gatherAssertTypes(__DIR__ . '/data/current_time.php');
1919
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_comment.php');
2020
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_object_taxonomies.php');
21+
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_post.php');
2122
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysql2date.php');
2223
}
2324

0 commit comments

Comments
 (0)