Skip to content

Commit 49b31a0

Browse files
committed
add tests
1 parent 9086398 commit 49b31a0

10 files changed

Lines changed: 125 additions & 6 deletions

File tree

.github/workflows/code_analysis.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ jobs:
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'

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
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"

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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#'

rector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Rector\Config\RectorConfig;
77
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
88
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
9-
use Rector\Utils\Rector\MakeUseOfContaintsStmtsRector;
9+
use Rector\Utils\Rector\MakeUseOfContainsStmtsRector;
1010

1111
return RectorConfig::configure()
1212
->withPreparedSets(
@@ -36,7 +36,7 @@
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([
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
]);

utils/Rector/MakeUseOfContaintsStmtsRector.php renamed to utils/Rector/MakeUseOfContainsStmtsRector.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
use PhpParser\Node\Stmt\Foreach_;
1919
use PhpParser\Node\Stmt\Function_;
2020
use PhpParser\NodeVisitor;
21+
use PHPStan\Reflection\ClassReflection;
2122
use PHPStan\Type\ObjectType;
2223
use Rector\PhpParser\Node\Value\ValueResolver;
2324
use Rector\Rector\AbstractRector;
2425
use 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

0 commit comments

Comments
 (0)