|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Type; |
| 15 | + |
| 16 | +use PhpParser\Node\Expr\FuncCall; |
| 17 | +use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Reflection\ClassReflection; |
| 19 | +use PHPStan\Reflection\FunctionReflection; |
| 20 | +use PHPStan\Reflection\ReflectionProvider; |
| 21 | +use PHPStan\ShouldNotHappenException; |
| 22 | +use PHPStan\Type\BooleanType; |
| 23 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 24 | +use PHPStan\Type\Constant\ConstantStringType; |
| 25 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 26 | +use PHPStan\Type\IntegerType; |
| 27 | +use PHPStan\Type\NonAcceptingNeverType; |
| 28 | +use PHPStan\Type\ObjectType; |
| 29 | +use PHPStan\Type\ObjectWithoutClassType; |
| 30 | +use PHPStan\Type\StringType; |
| 31 | +use PHPStan\Type\Type; |
| 32 | +use stdClass; |
| 33 | + |
| 34 | +final class FakeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @var array<string, class-string<Type>> |
| 38 | + */ |
| 39 | + private static array $notStringFormattedFields = [ |
| 40 | + 'success' => BooleanType::class, |
| 41 | + 'user_id' => IntegerType::class, |
| 42 | + ]; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var array<string, class-string<Type>> |
| 46 | + */ |
| 47 | + private static array $typeInterpolations = [ |
| 48 | + 'bool' => BooleanType::class, |
| 49 | + 'int' => IntegerType::class, |
| 50 | + ]; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var list<string> |
| 54 | + */ |
| 55 | + private array $dateFields = []; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param array<string, string> $notStringFormattedFieldsArray |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + private readonly FactoriesReturnTypeHelper $factoriesReturnTypeHelper, |
| 62 | + private readonly ReflectionProvider $reflectionProvider, |
| 63 | + array $notStringFormattedFieldsArray |
| 64 | + ) { |
| 65 | + foreach ($notStringFormattedFieldsArray as $field => $type) { |
| 66 | + if (! isset(self::$typeInterpolations[$type])) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + self::$notStringFormattedFields[$field] = self::$typeInterpolations[$type]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 75 | + { |
| 76 | + return $functionReflection->getName() === 'fake'; |
| 77 | + } |
| 78 | + |
| 79 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type |
| 80 | + { |
| 81 | + $arguments = $functionCall->getArgs(); |
| 82 | + |
| 83 | + if ($arguments === []) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + $modelType = $this->factoriesReturnTypeHelper->check($scope->getType($arguments[0]->value), 'model'); |
| 88 | + |
| 89 | + if (! $modelType->isObject()->yes()) { |
| 90 | + return new NonAcceptingNeverType(); |
| 91 | + } |
| 92 | + |
| 93 | + $classReflections = $modelType->getObjectClassReflections(); |
| 94 | + |
| 95 | + if (count($classReflections) !== 1) { |
| 96 | + return $modelType; // ObjectWithoutClassType |
| 97 | + } |
| 98 | + |
| 99 | + $classReflection = current($classReflections); |
| 100 | + |
| 101 | + $returnType = $this->getNativeStringPropertyValue($classReflection, $scope, 'returnType'); |
| 102 | + |
| 103 | + if ($returnType === 'object') { |
| 104 | + return new ObjectType(stdClass::class); |
| 105 | + } |
| 106 | + |
| 107 | + if ($returnType === 'array') { |
| 108 | + return $this->getArrayReturnType($classReflection, $scope); |
| 109 | + } |
| 110 | + |
| 111 | + if ($this->reflectionProvider->hasClass($returnType)) { |
| 112 | + return new ObjectType($returnType); |
| 113 | + } |
| 114 | + |
| 115 | + return new ObjectWithoutClassType(); |
| 116 | + } |
| 117 | + |
| 118 | + private function getArrayReturnType(ClassReflection $classReflection, Scope $scope): Type |
| 119 | + { |
| 120 | + $this->fillDateFields($classReflection, $scope); |
| 121 | + $fieldsTypes = $this->getNativePropertyType($classReflection, $scope, 'allowedFields')->getConstantArrays(); |
| 122 | + |
| 123 | + if ($fieldsTypes === []) { |
| 124 | + return new ConstantArrayType([], []); |
| 125 | + } |
| 126 | + |
| 127 | + $fields = array_filter(array_map( |
| 128 | + static fn (Type $type) => current($type->getConstantStrings()), |
| 129 | + current($fieldsTypes)->getValueTypes() |
| 130 | + )); |
| 131 | + |
| 132 | + return new ConstantArrayType( |
| 133 | + $fields, |
| 134 | + array_map(function (ConstantStringType $fieldType) use ($classReflection, $scope): Type { |
| 135 | + $field = $fieldType->getValue(); |
| 136 | + |
| 137 | + if (array_key_exists($field, self::$notStringFormattedFields)) { |
| 138 | + $type = self::$notStringFormattedFields[$field]; |
| 139 | + |
| 140 | + return new $type(); |
| 141 | + } |
| 142 | + |
| 143 | + if ( |
| 144 | + in_array($field, $this->dateFields, true) |
| 145 | + && $this->getNativeStringPropertyValue($classReflection, $scope, 'dateFormat') === 'int' |
| 146 | + ) { |
| 147 | + return new IntegerType(); |
| 148 | + } |
| 149 | + |
| 150 | + return new StringType(); |
| 151 | + }, $fields) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + private function fillDateFields(ClassReflection $classReflection, Scope $scope): void |
| 156 | + { |
| 157 | + foreach (['createdAt', 'updatedAt', 'deletedAt'] as $property) { |
| 158 | + if ($classReflection->hasNativeProperty($property)) { |
| 159 | + $this->dateFields[] = $this->getNativeStringPropertyValue($classReflection, $scope, $property); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private function getNativePropertyType(ClassReflection $classReflection, Scope $scope, string $property): Type |
| 165 | + { |
| 166 | + if (! $classReflection->hasNativeProperty($property)) { |
| 167 | + throw new ShouldNotHappenException(sprintf('Native property %s::$%s does not exist.', $classReflection->getDisplayName(), $property)); |
| 168 | + } |
| 169 | + |
| 170 | + return $scope->getType($classReflection->getNativeProperty($property)->getNativeReflection()->getDefaultValueExpression()); |
| 171 | + } |
| 172 | + |
| 173 | + private function getNativeStringPropertyValue(ClassReflection $classReflection, Scope $scope, string $property): string |
| 174 | + { |
| 175 | + $propertyType = $this->getNativePropertyType($classReflection, $scope, $property)->getConstantStrings(); |
| 176 | + assert(count($propertyType) === 1); |
| 177 | + |
| 178 | + return current($propertyType)->getValue(); |
| 179 | + } |
| 180 | +} |
0 commit comments