Skip to content

Commit b2725b1

Browse files
committed
cover scalar argument in behat as well
1 parent 1fce753 commit b2725b1

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;
4+
5+
use Behat\Behat\Context\Context;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;
7+
8+
final class CoverBehatContext implements Context
9+
{
10+
public function test()
11+
{
12+
$someClassWithSetter = new SomeClassWithSetter();
13+
$someClassWithSetter->setItems('1', 2, 3, 4);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;
22+
23+
use Behat\Behat\Context\Context;
24+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;
25+
26+
final class CoverBehatContext implements Context
27+
{
28+
public function test()
29+
{
30+
$someClassWithSetter = new SomeClassWithSetter();
31+
$someClassWithSetter->setItems('1', 2, '3', 4);
32+
}
33+
}
34+
35+
?>

rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use PhpParser\Node\Scalar\Float_;
1313
use PhpParser\Node\Scalar\Int_;
1414
use PhpParser\Node\Scalar\String_;
15+
use PHPStan\Reflection\ClassReflection;
1516
use PHPStan\Type\IntegerType;
1617
use PHPStan\Type\StringType;
1718
use PHPStan\Type\Type;
1819
use PHPStan\Type\TypeCombinator;
1920
use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver;
21+
use Rector\PHPUnit\Enum\BehatClassName;
2022
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2123
use Rector\Rector\AbstractRector;
24+
use Rector\Reflection\ReflectionResolver;
2225
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2326
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2427

@@ -30,6 +33,7 @@ final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector
3033
public function __construct(
3134
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
3235
private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver,
36+
private readonly ReflectionResolver $reflectionResolver,
3337
) {
3438
}
3539

@@ -154,7 +158,7 @@ public function refactor(Node $node): ?Node
154158

155159
private function shouldSkipCall(StaticCall|MethodCall $call): bool
156160
{
157-
if (! $this->testsNodeAnalyzer->isInTestClass($call)) {
161+
if (! $this->isInTestClass($call)) {
158162
return true;
159163
}
160164

@@ -187,4 +191,16 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
187191

188192
return false;
189193
}
194+
195+
private function isInTestClass(StaticCall|MethodCall $call): bool
196+
{
197+
$callerClassReflection = $this->reflectionResolver->resolveClassReflection($call);
198+
if ($callerClassReflection instanceof ClassReflection) {
199+
if ($callerClassReflection->is(BehatClassName::CONTEXT)) {
200+
return true;
201+
}
202+
}
203+
204+
return $this->testsNodeAnalyzer->isInTestClass($call);
205+
}
190206
}

src/Enum/BehatClassName.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Enum;
6+
7+
final class BehatClassName
8+
{
9+
/**
10+
* @var string
11+
*/
12+
public const CONTEXT = 'Behat\Behat\Context\Context';
13+
}

src/Enum/PHPUnitClassName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ final class PHPUnitClassName
1111
*/
1212
public const TEST_CASE = 'PHPUnit\Framework\TestCase';
1313

14+
/**
15+
* @var string
16+
*/
17+
public const TEST_CASE_LEGACY = 'PHPUnit_Framework_TestCase';
18+
1419
/**
1520
* @var string
1621
*/

src/NodeAnalyzer/TestsNodeAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @var string[]
2323
*/
24-
private const TEST_CASE_OBJECT_CLASSES = ['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'];
24+
private const TEST_CASE_OBJECT_CLASSES = [PHPUnitClassName::TEST_CASE, PHPUnitClassName::TEST_CASE_LEGACY];
2525

2626
public function __construct(
2727
private NodeTypeResolver $nodeTypeResolver,
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)