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 @@ -14,6 +14,7 @@
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
Expand Down Expand Up @@ -132,5 +133,6 @@
GetMockBuilderGetMockToCreateMockRector::class,
EntityDocumentCreateMockToDirectNewRector::class,
ReplaceAtMethodWithDesiredMatcherRector::class,
BareCreateMockAssignToDirectUseRector::class,
]);
};
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\ClassMethod\BareCreateMockAssignToDirectUseRector;

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

final class BareCreateMockAssignToDirectUseRectorTest 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,51 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleUsesAsNoConnection extends TestCase
{
public function test()
{
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);

$this->useMock($someMock);
$this->useMockAgain($someMock);
}

private function useMock($someMock)
{
}

private function useMockAgain($someMock)
{
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleUsesAsNoConnection extends TestCase
{
public function test()
{
$this->useMock($this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
$this->useMockAgain($this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
}

private function useMock($someMock)
{
}

private function useMockAgain($someMock)
{
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipUsedAsMock extends TestCase
{
public function test()
{
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);

$someMock->expects($this->atLeastOnce())
->method('some');

$this->useMock($someMock);
}

private function useMock($someMock)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
public function test()
{
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);

$this->useMock($someMock);
}

private function useMock($someMock)
{
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
public function test()
{
$this->useMock($this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
}

private function useMock($someMock)
{
}
}

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

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source;

final class AnotherClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector;

return RectorConfig::configure()
->withRules([BareCreateMockAssignToDirectUseRector::class]);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -47,4 +47,4 @@ final class SkipCommentInline extends TestCase
['content123', 12], // a comment inline
];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

final class SkipNonPhpUnit
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand All @@ -24,7 +24,7 @@ final class ImageBinaryTest extends TestCase
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
123 changes: 123 additions & 0 deletions rules/CodeQuality/NodeAnalyser/AssignedMocksCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class AssignedMocksCollector
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private ValueResolver $valueResolver,
private NodeNameResolver $nodeNameResolver,
private DoctrineEntityDocumentAnalyser $doctrineEntityDocumentAnalyser,
) {
}

/**
* @return array<string, string>
*/
public function collect(ClassMethod|Foreach_ $stmtsAware): array
{
if ($stmtsAware->stmts === null) {
return [];
}

$mockedVariablesToTypes = [];

foreach ($stmtsAware->stmts as $stmt) {
// find assign mock
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;

$firstArg = $this->matchCreateMockArgAssignedToVariable($assign);
if (! $firstArg instanceof Arg) {
continue;
}

$mockedClass = $this->valueResolver->getValue($firstArg->value);
if (! is_string($mockedClass)) {
continue;
}

if (! $this->reflectionProvider->hasClass($mockedClass)) {
continue;
}

$mockClassReflection = $this->reflectionProvider->getClass($mockedClass);
// these cannot be instantiated
if ($mockClassReflection->isAbstract()) {
continue;
}

if ($mockClassReflection->isInterface()) {
continue;
}

$mockedVariableName = $this->nodeNameResolver->getName($assign->var);
$mockedVariablesToTypes[$mockedVariableName] = $mockedClass;
}

return $mockedVariablesToTypes;
}

/**
* @return array<string, string>
*/
public function collectEntityClasses(ClassMethod $classMethod): array
{
$variableNamesToClassNames = $this->collect($classMethod);

$variableNamesToEntityClasses = [];

foreach ($variableNamesToClassNames as $variableName => $className) {
if (! $this->doctrineEntityDocumentAnalyser->isEntityClass($className)) {
continue;
}

$variableNamesToEntityClasses[$variableName] = $className;
}

return $variableNamesToEntityClasses;
}

public function matchCreateMockArgAssignedToVariable(Assign $assign): ?Arg
{
if (! $assign->var instanceof Variable) {
return null;
}

if (! $assign->expr instanceof MethodCall) {
return null;
}

$methodCall = $assign->expr;
if (! $this->nodeNameResolver->isName($methodCall->name, 'createMock')) {
return null;
}

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

return $methodCall->getArgs()[0];
}
}
15 changes: 13 additions & 2 deletions rules/CodeQuality/NodeAnalyser/DoctrineEntityDocumentAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;

final readonly class DoctrineEntityDocumentAnalyser
{
Expand All @@ -14,8 +14,19 @@
*/
private const array ENTITY_DOCBLOCK_MARKERS = ['@Document', '@ORM\\Document', '@Entity', '@ORM\\Entity'];

public function isEntityClass(ClassReflection $classReflection): bool
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function isEntityClass(string $className): bool
{
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($className);

$resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc();
if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) {
return false;
Expand Down
Loading