@@ -576,10 +576,7 @@ public function getPropertiesFromMethods($model)
576
576
// methods that resemble mutators but aren't.
577
577
$ reflections = array_filter ($ reflections , function (\ReflectionMethod $ methodReflection ) {
578
578
return !$ methodReflection ->isPrivate () && !(
579
- in_array (
580
- \Illuminate \Database \Eloquent \Concerns \HasAttributes::class,
581
- $ methodReflection ->getDeclaringClass ()->getTraitNames ()
582
- ) && (
579
+ $ methodReflection ->getDeclaringClass ()->getName () === \Illuminate \Database \Eloquent \Model::class && (
583
580
$ methodReflection ->getName () === 'setClassCastableAttribute ' ||
584
581
$ methodReflection ->getName () === 'setEnumCastableAttribute '
585
582
)
@@ -605,18 +602,15 @@ public function getPropertiesFromMethods($model)
605
602
$ this ->setProperty ($ name , $ type , true , null , $ comment );
606
603
}
607
604
} elseif ($ isAttribute ) {
608
- $ name = Str::snake ($ method );
609
- $ types = $ this ->getAttributeReturnType ($ model , $ reflection );
610
- $ comment = $ this ->getCommentFromDocBlock ($ reflection );
611
-
612
- if ($ types ->has ('get ' )) {
613
- $ type = $ this ->getTypeInModel ($ model , $ types ['get ' ]);
614
- $ this ->setProperty ($ name , $ type , true , null , $ comment );
615
- }
616
-
617
- if ($ types ->has ('set ' )) {
618
- $ this ->setProperty ($ name , null , null , true , $ comment );
619
- }
605
+ $ types = $ this ->getAttributeTypes ($ model , $ reflection );
606
+ $ type = $ this ->getTypeInModel ($ model , $ types ->get ('get ' ) ?: $ types ->get ('set ' )) ?: null ;
607
+ $ this ->setProperty (
608
+ Str::snake ($ method ),
609
+ $ type ,
610
+ $ types ->has ('get ' ),
611
+ $ types ->has ('set ' ),
612
+ $ this ->getCommentFromDocBlock ($ reflection )
613
+ );
620
614
} elseif (
621
615
Str::startsWith ($ method , 'set ' ) && Str::endsWith (
622
616
$ method ,
@@ -1136,21 +1130,33 @@ protected function hasCamelCaseModelProperties()
1136
1130
return $ this ->laravel ['config ' ]->get ('ide-helper.model_camel_case_properties ' , false );
1137
1131
}
1138
1132
1139
- protected function getAttributeReturnType (Model $ model , \ReflectionMethod $ reflectionMethod ): Collection
1133
+ protected function getAttributeTypes (Model $ model , \ReflectionMethod $ reflectionMethod ): Collection
1140
1134
{
1141
1135
// Private/protected ReflectionMethods require setAccessible prior to PHP 8.1
1142
1136
$ reflectionMethod ->setAccessible (true );
1143
1137
1144
1138
/** @var Attribute $attribute */
1145
1139
$ attribute = $ reflectionMethod ->invoke ($ model );
1146
1140
1147
- return collect ([
1148
- 'get ' => $ attribute ->get ? optional (new \ReflectionFunction ($ attribute ->get ))->getReturnType () : null ,
1149
- 'set ' => $ attribute ->set ? optional (new \ReflectionFunction ($ attribute ->set ))->getReturnType () : null ,
1150
- ])
1151
- ->filter ()
1141
+ $ methods = new Collection ();
1142
+
1143
+ if ($ attribute ->get ) {
1144
+ $ methods ['get ' ] = optional (new \ReflectionFunction ($ attribute ->get ))->getReturnType ();
1145
+ }
1146
+ if ($ attribute ->set ) {
1147
+ $ function = optional (new \ReflectionFunction ($ attribute ->set ));
1148
+ if ($ function ->getNumberOfParameters () === 0 ) {
1149
+ $ methods ['set ' ] = null ;
1150
+ } else {
1151
+ $ methods ['set ' ] = $ function ->getParameters ()[0 ]->getType ();
1152
+ }
1153
+ }
1154
+
1155
+ return $ methods
1152
1156
->map (function ($ type ) {
1153
- if ($ type instanceof \ReflectionUnionType) {
1157
+ if ($ type === null ) {
1158
+ $ types = collect ([]);
1159
+ } elseif ($ type instanceof \ReflectionUnionType) {
1154
1160
$ types = collect ($ type ->getTypes ())
1155
1161
/** @var ReflectionType $reflectionType */
1156
1162
->map (function ($ reflectionType ) {
@@ -1161,7 +1167,7 @@ protected function getAttributeReturnType(Model $model, \ReflectionMethod $refle
1161
1167
$ types = collect ($ this ->extractReflectionTypes ($ type ));
1162
1168
}
1163
1169
1164
- if ($ type ->allowsNull ()) {
1170
+ if ($ type && $ type ->allowsNull ()) {
1165
1171
$ types ->push ('null ' );
1166
1172
}
1167
1173
@@ -1415,8 +1421,7 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
1415
1421
{
1416
1422
$ reflection = $ model instanceof ReflectionClass
1417
1423
? $ model
1418
- : new ReflectionObject ($ model )
1419
- ;
1424
+ : new ReflectionObject ($ model );
1420
1425
1421
1426
$ className = trim ($ className , '\\' );
1422
1427
$ writingToExternalFile = !$ this ->write || $ this ->write_mixin ;
0 commit comments