Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use Behat\Behat\Context\Context;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;

final class CoverBehatContext implements Context
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setItems('1', 2, 3, 4);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use Behat\Behat\Context\Context;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;

final class CoverBehatContext implements Context
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setItems('1', 2, '3', 4);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;

final class MultipleArgs extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setItems('1', 2, 3, 4);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\SomeClassWithSetter;

final class MultipleArgs extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setItems('1', 2, '3', 4);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function setUnionType(int|string $unionValue)
{

}

public function setItems(string $one, int $two, string $three, int $four)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver;
use Rector\PHPUnit\Enum\BehatClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,6 +33,7 @@ final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver,
private readonly ReflectionResolver $reflectionResolver,
) {
}

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

private function shouldSkipCall(StaticCall|MethodCall $call): bool
{
if (! $this->testsNodeAnalyzer->isInTestClass($call)) {
if (! $this->isInTestClass($call)) {
return true;
}

Expand Down Expand Up @@ -187,4 +191,18 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool

return false;
}

private function isInTestClass(StaticCall|MethodCall $call): bool
{
$callerClassReflection = $this->reflectionResolver->resolveClassReflection($call);
if (! $callerClassReflection instanceof ClassReflection) {
return $this->testsNodeAnalyzer->isInTestClass($call);
}

if ($callerClassReflection->is(BehatClassName::CONTEXT)) {
return true;
}

return $this->testsNodeAnalyzer->isInTestClass($call);
}
}
13 changes: 13 additions & 0 deletions src/Enum/BehatClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Enum;

final class BehatClassName
{
/**
* @var string
*/
public const CONTEXT = 'Behat\Behat\Context\Context';
}
5 changes: 5 additions & 0 deletions src/Enum/PHPUnitClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ final class PHPUnitClassName
*/
public const TEST_CASE = 'PHPUnit\Framework\TestCase';

/**
* @var string
*/
public const TEST_CASE_LEGACY = 'PHPUnit_Framework_TestCase';

/**
* @var string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/NodeAnalyzer/TestsNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @var string[]
*/
private const TEST_CASE_OBJECT_CLASSES = ['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'];
private const TEST_CASE_OBJECT_CLASSES = [PHPUnitClassName::TEST_CASE, PHPUnitClassName::TEST_CASE_LEGACY];

public function __construct(
private NodeTypeResolver $nodeTypeResolver,
Expand Down
7 changes: 7 additions & 0 deletions stubs/Behat/Behat/Context/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Behat\Behat\Context;

interface Context
{
}