Skip to content

Commit

Permalink
Replace deprecated $type instanceof ConstantStringType (szepeviktor…
Browse files Browse the repository at this point in the history
…#150)

* Update CurrentTimeDynamicFunctionReturnTypeExtension.php

* Update GetListTableDynamicFunctionReturnTypeExtension.php

* Update GetObjectTaxonomiesDynamicFunctionReturnTypeExtension.php

* Update GetTaxonomiesDynamicFunctionReturnTypeExtension.php

* Update WpThemeGetDynamicMethodReturnTypeExtension.php

* Update MySQL2DateDynamicFunctionReturnTypeExtension.php
  • Loading branch information
IanDelMar authored Feb 24, 2023
1 parent 318f106 commit 5c6c6c4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 61 deletions.
29 changes: 17 additions & 12 deletions src/CurrentTimeDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use PHPStan\Type\Type;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\TypeCombinator;

class CurrentTimeDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['current_time'], true);
return $functionReflection->getName() === 'current_time';
}

/**
Expand All @@ -29,22 +29,27 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$args = $functionCall->getArgs();
$argumentType = $scope->getType($args[0]->value);
$argumentType = $scope->getType($functionCall->getArgs()[0]->value);

// When called with a $type that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
if (count($argumentType->getConstantStrings()) === 0) {
return null;
}

// Called with a constant string $type
switch ($argumentType->getValue()) {
case 'timestamp':
case 'U':
return new IntegerType();
case 'mysql':
default:
return new StringType();
$returnType = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
switch ($constantString->getValue()) {
case 'timestamp':
case 'U':
$returnType[] = new IntegerType();
break;
case 'mysql':
default:
$returnType[] = new StringType();
}
}

return TypeCombinator::union(...$returnType);
}
}
13 changes: 7 additions & 6 deletions src/GetListTableDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\Constant\ConstantStringType;

class GetListTableDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
Expand All @@ -37,13 +36,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$argumentType = $scope->getType($args[0]->value);

// When called with a $class that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
if (count($argumentType->getConstantStrings()) === 0) {
return null;
}

return TypeCombinator::union(
new ObjectType($argumentType->getValue()),
new ConstantBooleanType(false)
);
$types = [new ConstantBooleanType(false)];
foreach ($argumentType->getConstantStrings() as $constantString) {
$types[] = new ObjectType($constantString->getValue());
}

return TypeCombinator::union(...$types);
}
}
22 changes: 14 additions & 8 deletions src/GetObjectTaxonomiesDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\TypeCombinator;

class GetObjectTaxonomiesDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
Expand All @@ -43,17 +43,23 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$argumentType = $scope->getType($args[1]->value);

// When called with an $output that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
if (count($argumentType->getConstantStrings()) === 0) {
return null;
}

// Called with a constant string $output
switch ($argumentType->getValue()) {
case 'objects':
return new ArrayType(new StringType(), new ObjectType('WP_Taxonomy'));
case 'names':
default:
return new ArrayType(new IntegerType(), new StringType());
$returnType = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
switch ($constantString->getValue()) {
case 'objects':
$returnType[] = new ArrayType(new StringType(), new ObjectType('WP_Taxonomy'));
break;
case 'names':
default:
$returnType[] = new ArrayType(new IntegerType(), new StringType());
}
}

return TypeCombinator::union(...$returnType);
}
}
34 changes: 19 additions & 15 deletions src/GetTaxonomiesDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\TypeCombinator;

class GetTaxonomiesDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['get_taxonomies'], true);
return $functionReflection->getName() === 'get_taxonomies';
}

/**
Expand All @@ -34,32 +33,37 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
{
$objectsReturnType = new ArrayType(new StringType(), new ObjectType('WP_Taxonomy'));
$namesReturnType = new ArrayType(new StringType(), new StringType());
$indeterminateReturnType = TypeCombinator::union(
$objectsReturnType,
$namesReturnType
);

$args = $functionCall->getArgs();
// Called without second $output arguments

// Called without second $output arguments
if (count($args) <= 1) {
return $namesReturnType;
}

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

// When called with a non-string $output, return default return type
if (! $argumentType instanceof ConstantStringType) {
return $indeterminateReturnType;
if (count($argumentType->getConstantStrings()) === 0) {
return TypeCombinator::union(
$objectsReturnType,
$namesReturnType
);
}

// Called with a string $output
switch ($argumentType->getValue()) {
case 'objects':
return $objectsReturnType;
case 'names':
default:
return $namesReturnType;
$returnType = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
switch ($constantString->getValue()) {
case 'objects':
$returnType[] = $objectsReturnType;
break;
case 'names':
default:
$returnType[] = $namesReturnType;
}
}

return TypeCombinator::union(...$returnType);
}
}
27 changes: 16 additions & 11 deletions src/MySQL2DateDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
use PHPStan\Type\Type;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;

class MySQL2DateDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['mysql2date'], true);
return $functionReflection->getName() === 'mysql2date';
}

/**
Expand All @@ -31,21 +31,26 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$args = $functionCall->getArgs();
$argumentType = $scope->getType($args[0]->value);
$argumentType = $scope->getType($functionCall->getArgs()[0]->value);

// When called with a $format that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
if (count($argumentType->getConstantStrings()) === 0) {
return null;
}

// Called with a constant string $format
switch ($argumentType->getValue()) {
case 'G':
case 'U':
return new UnionType([new ConstantBooleanType(false), new IntegerType()]);
default:
return new UnionType([new ConstantBooleanType(false), new StringType()]);
$returnType = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
switch ($constantString->getValue()) {
case 'G':
case 'U':
$returnType[] = new UnionType([new ConstantBooleanType(false), new IntegerType()]);
break;
default:
$returnType[] = new UnionType([new ConstantBooleanType(false), new StringType()]);
}
}

return TypeCombinator::union(...$returnType);
}
}
20 changes: 11 additions & 9 deletions src/WpThemeGetDynamicMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -62,22 +61,25 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
{
$argumentType = $scope->getType($methodCall->getArgs()[0]->value);

if (!$argumentType instanceof ConstantStringType) {
if (count($argumentType->getConstantStrings()) === 0) {
return TypeCombinator::union(
new StringType(),
new ArrayType(new IntegerType(), new StringType()),
new ConstantBooleanType(false)
);
}

if ($argumentType->getValue() === 'Tags') {
return new ArrayType(new IntegerType(), new StringType());
$returnType = [];
foreach ($argumentType->getConstantStrings() as $constantString) {
if ($constantString->getValue() === 'Tags') {
$returnType[] = new ArrayType(new IntegerType(), new StringType());
} elseif (in_array($constantString->getValue(), self::$headers, true)) {
$returnType[] = new StringType();
} else {
$returnType[] = new ConstantBooleanType(false);
}
}

if (in_array($argumentType->getValue(), self::$headers, true)) {
return new StringType();
}

return new ConstantBooleanType(false);
return TypeCombinator::union(...$returnType);
}
}

0 comments on commit 5c6c6c4

Please sign in to comment.