Skip to content

Commit

Permalink
[TypeDeclaration] Allow defined in non-setUp() on TypedPropertyFromCr…
Browse files Browse the repository at this point in the history
…eateMockAssignRector (#6180)

* [TypeDeclaration] Skip defined in non-setUp() method on TypedPropertyFromCreateMockAssignRector

* nullable if setUp() method exists, but not assign

* allow non-setup

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Jul 23, 2024
1 parent 1a58300 commit eba6153
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

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

class SetupMethodExistsButNotAssign extends TestCase
{
public $someMock;

protected function setUp(): void
{
}

private function factory()
{
$this->someMock = $this->createMock(SomeMockedClass::class);
}
}

?>
-----
<?php

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

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

class SetupMethodExistsButNotAssign extends TestCase
{
public ?\PHPUnit\Framework\MockObject\MockObject $someMock = null;

protected function setUp(): void
{
}

private function factory()
{
$this->someMock = $this->createMock(SomeMockedClass::class);
}
}

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

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

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

class WithNonSetupMethod extends TestCase
{
public $someMock;

protected function factory(): void
{
$this->someMock = $this->createMock(SomeMockedClass::class);
}
}

?>
-----
<?php

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

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

class WithNonSetupMethod extends TestCase
{
public ?\PHPUnit\Framework\MockObject\MockObject $someMock = null;

protected function factory(): void
{
$this->someMock = $this->createMock(SomeMockedClass::class);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -39,7 +39,8 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer,
private readonly StaticTypeMapper $staticTypeMapper
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -101,8 +102,7 @@ public function refactor(Node $node): ?Node
continue;
}

$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
if (count($property->props) !== 1) {
continue;
}

Expand Down Expand Up @@ -134,6 +134,15 @@ public function refactor(Node $node): ?Node
continue;
}

$propertyName = (string) $this->getName($property);
if (! $this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
if (! $propertyType instanceof NullableType) {
continue;
}

$property->props[0]->default = $this->nodeFactory->createNull();
}

$property->type = $propertyType;
$hasChanged = true;
}
Expand Down

0 comments on commit eba6153

Please sign in to comment.