Skip to content

Commit 0a48055

Browse files
authored
skip behats (#451)
1 parent 70b0f2d commit 0a48055

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture;
4+
5+
use Behat\Behat\Context\Context;
6+
7+
final class SkipBehatContext implements Context
8+
{
9+
public function letItBe(?\Doctrine\Common\Collections\Collection $collection)
10+
{
11+
if (! $collection instanceof \Doctrine\Common\Collections\Collection) {
12+
throw new \InvalidArgumentException();
13+
}
14+
}
15+
}

rules/TypedCollections/Rector/If_/RemoveIfInstanceofCollectionRector.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
use PhpParser\Node\Expr\Ternary;
1818
use PhpParser\Node\Stmt\If_;
1919
use PhpParser\NodeVisitorAbstract;
20+
use PHPStan\Reflection\ClassReflection;
21+
use Rector\Doctrine\Enum\TestClass;
2022
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
2123
use Rector\PhpParser\Node\Value\ValueResolver;
24+
use Rector\PHPStan\ScopeFetcher;
2225
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2326
use Rector\Rector\AbstractRector;
2427
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -48,8 +51,7 @@ public function getNodeTypes(): array
4851
*/
4952
public function refactor(Node $node)
5053
{
51-
// most likely on purpose in tests
52-
if ($this->testsNodeAnalyzer->isInTestClass($node)) {
54+
if ($this->shouldSkip($node)) {
5355
return null;
5456
}
5557

@@ -223,4 +225,22 @@ private function isIsObjectFuncCallOnCollection(Expr $expr): bool
223225

224226
return $this->collectionTypeDetector->isCollectionType($firstArg->value);
225227
}
228+
229+
private function shouldSkip(If_|Node|Coalesce|Ternary|BooleanNot|BooleanAnd $node): bool
230+
{
231+
// most likely on purpose in tests
232+
if ($this->testsNodeAnalyzer->isInTestClass($node)) {
233+
return true;
234+
}
235+
236+
$classScope = ScopeFetcher::fetch($node);
237+
$classReflection = $classScope->getClassReflection();
238+
239+
if (! $classReflection instanceof ClassReflection) {
240+
return false;
241+
}
242+
243+
// usually assert on purpose
244+
return $classReflection->is(TestClass::BEHAT_CONTEXT);
245+
}
226246
}

stubs/Behat/Behat/Context/Context.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Behat\Behat\Context;
4+
5+
interface Context
6+
{
7+
}

0 commit comments

Comments
 (0)