Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Handle createMock as well in PreferPHPUnitThisCallRector #407

Merged
merged 2 commits into from
Nov 19, 2024
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=8.2"
},
"require-dev": {
"rector/rector-src": "dev-main#e3ba39d",
"rector/rector-src": "dev-main",
"phpunit/phpunit": "^10.5",
"phpstan/phpstan": "^1.12",
"symplify/phpstan-rules": "^13.0",
Expand All @@ -17,7 +17,7 @@
"phpstan/phpstan-webmozart-assert": "^1.2",
"symplify/vendor-patches": "^11.3",
"tracy/tracy": "^2.10",
"tomasvotruba/class-leak": "^1.1.2",
"tomasvotruba/class-leak": "^1.2",
"rector/type-perfect": "^1.0",
"rector/swiss-knife": "^1.0"
},
Expand Down
11 changes: 4 additions & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;

return RectorConfig::configure()
->withImportNames(removeUnusedImports: true)
Expand All @@ -30,7 +29,8 @@
__DIR__ . '/src/NodeFinder/DataProviderClassMethodFinder.php',
],
])
->withPhpSets(php82: true)
->withPhpSets()
->withAttributesSets(all: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
Expand All @@ -39,12 +39,9 @@
naming: true,
typeDeclarations: true,
privatization: true,
rectorPreset: true
rectorPreset: true,
phpunitCodeQuality: true
)
->withSets([
PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
])
->withConfiguredRule(StringClassNameToClassConstantRector::class, [
// keep unprefixed to protected from downgrade
'PHPUnit\Framework\*',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtLeastOnce extends TestCase
{
public function testMe()
{
$matcher = self::once();
$matcher = self::atLeast(5);
$matcher = self::never();
$matcher = self::atLeastOnce();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtLeastOnce extends TestCase
{
public function testMe()
{
$matcher = $this->once();
$matcher = $this->atLeast(5);
$matcher = $this->never();
$matcher = $this->atLeastOnce();
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateMock extends TestCase
{
public function testMe()
{
$someMock = self::createMock('stdClass');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateMock extends TestCase
{
public function testMe()
{
$someMock = $this->createMock('stdClass');
}
}

?>
20 changes: 13 additions & 7 deletions rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
final class PreferPHPUnitThisCallRector extends AbstractRector
{
/**
* @var string[]
*/
private const NON_ASSERT_STATIC_METHODS = ['createMock', 'atLeast', 'atLeastOnce', 'once', 'never'];

public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
Expand Down Expand Up @@ -75,30 +80,31 @@ public function refactor(Node $node): ?Node
}

$hasChanged = false;

$this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|MethodCall {
$isStatic = ($node instanceof ClassMethod && $node->isStatic()) || ($node instanceof Closure && $node->static);
if ($isStatic) {
$isInsideStaticFunctionLike = ($node instanceof ClassMethod && $node->isStatic()) || ($node instanceof Closure && $node->static);
if ($isInsideStaticFunctionLike) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $node instanceof StaticCall) {
return null;
}

$methodName = $this->getName($node->name);
if (! is_string($methodName)) {
if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->isNames($node->class, ['static', 'self'])) {
$methodName = $this->getName($node->name);
if (! is_string($methodName)) {
return null;
}

if (! str_starts_with($methodName, 'assert')) {
if (! $this->isNames($node->class, ['static', 'self'])) {
return null;
}

if ($node->isFirstClassCallable()) {
if (! str_starts_with($methodName, 'assert') && ! in_array($methodName, self::NON_ASSERT_STATIC_METHODS)) {
return null;
}

Expand Down
Loading