|
| 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 <admin@codeigniter.com> |
| 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 PHPStan\Reflection\ClassMemberAccessAnswerer; |
| 17 | +use PHPStan\Reflection\Type\CallbackUnresolvedPropertyPrototypeReflection; |
| 18 | +use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
| 19 | +use PHPStan\Type\ObjectType; |
| 20 | +use PHPStan\Type\Type; |
| 21 | + |
| 22 | +/** |
| 23 | + * An entity object type whose listed properties are retyped by the `$casts` of the model that produced it. |
| 24 | + * CodeIgniter applies the producing model's casts before hydrating the entity, so an entity fetched through |
| 25 | + * a model's `asObject()`/`asArray()` picks up that model's casts instead of the bare column types. |
| 26 | + */ |
| 27 | +final class ModelCastEntityType extends ObjectType |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @param array<string, Type> $castedProperties Field name => cast-resolved type |
| 31 | + * @param array<string, string> $datamap Property name => field name |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + string $className, |
| 35 | + private readonly array $castedProperties, |
| 36 | + private readonly array $datamap = [], |
| 37 | + ) { |
| 38 | + parent::__construct($className); |
| 39 | + } |
| 40 | + |
| 41 | + public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 42 | + { |
| 43 | + return $this->withCastOverride($propertyName, parent::getUnresolvedInstancePropertyPrototype($propertyName, $scope)); |
| 44 | + } |
| 45 | + |
| 46 | + public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 47 | + { |
| 48 | + return $this->withCastOverride($propertyName, parent::getUnresolvedPropertyPrototype($propertyName, $scope)); |
| 49 | + } |
| 50 | + |
| 51 | + private function withCastOverride(string $propertyName, UnresolvedPropertyPrototypeReflection $prototype): UnresolvedPropertyPrototypeReflection |
| 52 | + { |
| 53 | + $field = $this->datamap[$propertyName] ?? $propertyName; |
| 54 | + |
| 55 | + if (! isset($this->castedProperties[$field])) { |
| 56 | + return $prototype; |
| 57 | + } |
| 58 | + |
| 59 | + $naked = $prototype->getNakedProperty(); |
| 60 | + $overrideType = $this->castedProperties[$field]; |
| 61 | + |
| 62 | + return new CallbackUnresolvedPropertyPrototypeReflection( |
| 63 | + $naked, |
| 64 | + $naked->getDeclaringClass(), |
| 65 | + false, |
| 66 | + static fn (Type $type): Type => $overrideType, |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
0 commit comments