Skip to content

Commit 4378a24

Browse files
peterfoxGeniJaho
andauthored
Stop overriding previous tags (#331)
* Stop overriding previous tags * Add check for multiple PHPDoc types in AddGenericReturnTypeToRelationsRector --------- Co-authored-by: Geni Jaho <[email protected]>
1 parent bacfebe commit 4378a24

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ private function areGenericTypesEqual(
376376
$returnTagValueNode->type,
377377
$node
378378
);
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'));
379381

380382
if (! $phpDocPHPStanType instanceof GenericObjectType) {
381383
return false;
@@ -386,8 +388,13 @@ private function areGenericTypesEqual(
386388
return false;
387389
}
388390

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;
391398
}
392399

393400
if (! $this->shouldUseNewGenerics) {
@@ -404,19 +411,21 @@ private function areGenericTypesEqual(
404411
return $this->typeComparator->areTypesEqual($phpDocTypes[1], new ObjectType($classForChildGeneric));
405412
}
406413

407-
$phpDocHasIntermediateGeneric = count($phpDocTypes) === 3;
408-
409-
if ($classForIntermediateGeneric === null && ! $phpDocHasIntermediateGeneric) {
414+
if ($classForIntermediateGeneric === null && $relationUsesPivots) {
410415
// If there are less than three generics, it means method is using the old format. We should update it.
411416
if (count($phpDocTypes) < 3) {
412417
return false;
413418
}
414419

415420
// 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'));
417426
}
418427

419-
if ($classForIntermediateGeneric === null || ! $phpDocHasIntermediateGeneric) {
428+
if ($classForIntermediateGeneric === null) {
420429
return false;
421430
}
422431

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\Fixture\NewGenerics;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
10+
use RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\Source\SomeModel;
11+
use Illuminate\Database\Eloquent\Relations\Pivot;
12+
13+
class SkipAlreadySetGeneric extends Model
14+
{
15+
/**
16+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<SomeModel, $this>
17+
*/
18+
public function account(): BelongsTo
19+
{
20+
return $this->belongsTo(SomeModel::class);
21+
}
22+
23+
/**
24+
* @return \Illuminate\Database\Eloquent\Relations\HasMany<SomeModel, $this>
25+
*/
26+
public function posts(): HasMany
27+
{
28+
return $this->hasMany(SomeModel::class);
29+
}
30+
31+
/**
32+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<SomeModel, $this, Pivot>
33+
*/
34+
public function comments(): BelongsToMany
35+
{
36+
return $this->belongsToMany(SomeModel::class);
37+
}
38+
39+
/**
40+
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<SomeModel, $this, Pivot>
41+
*/
42+
public function photos(): MorphToMany
43+
{
44+
return $this->morphToMany(SomeModel::class);
45+
}
46+
}
47+
?>

0 commit comments

Comments
 (0)