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

Refactor get_sites() extension to improve readability #158

Merged
merged 3 commits into from
Feb 26, 2023
Merged
Changes from all commits
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
145 changes: 92 additions & 53 deletions src/GetSitesDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use WP_Site;

class GetSitesDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
/** @var list<mixed> $fields */
private $fields;

/** @var list<mixed> $count */
private $count;

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'get_sites';
}

/**
* @see https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter
* @see https://developer.wordpress.org/reference/functions/get_sites/
*
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter
*/
Expand All @@ -36,92 +44,123 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

// Called without arguments
if (count($args) === 0) {
return new ArrayType(new IntegerType(), new ObjectType(WP_Site::class));
return self::getDefaultType();
}

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

$this->fields = [];
$this->count = [];

// Called with a non constant argument
if (
count($argumentType->getConstantArrays()) === 0 &&
count($argumentType->getConstantStrings()) === 0
) {
return TypeCombinator::union(
new ArrayType(new IntegerType(), new ObjectType(WP_Site::class)),
new ArrayType(new IntegerType(), new IntegerType()),
new IntegerType()
);
return self::getIndeterminedType();
}

$fields = [];
$count = [];
$returnType = [];

// Called with a constant array argument
if (count($argumentType->getConstantArrays()) !== 0) {
foreach ($argumentType->getConstantArrays() as $constantArray) {
foreach ($constantArray->getKeyTypes() as $index => $key) {
if (count($key->getConstantStrings()) === 0) {
continue;
}
foreach ($key->getConstantStrings() as $constantKey) {
if (!in_array($constantKey->getValue(), ['fields', 'count'], true)) {
continue;
}
$fieldsType = $constantArray->getValueTypes()[$index];
if (count($fieldsType->getConstantScalarValues()) === 0) {
continue;
}

foreach ($fieldsType->getConstantScalarTypes() as $constantField) {
if ($constantKey->getValue() === 'fields') {
$fields[] = $constantField->getValue();
}
if ($constantKey->getValue() !== 'count') {
continue;
}

$count[] = (bool)$constantField->getValue();
}
}
}
}
if ($fields === []) {
$fields = [''];
}
if ($count === []) {
$count = [false];
$this->getValuesFromArray($constantArray);
}
}

// Called with a constant string argument
if (count($argumentType->getConstantStrings()) !== 0) {
foreach ($argumentType->getConstantStrings() as $constantString) {
parse_str($constantString->getValue(), $variables);
$fields[] = $variables['fields'] ?? '';
$count[] = isset($variables['count']) ? (bool)$variables['count'] : false;
$this->getValuesFromString($constantString);
}
}

if (in_array(true, $count, true) && count($count) === 1) {
return new IntegerType();
return TypeCombinator::union(...$this->getReturnTypeFromArgs());
}

/**
* @return list<\PHPStan\Type\IntegerType|\PHPStan\Type\ArrayType>
*/
private function getReturnTypeFromArgs(): array
{
if (in_array(true, $this->count, true) && count($this->count) === 1) {
return [new IntegerType()];
}

if (in_array(true, $count, true)) {
$returnType = [];

if (in_array(true, $this->count, true)) {
$returnType[] = new IntegerType();
}

if (in_array('ids', $fields, true)) {
if (in_array('ids', $this->fields, true)) {
$returnType[] = new ArrayType(new IntegerType(), new IntegerType());
}

if (
(in_array('ids', $fields, true) && count($fields) > 1) ||
(!in_array('ids', $fields, true) && count($fields) > 0)
(in_array('ids', $this->fields, true) && count($this->fields) > 1) ||
(!in_array('ids', $this->fields, true) && count($this->fields) > 0)
) {
$returnType[] = new ArrayType(new IntegerType(), new ObjectType(WP_Site::class));
$returnType[] = self::getDefaultType();
}

return $returnType;
}

private function getValuesFromArray(ConstantArrayType $constantArray): void
{
foreach ($constantArray->getKeyTypes() as $index => $key) {
if (count($key->getConstantStrings()) === 0) {
continue;
}
foreach ($key->getConstantStrings() as $constantKey) {
if (!in_array($constantKey->getValue(), ['fields', 'count'], true)) {
continue;
}
$fieldsType = $constantArray->getValueTypes()[$index];
if (count($fieldsType->getConstantScalarValues()) === 0) {
continue;
}

foreach ($fieldsType->getConstantScalarTypes() as $constantField) {
if ($constantKey->getValue() === 'fields') {
$this->fields[] = $constantField->getValue();
}
if ($constantKey->getValue() !== 'count') {
continue;
}

$this->count[] = (bool)$constantField->getValue();
}
}
}

return TypeCombinator::union(...$returnType);
// If fields and count are not set, add their default value.
if ($this->fields === []) {
$this->fields[] = '';
}
if ($this->count === []) { // phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
$this->count[] = false;
}
}

private function getValuesFromString(ConstantStringType $constantString): void
{
parse_str($constantString->getValue(), $variables);
$this->fields[] = $variables['fields'] ?? '';
$this->count[] = isset($variables['count']) ? (bool)$variables['count'] : false;
}

private static function getIndeterminedType(): Type
{
return TypeCombinator::union(
new ArrayType(new IntegerType(), new ObjectType(WP_Site::class)),
new ArrayType(new IntegerType(), new IntegerType()),
new IntegerType()
);
}

private static function getDefaultType(): ArrayType
{
return new ArrayType(new IntegerType(), new ObjectType(WP_Site::class));
}
}