-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
get_sites()
dynamic function return type extension (#117)
* Add tests for `get_posts()` * Add `get_sites()` dynamic function return type extension * Simplify switch * Simplify `isFunctionSupported` * No null coalescing
- Loading branch information
1 parent
c9fb3dc
commit e644df7
Showing
6 changed files
with
105 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/** | ||
* Set return type of get_sites(). | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SzepeViktor\PHPStan\WordPress; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
|
||
class GetSitesDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'get_sites'; | ||
} | ||
|
||
/** | ||
* @see https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter | ||
*/ | ||
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter | ||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$args = $functionCall->getArgs(); | ||
|
||
// Called without arguments | ||
if (count($args) === 0) { | ||
return new ArrayType(new IntegerType(), new ObjectType('WP_Site')); | ||
} | ||
|
||
$fields = ''; | ||
|
||
$argumentType = $scope->getType($args[0]->value); | ||
|
||
// Called with an array argument | ||
if ($argumentType instanceof ConstantArrayType) { | ||
foreach ($argumentType->getKeyTypes() as $index => $key) { | ||
if (! $key instanceof ConstantStringType || $key->getValue() !== 'fields') { | ||
continue; | ||
} | ||
|
||
$fieldsType = $argumentType->getValueTypes()[$index]; | ||
if ($fieldsType instanceof ConstantStringType) { | ||
$fields = $fieldsType->getValue(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
// Called with a string argument | ||
if ($argumentType instanceof ConstantStringType) { | ||
parse_str($argumentType->getValue(), $variables); | ||
$fields = $variables['fields'] ?? 'all'; | ||
} | ||
|
||
switch ($fields) { | ||
case 'count': | ||
return new IntegerType(); | ||
case 'ids': | ||
return new ArrayType(new IntegerType(), new IntegerType()); | ||
default: | ||
return new ArrayType(new IntegerType(), new ObjectType('WP_Site')); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SzepeViktor\PHPStan\WordPress\Tests; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('array<int, WP_Post>', get_posts()); | ||
assertType('array<int, WP_Post>', get_posts(['fields' => 'all'])); | ||
assertType('array<int, int>', get_posts(['fields' => 'ids'])); | ||
assertType('array<int, int>', get_posts(['fields' => 'id=>parent'])); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SzepeViktor\PHPStan\WordPress\Tests; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('array<int, WP_Site>', get_sites()); | ||
assertType('int', get_sites(['fields' => 'count'])); | ||
assertType('array<int, int>', get_sites(['fields' => 'ids'])); |