|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Nette; |
| 4 | + |
| 5 | +use Nette\Utils\Json; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 8 | +use PhpParser\Node\Expr\StaticCall; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\MethodReflection; |
| 12 | +use PHPStan\Type\ArrayType; |
| 13 | +use PHPStan\Type\BooleanType; |
| 14 | +use PHPStan\Type\Constant\ConstantStringType; |
| 15 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 16 | +use PHPStan\Type\FloatType; |
| 17 | +use PHPStan\Type\IntegerType; |
| 18 | +use PHPStan\Type\MixedType; |
| 19 | +use PHPStan\Type\ObjectType; |
| 20 | +use PHPStan\Type\StringType; |
| 21 | +use PHPStan\Type\Type; |
| 22 | +use PHPStan\Type\UnionType; |
| 23 | +use stdClass; |
| 24 | + |
| 25 | +final class JsonDecodeDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension |
| 26 | +{ |
| 27 | + |
| 28 | + public function getClass(): string |
| 29 | + { |
| 30 | + return 'Nette\Utils\Json'; |
| 31 | + } |
| 32 | + |
| 33 | + public function isStaticMethodSupported(MethodReflection $methodReflection): bool |
| 34 | + { |
| 35 | + return $methodReflection->getName() === 'decode'; |
| 36 | + } |
| 37 | + |
| 38 | + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type |
| 39 | + { |
| 40 | + $args = $methodCall->getArgs(); |
| 41 | + |
| 42 | + $isForceArray = $this->isForceArray($args); |
| 43 | + |
| 44 | + $firstArgValue = $args[0]->value; |
| 45 | + $firstValueType = $scope->getType($firstArgValue); |
| 46 | + |
| 47 | + if ($firstValueType instanceof ConstantStringType) { |
| 48 | + $resolvedType = $this->resolveConstantStringType($firstValueType, $isForceArray); |
| 49 | + } else { |
| 50 | + $resolvedType = new MixedType(); |
| 51 | + } |
| 52 | + |
| 53 | + if (! $resolvedType instanceof MixedType) { |
| 54 | + return $resolvedType; |
| 55 | + } |
| 56 | + |
| 57 | + // fallback type |
| 58 | + if ($isForceArray) { |
| 59 | + return new UnionType([ |
| 60 | + new ArrayType(new MixedType(), new MixedType()), |
| 61 | + new StringType(), |
| 62 | + new FloatType(), |
| 63 | + new IntegerType(), |
| 64 | + new BooleanType(), |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + // scalar types with stdClass |
| 69 | + return new UnionType([ |
| 70 | + new ObjectType(stdClass::class), |
| 71 | + new StringType(), |
| 72 | + new FloatType(), |
| 73 | + new IntegerType(), |
| 74 | + new BooleanType(), |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param Arg[] $args |
| 80 | + */ |
| 81 | + private function isForceArray(array $args): bool |
| 82 | + { |
| 83 | + if (!isset($args[1])) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $secondArg = $args[1]; |
| 88 | + |
| 89 | + // is second arg force array? |
| 90 | + if ($secondArg->value instanceof ClassConstFetch) { |
| 91 | + $classConstFetch = $secondArg->value; |
| 92 | + |
| 93 | + if ($classConstFetch->class instanceof Name) { |
| 94 | + if (! $classConstFetch->name instanceof \PhpParser\Node\Identifier) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + if ($classConstFetch->class->toString() !== 'Nette\Utils\Json') { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if ($classConstFetch->name->toString() === 'FORCE_ARRAY') { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + private function resolveConstantStringType(ConstantStringType $constantStringType, bool $isForceArray): Type |
| 112 | + { |
| 113 | + if ($isForceArray) { |
| 114 | + $decodedValue = Json::decode($constantStringType->getValue(), Json::FORCE_ARRAY); |
| 115 | + } else { |
| 116 | + $decodedValue = Json::decode($constantStringType->getValue()); |
| 117 | + } |
| 118 | + |
| 119 | + if (is_bool($decodedValue)) { |
| 120 | + return new BooleanType(); |
| 121 | + } |
| 122 | + |
| 123 | + if (is_array($decodedValue)) { |
| 124 | + return new ArrayType(new MixedType(), new MixedType()); |
| 125 | + } |
| 126 | + |
| 127 | + if (is_object($decodedValue) && get_class($decodedValue) === stdClass::class) { |
| 128 | + return new ObjectType(stdClass::class); |
| 129 | + } |
| 130 | + |
| 131 | + if (is_int($decodedValue)) { |
| 132 | + return new IntegerType(); |
| 133 | + } |
| 134 | + |
| 135 | + if (is_float($decodedValue)) { |
| 136 | + return new FloatType(); |
| 137 | + } |
| 138 | + |
| 139 | + return new MixedType(); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments