-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
969e12d
commit 9dbf2f8
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
<?php | ||
<?php declare(strict_types = 1); | ||
/** | ||
* Set return type of get_post(). | ||
* TODO https://developer.wordpress.org/reference/functions/get_post/ | ||
*/ | ||
*/ | ||
|
||
namespace PHPStan\WordPress; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class GetPostDynamicFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'get_post'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$output = 'OBJECT'; | ||
$argsCount = count($functionCall->args); | ||
if ($argsCount >= 2 && $functionCall->args[1]->value !== 'OBJECT') { | ||
$output = $functionCall->args[1]->value; | ||
} | ||
if ($output === 'ARRAY_A') { | ||
return TypeCombinator::union(new ArrayType(new StringType(), new MixedType()), new NullType()); | ||
} | ||
if ($output === 'ARRAY_N') { | ||
return TypeCombinator::union(new ArrayType(new IntegerType(), new MixedType()), new NullType()); | ||
} | ||
|
||
return TypeCombinator::union(new ObjectType('WP_Post'), new NullType()); | ||
} | ||
} |