Skip to content

Commit

Permalink
add to type declaration set
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 23, 2024
1 parent def5199 commit 52f2c01
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Utils\Rector\Tests\Rector\RenameSimpleRectorTest
* @see \Rector\Tests\TypeDeclaration\Rector\RenameSimpleRectorTest
*/
final class RenameSimpleRector extends AbstractRector
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Utils\Rector\Tests\Rector\RenameSimpleRector;
namespace Rector\Tests\TypeDeclaration\Rector\RenameSimpleRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"symplify/rule-doc-generator": "^12.2.2",
"symplify/vendor-patches": "^11.3",
"tomasvotruba/class-leak": "^0.2.15",
"tomasvotruba/unused-public": "^0.3.9",
"tomasvotruba/unused-public": "^0.3.10",
"tracy/tracy": "^2.9"
},
"replace": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Source\SomeInstance;

function binaryOr(?SomeInstance $someClass)
{
if ($someClass || $someClass->someMethod()) {
return 'yes';
}

return 'no';
}

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

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

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

class SomeTest extends TestCase
{
Expand All @@ -19,10 +19,10 @@ class SomeTest extends TestCase
-----
<?php

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

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

class SomeTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

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

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

class WithFinalizedType extends TestCase
{
Expand All @@ -20,11 +20,11 @@ class WithFinalizedType extends TestCase
-----
<?php

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

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

class WithFinalizedType extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

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

Expand All @@ -15,7 +16,7 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

public static function provideData(): \Iterator
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;

return RectorConfig::configure()
->withRules([TypedPropertyFromCreateMockAssignRector::class]);
->withRules([TypedPropertyFromCreateMockAssignRector::class])
->withPhpVersion(\Rector\ValueObject\PhpVersionFeature::TYPED_PROPERTIES);
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
Expand All @@ -16,6 +18,7 @@
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -34,13 +37,43 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';

public function __construct(
private ClassMethodPropertyFetchManipulator $classMethodPropertyFetchManipulator
private readonly ClassMethodPropertyFetchManipulator $classMethodPropertyFetchManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add typed property from assigned mock', []);
return new RuleDefinition('Add typed property from assigned mock', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private $someProperty;
protected function setUp(): void
{
$this->someProperty = $this->createMock(SomeMockedClass::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someProperty;
protected function setUp(): void
{
$this->someProperty = $this->createMock(SomeMockedClass::class);
}
}
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
Expand All @@ -51,7 +84,7 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactor(Node $node)
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node, new ObjectType(self::TEST_CASE_CLASS))) {
return null;
Expand All @@ -66,7 +99,6 @@ public function refactor(Node $node)

$propertyName = $this->getName($property);

// is assigned mock in setUp()? not nullable
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
Expand Down Expand Up @@ -68,6 +69,8 @@ final class TypeDeclarationLevel
AddTestsVoidReturnTypeWhereNoReturnRector::class,

ReturnTypeFromMockObjectRector::class,
TypedPropertyFromCreateMockAssignRector::class,

AddArrowFunctionReturnTypeRector::class,
BoolReturnTypeFromBooleanConstReturnsRector::class,
ReturnTypeFromStrictNewArrayRector::class,
Expand Down
2 changes: 1 addition & 1 deletion templates/custom-rule/utils/rector/src/Rector/__Name__.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Utils\Rector\Tests\Rector\__Name__\__Name__Test
* @see \Rector\Tests\TypeDeclaration\Rector\__Name__\__Name__Test
*/
final class __Name__ extends AbstractRector
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Utils\Rector\Tests\Rector\__Name__\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\__Name__\Fixture;

// @todo fill code before

?>
-----
<?php

namespace Utils\Rector\Tests\Rector\__Name__\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\__Name__\Fixture;

// @todo fill code after

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Utils\Rector\Tests\Rector\__Name__;
namespace Rector\Tests\TypeDeclaration\Rector\__Name__;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Utils\Rector\Tests\Rector\__Name__;
namespace Rector\Tests\TypeDeclaration\Rector\__Name__;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

Expand Down

0 comments on commit 52f2c01

Please sign in to comment.