1616use CodeIgniter \Entity \Entity ;
1717use CodeIgniter \I18n \Time ;
1818use CodeIgniter \PHPStan \Database \ModelTableMapProvider ;
19- use CodeIgniter \PHPStan \Database \Schema \CastTypeResolver ;
19+ use CodeIgniter \PHPStan \Database \Schema \CastFieldTypeResolver ;
2020use CodeIgniter \PHPStan \Database \Schema \Column ;
2121use CodeIgniter \PHPStan \Database \Schema \ColumnTypeResolver ;
2222use CodeIgniter \PHPStan \Database \SchemaProvider ;
2323use PHPStan \Reflection \ClassReflection ;
24- use PHPStan \Reflection \ParametersAcceptorSelector ;
2524use PHPStan \Reflection \PropertiesClassReflectionExtension ;
2625use PHPStan \Reflection \PropertyReflection ;
27- use PHPStan \Reflection \ReflectionProvider ;
2826use PHPStan \Type \MixedType ;
2927use PHPStan \Type \ObjectType ;
3028use PHPStan \Type \Type ;
31- use PHPStan \Type \TypeCombinator ;
3229
3330/**
3431 * Types virtual properties on `CodeIgniter\Entity\Entity` subclasses, layering `$dates` and `$casts`
3734final class EntityPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
3835{
3936 public function __construct (
40- private readonly ReflectionProvider $ reflectionProvider ,
41- private readonly CastTypeResolver $ castTypeResolver ,
37+ private readonly CastFieldTypeResolver $ castFieldTypeResolver ,
4238 private readonly SchemaProvider $ schemaProvider ,
4339 private readonly ModelTableMapProvider $ modelTableMapProvider ,
4440 private readonly ColumnTypeResolver $ columnTypeResolver ,
@@ -70,7 +66,7 @@ private function resolveType(ClassReflection $classReflection, string $propertyN
7066 }
7167
7268 if (isset ($ casts [$ column ])) {
73- return $ this ->resolveCastType ( $ classReflection , $ casts [$ column ]);
69+ return $ this ->castFieldTypeResolver -> resolve ( $ casts [$ column ], $ this -> readStringMap ( $ classReflection , ' castHandlers ' ) );
7470 }
7571
7672 if ($ schema !== null && ! $ this ->hasGetter ($ classReflection , $ column )) {
@@ -80,36 +76,6 @@ private function resolveType(ClassReflection $classReflection, string $propertyN
8076 return null ;
8177 }
8278
83- private function resolveCastType (ClassReflection $ classReflection , string $ cast ): Type
84- {
85- return $ this ->castTypeResolver ->resolve ($ cast ) ?? $ this ->resolveCustomHandlerType ($ classReflection , $ cast );
86- }
87-
88- private function resolveCustomHandlerType (ClassReflection $ classReflection , string $ cast ): Type
89- {
90- $ nullable = str_starts_with ($ cast , '? ' );
91-
92- if ($ nullable ) {
93- $ cast = substr ($ cast , 1 );
94- }
95-
96- $ handler = $ this ->readStringMap ($ classReflection , 'castHandlers ' )[$ this ->castName ($ cast )] ?? null ;
97-
98- if ($ handler === null || ! $ this ->reflectionProvider ->hasClass ($ handler )) {
99- return new MixedType ();
100- }
101-
102- $ handlerReflection = $ this ->reflectionProvider ->getClass ($ handler );
103-
104- if (! $ handlerReflection ->hasNativeMethod ('get ' )) {
105- return new MixedType ();
106- }
107-
108- $ type = ParametersAcceptorSelector::combineAcceptors ($ handlerReflection ->getNativeMethod ('get ' )->getVariants ())->getReturnType ();
109-
110- return $ nullable ? TypeCombinator::addNull ($ type ) : $ type ;
111- }
112-
11379 private function lookupColumn (ClassReflection $ classReflection , string $ column ): ?Column
11480 {
11581 $ table = $ this ->modelTableMapProvider ->getTableForEntity ($ classReflection ->getName ());
@@ -128,15 +94,6 @@ private function hasGetter(ClassReflection $classReflection, string $column): bo
12894 return $ classReflection ->hasNativeMethod ($ method ) || $ classReflection ->hasNativeMethod ('_ ' . $ method );
12995 }
13096
131- private function castName (string $ cast ): string
132- {
133- if (preg_match ('/\A(.+)\[.+\]\z/ ' , $ cast , $ matches ) === 1 ) {
134- return $ matches [1 ];
135- }
136-
137- return $ cast ;
138- }
139-
14097 private function mapColumn (ClassReflection $ classReflection , string $ propertyName ): string
14198 {
14299 $ mapped = $ this ->readStringMap ($ classReflection , 'datamap ' )[$ propertyName ] ?? '' ;
0 commit comments