2222use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
2323use Rector \BetterPhpDocParser \ValueObject \Type \FullyQualifiedIdentifierTypeNode ;
2424use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
25- use Rector \Contract \Rector \ConfigurableRectorInterface ;
2625use Rector \NodeTypeResolver \TypeComparator \TypeComparator ;
2726use Rector \PhpParser \Node \BetterNodeFinder ;
2827use Rector \Rector \AbstractScopeAwareRector ;
2928use Rector \StaticTypeMapper \StaticTypeMapper ;
30- use Symplify \RuleDocGenerator \ValueObject \CodeSample \ConfiguredCodeSample ;
29+ use ReflectionClassConstant ;
30+ use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
3131use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
32- use Webmozart \Assert \Assert ;
3332
3433/**
3534 * @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorNewGenericsTest
3635 * @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorOldGenericsTest
3736 */
38- class AddGenericReturnTypeToRelationsRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
37+ class AddGenericReturnTypeToRelationsRector extends AbstractScopeAwareRector
3938{
4039 // Relation methods which are supported by this Rector.
4140 private const RELATION_METHODS = [
@@ -59,6 +58,7 @@ public function __construct(
5958 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
6059 private readonly BetterNodeFinder $ betterNodeFinder ,
6160 private readonly StaticTypeMapper $ staticTypeMapper ,
61+ private readonly string $ applicationClass = 'Illuminate\Foundation\Application ' ,
6262 ) {
6363 }
6464
@@ -67,7 +67,7 @@ public function getRuleDefinition(): RuleDefinition
6767 return new RuleDefinition (
6868 'Add generic return type to relations in child of Illuminate\Database\Eloquent\Model ' ,
6969 [
70- new ConfiguredCodeSample (
70+ new CodeSample (
7171 <<<'CODE_SAMPLE'
7272use App\Account;
7373use Illuminate\Database\Eloquent\Model;
@@ -96,9 +96,9 @@ public function accounts(): HasMany
9696 return $this->hasMany(Account::class);
9797 }
9898}
99- CODE_SAMPLE,
100- [ ' shouldUseNewGenerics ' => false ] ),
101- new ConfiguredCodeSample (
99+ CODE_SAMPLE
100+ ),
101+ new CodeSample (
102102 <<<'CODE_SAMPLE'
103103use App\Account;
104104use Illuminate\Database\Eloquent\Model;
@@ -127,8 +127,8 @@ public function accounts(): HasMany
127127 return $this->hasMany(Account::class);
128128 }
129129}
130- CODE_SAMPLE,
131- [ ' shouldUseNewGenerics ' => true ] ),
130+ CODE_SAMPLE
131+ ),
132132 ]
133133 );
134134 }
@@ -196,6 +196,9 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
196196 return null ;
197197 }
198198
199+ // Put here to make the check as late as possible
200+ $ this ->setShouldUseNewGenerics ();
201+
199202 $ classForChildGeneric = $ this ->getClassForChildGeneric ($ scope , $ relationMethodCall );
200203 $ classForIntermediateGeneric = $ this ->getClassForIntermediateGeneric ($ relationMethodCall );
201204
@@ -232,24 +235,6 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
232235 return $ node ;
233236 }
234237
235- /**
236- * {@inheritDoc}
237- */
238- public function configure (array $ configuration ): void
239- {
240- if ($ configuration === []) {
241- $ this ->shouldUseNewGenerics = false ;
242-
243- return ;
244- }
245-
246- Assert::count ($ configuration , 1 );
247- Assert::keyExists ($ configuration , 'shouldUseNewGenerics ' );
248- Assert::boolean ($ configuration ['shouldUseNewGenerics ' ]);
249-
250- $ this ->shouldUseNewGenerics = $ configuration ['shouldUseNewGenerics ' ];
251- }
252-
253238 private function getRelatedModelClassFromMethodCall (MethodCall $ methodCall ): ?string
254239 {
255240 $ argType = $ this ->getType ($ methodCall ->getArgs ()[0 ]->value );
@@ -489,4 +474,13 @@ private function getGenericTypes(string $relatedClass, ?string $childClass, ?str
489474
490475 return $ generics ;
491476 }
477+
478+ private function setShouldUseNewGenerics (): void
479+ {
480+ $ laravelVersion = new ReflectionClassConstant ($ this ->applicationClass , 'VERSION ' );
481+
482+ if (is_string ($ laravelVersion ->getValue ())) {
483+ $ this ->shouldUseNewGenerics = version_compare ($ laravelVersion ->getValue (), '11.15.0 ' , '>= ' );
484+ }
485+ }
492486}
0 commit comments