@@ -376,6 +376,8 @@ private function areGenericTypesEqual(
376
376
$ returnTagValueNode ->type ,
377
377
$ node
378
378
);
379
+ $ relationUsesPivots = $ this ->typeComparator ->isSubtype ($ phpDocPHPStanType , new ObjectType ('Illuminate\Database\Eloquent\Relations\BelongsToMany ' ))
380
+ || $ this ->typeComparator ->isSubtype ($ phpDocPHPStanType , new ObjectType ('Illuminate\Database\Eloquent\Relations\MorphToMany ' ));
379
381
380
382
if (! $ phpDocPHPStanType instanceof GenericObjectType) {
381
383
return false ;
@@ -386,8 +388,13 @@ private function areGenericTypesEqual(
386
388
return false ;
387
389
}
388
390
389
- if (! $ this ->typeComparator ->areTypesEqual ($ phpDocTypes [0 ], new ObjectType ($ relatedClass ))) {
390
- return false ;
391
+ if (
392
+ $ this ->typeComparator ->areTypesEqual ($ phpDocTypes [0 ], new ObjectType ($ relatedClass ))
393
+ && count ($ phpDocTypes ) > 1
394
+ && $ phpDocTypes [1 ] instanceof ThisType
395
+ && ! $ relationUsesPivots
396
+ ) {
397
+ return true ;
391
398
}
392
399
393
400
if (! $ this ->shouldUseNewGenerics ) {
@@ -404,19 +411,21 @@ private function areGenericTypesEqual(
404
411
return $ this ->typeComparator ->areTypesEqual ($ phpDocTypes [1 ], new ObjectType ($ classForChildGeneric ));
405
412
}
406
413
407
- $ phpDocHasIntermediateGeneric = count ($ phpDocTypes ) === 3 ;
408
-
409
- if ($ classForIntermediateGeneric === null && ! $ phpDocHasIntermediateGeneric ) {
414
+ if ($ classForIntermediateGeneric === null && $ relationUsesPivots ) {
410
415
// If there are less than three generics, it means method is using the old format. We should update it.
411
416
if (count ($ phpDocTypes ) < 3 ) {
412
417
return false ;
413
418
}
414
419
415
420
// We want to convert the existing relationship definition to use `$this` as the second generic
416
- return $ phpDocTypes [1 ] instanceof ThisType;
421
+ // but only if the PHPDoc Tag doesn't look valid already
422
+ return
423
+ $ this ->typeComparator ->areTypesEqual ($ phpDocTypes [0 ], new ObjectType ($ relatedClass ))
424
+ && $ phpDocTypes [1 ] instanceof ThisType
425
+ && $ this ->typeComparator ->isSubtype ($ phpDocTypes [2 ], new ObjectType ('Illuminate\Database\Eloquent\Relations\Pivot ' ));
417
426
}
418
427
419
- if ($ classForIntermediateGeneric === null || ! $ phpDocHasIntermediateGeneric ) {
428
+ if ($ classForIntermediateGeneric === null ) {
420
429
return false ;
421
430
}
422
431
0 commit comments