File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed
rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector
rules/Unambiguous/Rector/Expression Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \Unambiguous \Rector \Expression \FluentSettersToStandaloneCallMethodRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use Rector \Tests \Unambiguous \Rector \Expression \FluentSettersToStandaloneCallMethodRector \Source \SomeSetterClass ;
7+
8+ final class SkipMocks extends TestCase
9+ {
10+ public function test ()
11+ {
12+ $ this ->createMock (SomeSetterClass::class)
13+ ->expects ($ this ->once ())
14+ ->method ('some ' );
15+ }
16+ }
Original file line number Diff line number Diff line change 22
33namespace Rector \Tests \Unambiguous \Rector \Expression \FluentSettersToStandaloneCallMethodRector \Source ;
44
5- final class SomeSetterClass
5+ class SomeSetterClass
66{
77 private ?string $ name = null ;
88
Original file line number Diff line number Diff line change 44
55namespace Rector \Unambiguous \Rector \Expression ;
66
7+ use PHPStan \Type \ObjectType ;
78use PhpParser \Node ;
89use PhpParser \Node \Expr ;
910use PhpParser \Node \Expr \Assign ;
1314use PhpParser \Node \Name ;
1415use PhpParser \Node \Stmt \Expression ;
1516use PhpParser \Node \Stmt \Return_ ;
17+ use PHPStan \Reflection \ClassReflection ;
1618use Rector \Naming \Naming \PropertyNaming ;
1719use Rector \NodeTypeResolver \Node \AttributeKey ;
1820use Rector \Rector \AbstractRector ;
@@ -107,6 +109,10 @@ public function refactor(Node $node): ?array
107109
108110 $ rootExpr = $ currentMethodCall ;
109111
112+ if ($ this ->shouldSkipForVendorOrInternal ($ firstMethodCall )) {
113+ return null ;
114+ }
115+
110116 $ variableName = $ this ->resolveVariableName ($ rootExpr );
111117 $ someVariable = new Variable ($ variableName );
112118
@@ -137,4 +143,22 @@ private function resolveVariableName(Expr $expr): string
137143
138144 return 'someVariable ' ;
139145 }
146+
147+ private function shouldSkipForVendorOrInternal (MethodCall $ firstMethodCall ): bool
148+ {
149+ $ callerType = $ this ->getType ($ firstMethodCall );
150+ if ($ callerType instanceof ObjectType) {
151+ $ classReflection = $ callerType ->getClassReflection ();
152+ if (! $ classReflection instanceof ClassReflection) {
153+ return false ;
154+ }
155+
156+ $ fileName = $ classReflection ->getFileName ();
157+ if ($ fileName === null || str_contains ($ fileName , 'vendor ' )) {
158+ return true ;
159+ }
160+ }
161+
162+ return false ;
163+ }
140164}
You can’t perform that action at this time.
0 commit comments