File tree Expand file tree Collapse file tree
utils-tests/Rector/MakeUseOfContainsStmtsRector Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2121 name : ' Run with Space in Directory'
2222 run : bin/rector process tests-paths/path/with\ space/SomeFile.php --clear-cache
2323
24- # -
25- # name: 'Preload php-parser Order'
26- # run: php preload.php
24+ -
25+ name : ' Preload php-parser Order'
26+ run : php preload.php
2727
2828 -
2929 name : ' PHPStan'
Original file line number Diff line number Diff line change 8181 " rules-tests" ,
8282 " tests"
8383 ],
84+ "Rector\\ Utils\\ Tests\\ " : " utils-tests" ,
8485 "E2e\\ Parallel\\ Reflection\\ Resolver\\ " : [
8586 " e2e/parallel-reflection-resolver/src/" ,
8687 " e2e/no-parallel-reflection-resolver/src"
Original file line number Diff line number Diff line change @@ -360,3 +360,4 @@ parameters:
360360 -
361361 message : ' #Method Rector\\Utils\\Rector\\MakeUseOfContaintsStmtsRector\:\:refactor\(\) should return 4\|PhpParser\\Node\\Expr\\BinaryOp\\Identical\|PhpParser\\Node\\Expr\\MethodCall\|null but returns int\|null#'
362362 path : utils/Rector/MakeUseOfContaintsStmtsRector.php
363+ - message : ' #Method Rector\\Utils\\Rector\\MakeUseOfContainsStmtsRector\:\:refactor\(\) should return 4\|PhpParser\\Node\\Expr\\BinaryOp\\Identical\|PhpParser\\Node\\Expr\\MethodCall\|null but returns int\|null#'
Original file line number Diff line number Diff line change 66use Rector \Config \RectorConfig ;
77use Rector \DeadCode \Rector \ConstFetch \RemovePhpVersionIdCheckRector ;
88use Rector \Php55 \Rector \String_ \StringClassNameToClassConstantRector ;
9- use Rector \Utils \Rector \MakeUseOfContaintsStmtsRector ;
9+ use Rector \Utils \Rector \MakeUseOfContainsStmtsRector ;
1010
1111return RectorConfig::configure ()
1212 ->withPreparedSets (
3636 __DIR__ . '/config ' ,
3737 __DIR__ . '/build/build-preload.php ' ,
3838 ])
39- ->withRules ([MakeUseOfContaintsStmtsRector ::class])
39+ ->withRules ([MakeUseOfContainsStmtsRector ::class])
4040 ->withRootFiles ()
4141 ->withImportNames (removeUnusedImports: true )
4242 ->withSkip ([
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Utils \Tests \Rector \MakeUseOfContainsStmtsRector \Fixture ;
4+
5+ use PhpParser \Node \ContainsStmts ;
6+
7+ final class ContaintsStmtsContract
8+ {
9+ public function process (ContainsStmts $ containsStmts ): bool
10+ {
11+ if ($ containsStmts ->stmts === null ) {
12+ return false ;
13+ }
14+
15+ return true ;
16+ }
17+ }
18+
19+ ?>
20+ -----
21+ <?php
22+
23+ namespace Rector \Utils \Tests \Rector \MakeUseOfContainsStmtsRector \Fixture ;
24+
25+ use PhpParser \Node \ContainsStmts ;
26+
27+ final class ContaintsStmtsContract
28+ {
29+ public function process (ContainsStmts $ containsStmts ): bool
30+ {
31+ if ($ containsStmts ->getStmts () === []) {
32+ return false ;
33+ }
34+
35+ return true ;
36+ }
37+ }
38+
39+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Utils \Tests \Rector \MakeUseOfContainsStmtsRector \Fixture ;
4+
5+ use PhpParser \Node \Stmt \ClassMethod ;
6+
7+ final class SkipChildOfContract
8+ {
9+ public function process (ClassMethod $ classMethod ): void
10+ {
11+ $ stmts = $ classMethod ->stmts ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Utils \Tests \Rector \MakeUseOfContainsStmtsRector \Fixture ;
4+
5+ use PhpParser \Node \Stmt \Class_ ;
6+
7+ final class SkipClass
8+ {
9+ public function process (Class_ $ class ): void
10+ {
11+ $ stmts = $ class ->stmts ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Utils \Tests \Rector \MakeUseOfContainsStmtsRector ;
6+
7+ use PHPUnit \Framework \Attributes \DataProvider ;
8+ use Rector \Testing \PHPUnit \AbstractRectorTestCase ;
9+
10+ final class MakeUseOfContainsStmtsRectorTest extends AbstractRectorTestCase
11+ {
12+ #[DataProvider('provideData ' )]
13+ public function test (string $ filePath ): void
14+ {
15+ $ this ->doTestFile ($ filePath );
16+ }
17+
18+ public static function provideData (): \Iterator
19+ {
20+ return self ::yieldFilesFromDirectory (__DIR__ . '/Fixture ' );
21+ }
22+
23+ public function provideConfigFilePath (): string
24+ {
25+ return __DIR__ . '/config/configured_rule.php ' ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use Rector \Config \RectorConfig ;
6+ use Rector \Utils \Rector \MakeUseOfContainsStmtsRector ;
7+
8+ return RectorConfig::configure ()
9+ ->withRules ([
10+ MakeUseOfContainsStmtsRector::class,
11+ ]);
Original file line number Diff line number Diff line change 1818use PhpParser \Node \Stmt \Foreach_ ;
1919use PhpParser \Node \Stmt \Function_ ;
2020use PhpParser \NodeVisitor ;
21+ use PHPStan \Reflection \ClassReflection ;
2122use PHPStan \Type \ObjectType ;
2223use Rector \PhpParser \Node \Value \ValueResolver ;
2324use Rector \Rector \AbstractRector ;
2425use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2526
26- final class MakeUseOfContaintsStmtsRector extends AbstractRector
27+ /**
28+ * @see \Rector\Utils\Tests\Rector\MakeUseOfContainsStmtsRector\MakeUseOfContainsStmtsRectorTest
29+ */
30+ final class MakeUseOfContainsStmtsRector extends AbstractRector
2731{
2832 public function __construct (
2933 private readonly ValueResolver $ valueResolver
@@ -87,6 +91,16 @@ public function refactor(Node $node): MethodCall|Identical|null|int
8791 return null ;
8892 }
8993
94+ $ callerType = $ this ->getType ($ node ->var );
95+ if ($ callerType instanceof ObjectType) {
96+ /** @var ClassReflection $callerClassReflection */
97+ $ callerClassReflection = $ callerType ->getClassReflection ();
98+ if (! $ callerClassReflection ->isInterface ()) {
99+ // we have to match interface exactly
100+ return null ;
101+ }
102+ }
103+
90104 return new MethodCall ($ node ->var , 'getStmts ' );
91105 }
92106
You can’t perform that action at this time.
0 commit comments