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
2 changes: 2 additions & 0 deletions config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
Expand Down Expand Up @@ -74,6 +75,7 @@
StringCastAssertStringContainsStringRector::class,
AddParamTypeFromDependsRector::class,
AddReturnTypeToDependedRector::class,
ScalarArgumentToExpectedParamTypeRector::class,

NarrowUnusedSetUpDefinedPropertyRector::class,

Expand Down
3 changes: 2 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
naming: true,
earlyReturn: true,
rectorPreset: true,
phpunitCodeQuality: true
phpunitCodeQuality: true,
symfonyCodeQuality: true,
)
->withConfiguredRule(StringClassNameToClassConstantRector::class, [
// keep unprefixed to protected from downgrade
Expand Down
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 SimpleMethodCall extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setPhoneNumber(123456);
}
}

?>
-----
<?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 SimpleMethodCall extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setPhoneNumber('123456');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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 SkipDocblockType extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setMagicType(123456);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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 SkipUnionType extends TestCase
{
public function test()
{
$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setUnionType(123456);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ScalarArgumentToExpectedParamTypeRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

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

final class SomeClassWithSetter
{
public function setPhoneNumber(string $phoneNumber)
{
}

/**
* @param string $passportId
*/
public function setMagicType($passportId)
{

}

public function setUnionType(int|string $unionValue)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ScalarArgumentToExpectedParamTypeRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\ScalarArgumentToExpectedParamTypeRectorTest
*/
final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Correct expected type in setter of tests, if param type is strictly defined',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function test()
{
$someClass = new SomeClass();
$someClass->setPhone(12345);
}
}

final class SomeClass
{
public function setPhone(string $phone)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function test()
{
$someClass = new SomeClass();
$someClass->setPhone('12345');
}
}

final class SomeClass
{
public function setPhone(string $phone)
{
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
}

/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if ($node->getArgs() === []) {
return null;
}

$hasChanged = false;

if (! $this->hasStringOrNumberArguments($node)) {
return null;
}

$callParameterTypes = $this->methodParametersAndReturnTypesResolver->resolveCallParameterTypes($node);

foreach ($node->getArgs() as $key => $arg) {
if (! $arg->value instanceof Scalar) {
continue;
}

$knownParameterType = $callParameterTypes[$key] ?? null;
if (! $knownParameterType instanceof Type) {
continue;
}

if ($knownParameterType instanceof StringType && $arg->value instanceof Int_) {
$arg->value = new String_((string) $arg->value->value);
$hasChanged = true;
}

if ($knownParameterType instanceof IntegerType && $arg->value instanceof String_) {
$arg->value = new Int_((int) $arg->value->value);
$hasChanged = true;
}
}

if (! $hasChanged) {
return null;
}

return $node;
}

private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
{
foreach ($call->getArgs() as $arg) {
if ($arg->value instanceof Int_) {
return true;
}

if ($arg->value instanceof String_) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\PHPUnit\CodeQuality\Reflection;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
Expand All @@ -13,10 +16,17 @@
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use Rector\Enum\ClassName;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\CodeQuality\ValueObject\ParamTypesAndReturnType;

final class MethodParametersAndReturnTypesResolver
final readonly class MethodParametersAndReturnTypesResolver
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver
) {
}

public function resolveFromReflection(
IntersectionType $intersectionType,
string $methodName,
Expand Down Expand Up @@ -51,10 +61,41 @@ public function resolveFromReflection(
return null;
}

/**
* @return null|Type[]
*/
public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
{
if (! $call->name instanceof Identifier) {
return null;
}

$methodName = $call->name->toString();

$callerType = $this->nodeTypeResolver->getType($call instanceof MethodCall ? $call->var : $call->class);
if (! $callerType instanceof ObjectType) {
return null;
}

$classReflection = $callerType->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if (! $classReflection->hasNativeMethod($methodName)) {
return null;
}

$scope = ScopeFetcher::fetch($call);
$extendedMethodReflection = $classReflection->getMethod($methodName, $scope);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$extendedMethodReflection = $classReflection->getMethod($methodName, $scope);
$extendedMethodReflection = $classReflection->getNativeMethod($methodName);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return $this->resolveParameterTypes($extendedMethodReflection, $classReflection);
}

/**
* @return Type[]
*/
private function resolveParameterTypes(
public function resolveParameterTypes(
ExtendedMethodReflection $extendedMethodReflection,
ClassReflection $currentClassReflection
): array {
Expand Down