Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated instanceof #184

Merged
merged 5 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions src/GetPostsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

namespace SzepeViktor\PHPStan\WordPress;

use WP_Post;
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\UnionType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\Constant\ConstantStringType;

use function count;

class GetPostsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
Expand All @@ -36,43 +40,64 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

// Called without arguments
if (count($args) === 0) {
return new ArrayType(new IntegerType(), new ObjectType('WP_Post'));
return self::getObjectType();
}

$argumentType = $scope->getType($args[0]->value);
$returnTypes = [];

// Called with an array argument
if ($argumentType instanceof ConstantArrayType) {
foreach ($argumentType->getKeyTypes() as $index => $key) {
if (! $key instanceof ConstantStringType || $key->getValue() !== 'fields') {
continue;
}
foreach ($argumentType->getArrays() as $array) {
if (!$array->isConstantArray()->yes()) {
$returnTypes[] = self::getIndeterminedType();
continue;
}

if ($array->hasOffsetValueType(new ConstantStringType('fields'))->no()) {
$returnTypes[] = self::getObjectType();
continue;
}

if ($array->hasOffsetValueType(new ConstantStringType('fields'))->maybe()) {
$returnTypes[] = self::getIndeterminedType();
continue;
}

$fieldsValueType = $array->getOffsetValueType(new ConstantStringType('fields'));
$constantFieldsValueTypes = $fieldsValueType->getConstantStrings();

if ($fieldsValueType instanceof UnionType && count($fieldsValueType->getTypes()) !== count($constantFieldsValueTypes)) {
$returnTypes[] = self::getIndeterminedType();
}

$fieldsType = $argumentType->getValueTypes()[$index];
if ($fieldsType instanceof ConstantStringType) {
$fields = $fieldsType->getValue();
foreach ($constantFieldsValueTypes as $constantFieldsValueType) {
switch ($constantFieldsValueType->getValue()) {
case 'id=>parent':
case 'ids':
$returnTypes[] = self::getIntType();
break;
default:
$returnTypes[] = self::getObjectType();
}
break;
}
}
// Called with a string argument
if ($argumentType instanceof ConstantStringType) {
parse_str($argumentType->getValue(), $variables);
$fields = $variables['fields'] ?? 'all';
}
return TypeCombinator::union(...$returnTypes);
}

// Without constant argument return default return type
if (! isset($fields)) {
return new ArrayType(new IntegerType(), new ObjectType('WP_Post'));
}
private static function getIntType(): Type
{
return new ArrayType(new IntegerType(), new IntegerType());
}

switch ($fields) {
case 'id=>parent':
case 'ids':
return new ArrayType(new IntegerType(), new IntegerType());
case 'all':
default:
return new ArrayType(new IntegerType(), new ObjectType('WP_Post'));
}
private static function getObjectType(): Type
{
return new ArrayType(new IntegerType(), new ObjectType(WP_Post::class));
}

private static function getIndeterminedType(): Type
{
return TypeCombinator::union(
new ArrayType(new IntegerType(), new ObjectType(WP_Post::class)),
new ArrayType(new IntegerType(), new IntegerType()),
);
}
}
38 changes: 37 additions & 1 deletion tests/data/get_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,42 @@
use function PHPStan\Testing\assertType;

assertType('array<int, WP_Post>', get_posts());
assertType('array<int, WP_Post>', get_posts(['fields' => 'all']));
assertType('array<int, WP_Post>', get_posts(['key' => 'value']));
assertType('array<int, WP_Post>', get_posts(['fields' => '']));
assertType('array<int, int>', get_posts(['fields' => 'ids']));
assertType('array<int, int>', get_posts(['fields' => 'id=>parent']));
assertType('array<int, WP_Post>', get_posts(['fields' => 'Hello']));

// Non constant array
assertType('array<int, int|WP_Post>', get_posts((array)$_GET['array']));

// Unions
$union = $_GET['foo'] ? ['key' => 'value'] : ['some' => 'thing'];
assertType('array<int, WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'ids'];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => ''];
assertType('array<int, WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'id=>parent'];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'ids'];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'id=>parent'];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? ['fields' => 'ids'] : ['fields' => 'id=>parent'];
assertType('array<int, int>', get_posts($union));

$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => ''];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'ids'];
assertType('array<int, int|WP_Post>', get_posts($union));

$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'id=>parent'];
assertType('array<int, int|WP_Post>', get_posts($union));