@@ -219,7 +219,7 @@ protected function getOptions()
219219 ],
220220 ['nowrite ' , 'N ' , InputOption::VALUE_NONE , 'Don \'t write to Model file ' ],
221221 ['reset ' , 'R ' , InputOption::VALUE_NONE , 'Remove the original phpdocs instead of appending ' ],
222- ['smart-reset ' , 'r ' , InputOption::VALUE_NONE , 'Refresh the properties/methods list, but keep the text ' ],
222+ ['smart-reset ' , 'r ' , InputOption::VALUE_NONE , 'Retained for compatibility, while it no longer has any effect ' ],
223223 ['phpstorm-noinspections ' , 'p ' , InputOption::VALUE_NONE ,
224224 'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' .
225225 'noinspection tags ' ,
@@ -412,8 +412,6 @@ public function castPropertiesType($model)
412412 case 'immutable_datetime ' :
413413 $ realType = '\Carbon\CarbonImmutable ' ;
414414 break ;
415- case AsCollection::class:
416- case AsEnumCollection::class:
417415 case 'collection ' :
418416 $ realType = '\Illuminate\Support\Collection ' ;
419417 break ;
@@ -439,6 +437,18 @@ public function castPropertiesType($model)
439437 continue ;
440438 }
441439
440+ if (Str::startsWith ($ type , AsCollection::class)) {
441+ $ realType = $ this ->getTypeInModel ($ model , $ params [0 ] ?? null ) ?? '\Illuminate\Support\Collection ' ;
442+ }
443+
444+ if (Str::startsWith ($ type , AsEnumCollection::class)) {
445+ $ realType = '\Illuminate\Support\Collection ' ;
446+ $ relatedModel = $ this ->getTypeInModel ($ model , $ params [0 ] ?? null );
447+ if ($ relatedModel ) {
448+ $ realType = $ this ->getCollectionTypeHint ($ realType , $ relatedModel );
449+ }
450+ }
451+
442452 $ realType = $ this ->checkForCastableCasts ($ realType , $ params );
443453 $ realType = $ this ->checkForCustomLaravelCasts ($ realType );
444454 $ realType = $ this ->getTypeOverride ($ realType );
@@ -555,7 +565,7 @@ public function getPropertiesFromTable($model)
555565 $ this ->setMethod (
556566 Str::camel ('where_ ' . $ name ),
557567 $ this ->getClassNameInDestinationFile ($ model , $ builderClass )
558- . '| '
568+ . '<static> | '
559569 . $ this ->getClassNameInDestinationFile ($ model , get_class ($ model )),
560570 ['$value ' ]
561571 );
@@ -634,14 +644,14 @@ public function getPropertiesFromMethods($model)
634644 new ReflectionClass ($ model ),
635645 get_class ($ model )
636646 );
637- $ this ->setMethod ($ name , $ builder . '| ' . $ modelName , $ args , $ comment );
647+ $ this ->setMethod ($ name , $ builder . '<static> | ' . $ modelName , $ args , $ comment );
638648 }
639649 } elseif (in_array ($ method , ['query ' , 'newQuery ' , 'newModelQuery ' ])) {
640650 $ builder = $ this ->getClassNameInDestinationFile ($ model , get_class ($ model ->newModelQuery ()));
641651
642652 $ this ->setMethod (
643653 $ method ,
644- $ builder . '| ' . $ this ->getClassNameInDestinationFile ($ model , get_class ($ model ))
654+ $ builder . '<static> | ' . $ this ->getClassNameInDestinationFile ($ model , get_class ($ model ))
645655 );
646656
647657 if ($ this ->write_model_external_builder_methods ) {
@@ -716,14 +726,25 @@ public function getPropertiesFromMethods($model)
716726 if ($ relationObj instanceof BelongsToMany) {
717727 $ pivot = get_class ($ relationObj ->newPivot ());
718728 if (!in_array ($ pivot , [Pivot::class, MorphPivot::class])) {
729+ $ pivot = $ this ->getClassNameInDestinationFile ($ model , $ pivot );
730+
731+ if ($ existingPivot = ($ this ->properties [$ relationObj ->getPivotAccessor ()] ?? null )) {
732+ // If the pivot is already set, we need to append the type to it
733+ $ pivot .= '| ' . $ existingPivot ['type ' ];
734+ } else {
735+ // pivots are not always set
736+ $ pivot .= '|null ' ;
737+ }
738+
719739 $ this ->setProperty (
720740 $ relationObj ->getPivotAccessor (),
721- $ this -> getClassNameInDestinationFile ( $ model , $ pivot) ,
741+ $ pivot ,
722742 true ,
723743 false
724744 );
725745 }
726746 }
747+
727748 //Collection or array of models (because Collection is Arrayable)
728749 $ relatedClass = '\\' . get_class ($ relationObj ->getRelated ());
729750 $ collectionClass = $ this ->getCollectionClass ($ relatedClass );
@@ -907,7 +928,7 @@ public function getMethodType(Model $model, string $classType)
907928 {
908929 $ modelName = $ this ->getClassNameInDestinationFile ($ model , get_class ($ model ));
909930 $ builder = $ this ->getClassNameInDestinationFile ($ model , $ classType );
910- return $ builder . '| ' . $ modelName ;
931+ return $ builder . '<static> | ' . $ modelName ;
911932 }
912933
913934 /**
@@ -926,13 +947,19 @@ protected function createPhpDocs($class)
926947 $ reflection ->getParentClass ()->getInterfaceNames ()
927948 );
928949
950+ $ phpdoc = new DocBlock ($ reflection , new Context ($ namespace ));
929951 if ($ this ->reset ) {
930- $ phpdoc = new DocBlock ('' , new Context ($ namespace ));
931952 $ phpdoc ->setText (
932953 (new DocBlock ($ reflection , new Context ($ namespace )))->getText ()
933954 );
934- } else {
935- $ phpdoc = new DocBlock ($ reflection , new Context ($ namespace ));
955+ foreach ($ phpdoc ->getTags () as $ tag ) {
956+ if (
957+ in_array ($ tag ->getName (), ['property ' , 'property-read ' , 'property-write ' , 'method ' , 'mixin ' ])
958+ || ($ tag ->getName () === 'noinspection ' && in_array ($ tag ->getContent (), ['PhpUnnecessaryFullyQualifiedNameInspection ' , 'PhpFullyQualifiedNameUsageInspection ' ]))
959+ ) {
960+ $ phpdoc ->deleteTag ($ tag );
961+ }
962+ }
936963 }
937964
938965 $ properties = [];
@@ -1322,9 +1349,9 @@ protected function getSoftDeleteMethods($model)
13221349 if (in_array ('Illuminate \\Database \\Eloquent \\SoftDeletes ' , $ traits )) {
13231350 $ modelName = $ this ->getClassNameInDestinationFile ($ model , get_class ($ model ));
13241351 $ builder = $ this ->getClassNameInDestinationFile ($ model , \Illuminate \Database \Eloquent \Builder::class);
1325- $ this ->setMethod ('withTrashed ' , $ builder . '| ' . $ modelName , []);
1326- $ this ->setMethod ('withoutTrashed ' , $ builder . '| ' . $ modelName , []);
1327- $ this ->setMethod ('onlyTrashed ' , $ builder . '| ' . $ modelName , []);
1352+ $ this ->setMethod ('withTrashed ' , $ builder . '<static> | ' . $ modelName , []);
1353+ $ this ->setMethod ('withoutTrashed ' , $ builder . '<static> | ' . $ modelName , []);
1354+ $ this ->setMethod ('onlyTrashed ' , $ builder . '<static> | ' . $ modelName , []);
13281355 }
13291356 }
13301357
@@ -1528,7 +1555,7 @@ protected function writeModelExternalBuilderMethods(Model $model): void
15281555
15291556 $ this ->setMethod (
15301557 $ builderMethod ,
1531- $ builderClassBasedOnFQCNOption . '| ' . $ this ->getClassNameInDestinationFile ($ model , get_class ($ model )),
1558+ $ builderClassBasedOnFQCNOption . '<static> | ' . $ this ->getClassNameInDestinationFile ($ model , get_class ($ model )),
15321559 $ args
15331560 );
15341561 }
0 commit comments