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 @@ -57,6 +57,7 @@
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
Expand Down Expand Up @@ -138,6 +139,7 @@
ExpressionCreateMockToCreateStubRector::class,
PropertyCreateMockToCreateStubRector::class,
MockObjectVarToStubRector::class,
BareVarToStubIntersectionRector::class,
InlineStubPropertyToCreateStubMethodCallRector::class,

// @test first, enable later
Expand Down
2 changes: 2 additions & 0 deletions config/sets/phpunit-mock-to-stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -17,5 +18,6 @@
ExpressionCreateMockToCreateStubRector::class,
PropertyCreateMockToCreateStubRector::class,
MockObjectVarToStubRector::class,
BareVarToStubIntersectionRector::class,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class AssertCompareContext
use Behat\Behat\Context\Context;

final class AssertCompareContext implements Context
{
public function some($response)
{
Expand All @@ -17,7 +19,9 @@ final class AssertCompareContext

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class AssertCompareContext
use Behat\Behat\Context\Context;

final class AssertCompareContext implements Context
{
public function some($response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class AssertNullCompareContext
use Behat\Behat\Context\Context;

final class AssertNullCompareContext implements Context
{
public function some($response)
{
Expand All @@ -16,7 +18,9 @@ final class AssertNullCompareContext

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class AssertNullCompareContext
use Behat\Behat\Context\Context;

final class AssertNullCompareContext implements Context
{
public function some($response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class MethodExistsContext
use Behat\Behat\Context\Context;

final class MethodExistsContext implements Context
{
public function some($response)
{
Expand All @@ -16,7 +18,9 @@ final class MethodExistsContext

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class MethodExistsContext
use Behat\Behat\Context\Context;

final class MethodExistsContext implements Context
{
public function some($response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class SimpleContext
use Behat\Behat\Context\Context;

final class SimpleContext implements Context
{
public function some($response)
{
Expand All @@ -18,7 +20,9 @@ final class SimpleContext

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class SimpleContext
use Behat\Behat\Context\Context;

final class SimpleContext implements Context
{
public function some($response)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

final class SchemaValidationContext
{
public function some($response)
{
assert($response instanceof \DateTime, 'only remaining option');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;

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

final class BareVarToStubIntersectionRectorTest 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,31 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

use PHPUnit\Framework\TestCase;

final class BareVarTest extends TestCase
{
/**
* @var FormBuilderInterface
*/
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

use PHPUnit\Framework\TestCase;

final class BareVarTest extends TestCase
{
/**
* @var FormBuilderInterface&Stub
*/
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
}

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

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipAlreadyIntersectionTest extends TestCase
{
/**
* @var FormBuilderInterface&Stub
*/
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipNonStubNativeTypeTest extends TestCase
{
/**
* @var FormBuilderInterface
*/
private \PHPUnit\Framework\MockObject\MockObject $formBuilder;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

final class SkipNonTestClass
{
/**
* @var FormBuilderInterface
*/
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;

final class SkipStubShortNameTest extends TestCase
{
/**
* @var Stub
*/
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
}
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\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;

return RectorConfig::configure()
->withRules(rules: [BareVarToStubIntersectionRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\Enum\AssertMethod;
use Rector\PHPUnit\Enum\BehatClassName;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -195,15 +196,13 @@ public function refactor(Node $node): ?Class_
private function isBehatContext(Class_ $class): bool
{
$scope = ScopeFetcher::fetch($class);
if (! $scope->getClassReflection() instanceof ClassReflection) {
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$className = $scope->getClassReflection()
->getName();

// special case with static call
return str_ends_with($className, 'Context');
return $classReflection->is(BehatClassName::CONTEXT);
}

/**
Expand Down
Loading
Loading