|
15 | 15 | use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
|
16 | 16 | use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
|
17 | 17 | use Illuminate\Database\Eloquent\Casts\AsStringable;
|
| 18 | +use Illuminate\Database\Eloquent\Model; |
18 | 19 | use Illuminate\Support\Arr;
|
19 | 20 | use Illuminate\Support\Carbon as IlluminateCarbon;
|
20 | 21 | use Illuminate\Support\Collection;
|
21 | 22 | use Illuminate\Support\Facades\Date;
|
22 | 23 | use Illuminate\Support\Stringable as IlluminateStringable;
|
| 24 | +use PHPStan\Analyser\OutOfClassScope; |
| 25 | +use PHPStan\Reflection\ClassReflection; |
| 26 | +use PHPStan\Reflection\MissingMethodFromReflectionException; |
23 | 27 | use PHPStan\Reflection\ParameterReflection;
|
24 | 28 | use PHPStan\Reflection\ParametersAcceptorSelector;
|
25 | 29 | use PHPStan\Reflection\ReflectionProvider;
|
| 30 | +use PHPStan\ShouldNotHappenException; |
26 | 31 | use PHPStan\Type\Accessory\AccessoryNumericStringType;
|
27 | 32 | use PHPStan\Type\ArrayType;
|
28 | 33 | use PHPStan\Type\BenevolentUnionType;
|
|
36 | 41 | use PHPStan\Type\StringType;
|
37 | 42 | use PHPStan\Type\Type;
|
38 | 43 | use PHPStan\Type\TypeCombinator;
|
| 44 | +use ReflectionException; |
39 | 45 | use stdClass;
|
40 | 46 | use Stringable;
|
41 | 47 |
|
| 48 | +use function array_combine; |
| 49 | +use function array_key_exists; |
| 50 | +use function array_map; |
| 51 | +use function array_merge; |
42 | 52 | use function class_exists;
|
43 | 53 | use function explode;
|
| 54 | +use function str_replace; |
| 55 | +use function version_compare; |
44 | 56 |
|
45 | 57 | class ModelCastHelper
|
46 | 58 | {
|
| 59 | + /** @var array<string, array<string, string>> */ |
| 60 | + private array $modelCasts = []; |
| 61 | + |
47 | 62 | public function __construct(
|
48 | 63 | protected ReflectionProvider $reflectionProvider,
|
49 | 64 | ) {
|
@@ -193,4 +208,63 @@ private function parseCast(string $cast): string
|
193 | 208 |
|
194 | 209 | return $cast;
|
195 | 210 | }
|
| 211 | + |
| 212 | + public function hasCastForProperty(ClassReflection $modelClassReflection, string $propertyName): bool |
| 213 | + { |
| 214 | + if (! array_key_exists($modelClassReflection->getName(), $this->modelCasts)) { |
| 215 | + $modelCasts = $this->getModelCasts($modelClassReflection); |
| 216 | + } else { |
| 217 | + $modelCasts = $this->modelCasts[$modelClassReflection->getName()]; |
| 218 | + } |
| 219 | + |
| 220 | + return array_key_exists($propertyName, $modelCasts); |
| 221 | + } |
| 222 | + |
| 223 | + public function getCastForProperty(ClassReflection $modelClassReflection, string $propertyName): string|null |
| 224 | + { |
| 225 | + if (! array_key_exists($modelClassReflection->getName(), $this->modelCasts)) { |
| 226 | + $modelCasts = $this->getModelCasts($modelClassReflection); |
| 227 | + } else { |
| 228 | + $modelCasts = $this->modelCasts[$modelClassReflection->getName()]; |
| 229 | + } |
| 230 | + |
| 231 | + return $modelCasts[$propertyName] ?? null; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @return array<string, string> |
| 236 | + * |
| 237 | + * @throws ShouldNotHappenException |
| 238 | + * @throws MissingMethodFromReflectionException |
| 239 | + */ |
| 240 | + private function getModelCasts(ClassReflection $modelClassReflection): array |
| 241 | + { |
| 242 | + try { |
| 243 | + /** @var Model $modelInstance */ |
| 244 | + $modelInstance = $modelClassReflection->getNativeReflection()->newInstanceWithoutConstructor(); |
| 245 | + } catch (ReflectionException) { |
| 246 | + throw new ShouldNotHappenException(); |
| 247 | + } |
| 248 | + |
| 249 | + $modelCasts = $modelInstance->getCasts(); |
| 250 | + |
| 251 | + if (version_compare(LARAVEL_VERSION, '11.0.0', '>=')) { // @phpstan-ignore-line |
| 252 | + $castsMethodReturnType = ParametersAcceptorSelector::selectSingle($modelClassReflection->getMethod( |
| 253 | + 'casts', |
| 254 | + new OutOfClassScope(), |
| 255 | + )->getVariants())->getReturnType(); |
| 256 | + |
| 257 | + if ($castsMethodReturnType->isConstantArray()->yes()) { |
| 258 | + $modelCasts = array_merge( |
| 259 | + $modelCasts, |
| 260 | + array_combine( |
| 261 | + array_map(static fn ($key) => $key->getValue(), $castsMethodReturnType->getKeyTypes()), // @phpstan-ignore-line |
| 262 | + array_map(static fn ($value) => str_replace('\\\\', '\\', $value->getValue()), $castsMethodReturnType->getValueTypes()), // @phpstan-ignore-line |
| 263 | + ), |
| 264 | + ); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + return $modelCasts; |
| 269 | + } |
196 | 270 | }
|
0 commit comments