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,17 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass;

final class SkipIntersectionType extends TestCase
{
public SomeMockedClass&MockObject $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(SomeMockedClass::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node
$dateExpr = $this->getArgValue($node, 0);
$baseTimestamp = $this->getArgValue($node, 1);

if ($dateExpr instanceof Expr && !$baseTimestamp instanceof Expr) {
if ($dateExpr instanceof Expr && ! $baseTimestamp instanceof Expr) {
return $this->createCarbonParseTimestamp($dateExpr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Enum\ClassName;
Expand Down Expand Up @@ -34,9 +36,11 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add typed property from assigned mock', [
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'Add "PHPUnit\Framework\MockObject\MockObject" typed property from assigned mock to clearly separate from real objects',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
Expand All @@ -49,22 +53,25 @@ protected function setUp(): void
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someProperty;
private MockObject $someProperty;

protected function setUp(): void
{
$this->someProperty = $this->createMock(SomeMockedClass::class);
}
}
CODE_SAMPLE
),
]);
),

]
);
}

public function getNodeTypes(): array
Expand All @@ -84,15 +91,12 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

foreach ($node->getProperties() as $property) {
// already use PHPUnit\Framework\MockObject\MockObject type
if ($property->type instanceof Node && $this->isObjectType(
$property->type,
new ObjectType(ClassName::MOCK_OBJECT)
)) {
if (count($property->props) !== 1) {
continue;
}

if (count($property->props) !== 1) {
// already use PHPUnit\Framework\MockObject\MockObject type
if ($this->isAlreadyTypedWithMockObject($property)) {
continue;
}

Expand Down Expand Up @@ -135,4 +139,18 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

private function isAlreadyTypedWithMockObject(Property $property): bool
{
if (! $property->type instanceof Node) {
return false;
}

// complex type, used on purpose
if ($property->type instanceof IntersectionType) {
return true;
}

return $this->isObjectType($property->type, new ObjectType(ClassName::MOCK_OBJECT));
}
}