Skip to content

Commit c607ef0

Browse files
authored
[TypeDeclaration] Skip trait methods on ParamTypeByMethodCallType rules (#8189)
1 parent ef3c5cc commit c607ef0

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayParamTypeByMethodCallTypeRector\Fixture;
4+
5+
trait SkipTrait
6+
{
7+
public function go($value)
8+
{
9+
$this->use($value);
10+
}
11+
12+
private function use(array $value): void
13+
{
14+
}
15+
}

rules/TypeDeclaration/Rector/ClassMethod/AbstractParamTypeByMethodCallTypeRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Param;
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Function_;
16+
use PHPStan\Analyser\Scope;
1617
use PHPStan\Type\Type;
1718
use Rector\NodeTypeResolver\Node\AttributeKey;
1819
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
@@ -99,6 +100,12 @@ abstract protected function isMatchingParamType(Type $type): bool;
99100

100101
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
101102
{
103+
// trait method can be used in a class that requires a contract type, adding a param type would break it
104+
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
105+
if ($scope instanceof Scope && $scope->isInTrait()) {
106+
return true;
107+
}
108+
102109
// user-guarded class: adding a param type here would break its child classes
103110
if ($this->parentClassMethodTypeOverrideGuard->isTypeGuardedClass($classMethod)) {
104111
return true;

0 commit comments

Comments
 (0)