From 52f2c0153ec6b4952e89b48f2ddc9b9fd0af36ee Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 23 Jul 2024 15:42:26 +0200 Subject: [PATCH] add to type declaration set --- .../rector/src/Rector/RenameSimpleRector.php | 2 +- .../RenameSimpleRectorTest.php | 2 +- composer.json | 2 +- .../Fixture/binary_or.php | 16 ++++++++ .../Fixture/some_fixture.php.inc | 8 ++-- .../Fixture/with_finalized_type.php.inc | 12 +++--- ...PropertyFromCreateMockAssignRectorTest.php | 3 +- .../config/rule_config.php | 3 +- ...ypedPropertyFromCreateMockAssignRector.php | 40 +++++++++++++++++-- src/Config/Level/TypeDeclarationLevel.php | 3 ++ .../utils/rector/src/Rector/__Name__.php | 2 +- .../__Name__/Fixture/some_class.php.inc | 4 +- .../tests/Rector/__Name__/__Name__Test.php | 2 +- .../tests/Rector/__Name__/__Name__Test.php | 2 +- 14 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/BooleanAnd/BinaryOpNullableToInstanceofRector/Fixture/binary_or.php diff --git a/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/src/Rector/RenameSimpleRector.php b/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/src/Rector/RenameSimpleRector.php index 550fcc1e50d..298e3ba9956 100644 --- a/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/src/Rector/RenameSimpleRector.php +++ b/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/src/Rector/RenameSimpleRector.php @@ -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 { diff --git a/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/tests/Rector/RenameSimpleRector/RenameSimpleRectorTest.php b/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/tests/Rector/RenameSimpleRector/RenameSimpleRectorTest.php index 0c91d6fa217..67e7f1e9e34 100644 --- a/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/tests/Rector/RenameSimpleRector/RenameSimpleRectorTest.php +++ b/build/target-repository/e2e/rector-prefixed-rule-test/utils/rector/tests/Rector/RenameSimpleRector/RenameSimpleRectorTest.php @@ -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; diff --git a/composer.json b/composer.json index 4112cbdc32e..7c8c5ff8e91 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/rules-tests/TypeDeclaration/Rector/BooleanAnd/BinaryOpNullableToInstanceofRector/Fixture/binary_or.php b/rules-tests/TypeDeclaration/Rector/BooleanAnd/BinaryOpNullableToInstanceofRector/Fixture/binary_or.php new file mode 100644 index 00000000000..d5bf894fae8 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/BooleanAnd/BinaryOpNullableToInstanceofRector/Fixture/binary_or.php @@ -0,0 +1,16 @@ +someMethod()) { + return 'yes'; + } + + return 'no'; +} + +?> diff --git a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/some_fixture.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/some_fixture.php.inc index 31b5ea97d2c..a3476eb9a85 100644 --- a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/some_fixture.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/some_fixture.php.inc @@ -1,9 +1,9 @@ doTestFile($filePath); } - public static function provideData(): \Iterator + public static function provideData(): Iterator { return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); } diff --git a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/config/rule_config.php b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/config/rule_config.php index 66d03950511..4c7a977d8e9 100644 --- a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/config/rule_config.php +++ b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/config/rule_config.php @@ -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); diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php index 85c53247dc3..ba3eb9ed9bb 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php @@ -1,5 +1,7 @@ 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 @@ -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; @@ -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; diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index 04182730069..7193af0bc3b 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -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; @@ -68,6 +69,8 @@ final class TypeDeclarationLevel AddTestsVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromMockObjectRector::class, + TypedPropertyFromCreateMockAssignRector::class, + AddArrowFunctionReturnTypeRector::class, BoolReturnTypeFromBooleanConstReturnsRector::class, ReturnTypeFromStrictNewArrayRector::class, diff --git a/templates/custom-rule/utils/rector/src/Rector/__Name__.php b/templates/custom-rule/utils/rector/src/Rector/__Name__.php index f3b9e64e45e..d4bf30ee82b 100644 --- a/templates/custom-rule/utils/rector/src/Rector/__Name__.php +++ b/templates/custom-rule/utils/rector/src/Rector/__Name__.php @@ -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 { diff --git a/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc b/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc index 0a911527b42..6cf9dc365f0 100644 --- a/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc +++ b/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc @@ -1,6 +1,6 @@