From 499b272964a8b9dde2badaf872d053b3df17d162 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 9 Jul 2023 00:14:17 +0200 Subject: [PATCH 1/2] fixup! Make RemoveNonExistingVarAnnotationRector simpler, skip compact() check as not generic (#4439) --- .../Fixture/skip_used_later.php.inc | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rules-tests/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/skip_used_later.php.inc diff --git a/rules-tests/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/skip_used_later.php.inc b/rules-tests/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/skip_used_later.php.inc new file mode 100644 index 00000000000..8730c4fc620 --- /dev/null +++ b/rules-tests/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/skip_used_later.php.inc @@ -0,0 +1,31 @@ + +----- + From 057f9c96f65ba4f9cb146d05638595424a15d5e0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 9 Jul 2023 00:36:37 +0200 Subject: [PATCH 2/2] [PHP 8] Resources object upgrade - remove 2 rules as the upgrade require wholesome manual work --- config/set/php80.php | 37 ++- config/set/php81.php | 2 - .../Fixture/different_condition.php.inc | 29 --- .../Fixture/different_instanceof.php.inc | 29 --- .../different_instanceof_variable.php.inc | 29 --- .../Fixture/double_check.php.inc | 29 --- .../Fixture/double_check_flip.php.inc | 29 --- .../double_check_with_parenthesis.php.inc | 29 --- .../Fixture/skip_complex_condition.php.inc | 12 - .../Fixture/some_ msg_get_queue.php.inc | 29 --- .../Fixture/some_curl_init.php.inc | 29 --- .../Fixture/some_enhance_broker_init.php.inc | 29 --- .../Fixture/some_imagecreate.php.inc | 29 --- .../Fixture/some_inflate_init.php.inc | 29 --- .../Fixture/some_openssl.php.inc | 63 ----- .../Fixture/some_shmop_open.php.inc | 33 --- .../Fixture/some_socket.php.inc | 29 --- .../Fixture/some_xml_parser_create.php.inc | 29 --- .../Fixture/some_xml_parser_create_ns.php.inc | 29 --- .../some_xmlwriter_open_memory.php.inc | 33 --- .../Php8ResourceReturnToObjectRectorTest.php | 28 -- .../config/configured_rule.php | 10 - .../Fixture/some_finfo_open.php.inc | 29 --- .../Fixture/some_ftp_connect.php.inc | 29 --- .../Fixture/some_imap_open.php.inc | 29 --- .../Fixture/some_ldap_connect.php.inc | 37 --- .../Fixture/some_pg_connect.php.inc | 29 --- .../Fixture/some_pspell.php.inc | 29 --- .../Php81ResourceReturnToObjectRectorTest.php | 28 -- .../config/configured_rule.php | 10 - .../ResourceReturnToObject.php | 240 ------------------ .../Php8ResourceReturnToObjectRector.php | 150 ----------- .../Php81ResourceReturnToObjectRector.php | 115 --------- .../EarlyReturnResourceToObjectTest.php | 28 -- .../Fixture/double_check.php.inc | 41 --- .../Fixture/double_check_flip.php.inc | 41 --- .../double_check_with_parenthesis.php.inc | 46 ---- .../config/configured_rule.php | 12 - 38 files changed, 17 insertions(+), 1500 deletions(-) delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_condition.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof_variable.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_flip.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_with_parenthesis.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/skip_complex_condition.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_ msg_get_queue.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_curl_init.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_enhance_broker_init.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_imagecreate.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_inflate_init.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_openssl.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_shmop_open.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_socket.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create_ns.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xmlwriter_open_memory.php.inc delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Php8ResourceReturnToObjectRectorTest.php delete mode 100644 rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/config/configured_rule.php delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_finfo_open.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ftp_connect.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_imap_open.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ldap_connect.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pg_connect.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pspell.php.inc delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Php81ResourceReturnToObjectRectorTest.php delete mode 100644 rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/config/configured_rule.php delete mode 100644 rules/Php80/NodeManipulator/ResourceReturnToObject.php delete mode 100644 rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php delete mode 100644 rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php delete mode 100644 tests/Issues/EarlyReturnResourceToObject/EarlyReturnResourceToObjectTest.php delete mode 100644 tests/Issues/EarlyReturnResourceToObject/Fixture/double_check.php.inc delete mode 100644 tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_flip.php.inc delete mode 100644 tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_with_parenthesis.php.inc delete mode 100644 tests/Issues/EarlyReturnResourceToObject/config/configured_rule.php diff --git a/config/set/php80.php b/config/set/php80.php index a5ec1bb6032..a0774cc2ffc 100644 --- a/config/set/php80.php +++ b/config/set/php80.php @@ -17,7 +17,6 @@ use Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector; use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector; use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; -use Rector\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector; use Rector\Php80\Rector\FunctionLike\MixedTypeRector; use Rector\Php80\Rector\Identical\StrEndsWithRector; use Rector\Php80\Rector\Identical\StrStartsWithRector; @@ -29,35 +28,38 @@ use Rector\Transform\ValueObject\StaticCallToFuncCall; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rule(StrContainsRector::class); - $rectorConfig->rule(StrStartsWithRector::class); - $rectorConfig->rule(StrEndsWithRector::class); - - $rectorConfig - ->ruleWithConfiguration(StaticCallToFuncCallRector::class, [ - new StaticCallToFuncCall('Nette\Utils\Strings', 'startsWith', 'str_starts_with'), - new StaticCallToFuncCall('Nette\Utils\Strings', 'endsWith', 'str_ends_with'), - new StaticCallToFuncCall('Nette\Utils\Strings', 'contains', 'str_contains'), - ]); - $rectorConfig->rules([ + StrContainsRector::class, + StrStartsWithRector::class, + StrEndsWithRector::class, StringableForToStringRector::class, ClassOnObjectRector::class, GetDebugTypeRector::class, RemoveUnusedVariableInCatchRector::class, ClassPropertyAssignToConstructorPromotionRector::class, ChangeSwitchToMatchRector::class, + RemoveParentCallWithoutParentRector::class, + SetStateToStaticRector::class, + FinalPrivateToPrivateVisibilityRector::class, + AddParamBasedOnParentClassMethodRector::class, + MixedTypeRector::class, + ClassOnThisVariableObjectRector::class, ]); + $rectorConfig + ->ruleWithConfiguration(StaticCallToFuncCallRector::class, [ + new StaticCallToFuncCall('Nette\Utils\Strings', 'startsWith', 'str_starts_with'), + new StaticCallToFuncCall('Nette\Utils\Strings', 'endsWith', 'str_ends_with'), + new StaticCallToFuncCall('Nette\Utils\Strings', 'contains', 'str_contains'), + ]); + // nette\utils and Strings::replace() $rectorConfig ->ruleWithConfiguration( ArgumentAdderRector::class, [new ArgumentAdder('Nette\Utils\Strings', 'replace', 2, 'replacement', '')] ); - $rectorConfig->rule(RemoveParentCallWithoutParentRector::class); - $rectorConfig->rule(SetStateToStaticRector::class); - $rectorConfig->rule(FinalPrivateToPrivateVisibilityRector::class); + // @see https://php.watch/versions/8.0/pgsql-aliases-deprecated $rectorConfig ->ruleWithConfiguration(RenameFunctionRector::class, [ @@ -100,9 +102,4 @@ new ReplaceFuncCallArgumentDefaultValue('version_compare', 2, 'lte', 'le'), new ReplaceFuncCallArgumentDefaultValue('version_compare', 2, 'n', 'ne'), ]); - - $rectorConfig->rule(Php8ResourceReturnToObjectRector::class); - $rectorConfig->rule(AddParamBasedOnParentClassMethodRector::class); - $rectorConfig->rule(MixedTypeRector::class); - $rectorConfig->rule(ClassOnThisVariableObjectRector::class); }; diff --git a/config/set/php81.php b/config/set/php81.php index baca65db4e7..62a8bba4c7b 100644 --- a/config/set/php81.php +++ b/config/set/php81.php @@ -9,7 +9,6 @@ use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector; use Rector\Php81\Rector\ClassMethod\NewInInitializerRector; use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector; -use Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector; use Rector\Php81\Rector\FunctionLike\IntersectionTypesRector; use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector; use Rector\Php81\Rector\MethodCall\SpatieEnumMethodCallToEnumConstRector; @@ -25,7 +24,6 @@ ReadOnlyPropertyRector::class, SpatieEnumClassToEnumRector::class, SpatieEnumMethodCallToEnumConstRector::class, - Php81ResourceReturnToObjectRector::class, NewInInitializerRector::class, IntersectionTypesRector::class, NullToStrictStringFuncCallArgRector::class, diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_condition.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_condition.php.inc deleted file mode 100644 index 0a18c59609b..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_condition.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof.php.inc deleted file mode 100644 index b6129322321..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof_variable.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof_variable.php.inc deleted file mode 100644 index 7bef75c8a1f..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/different_instanceof_variable.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check.php.inc deleted file mode 100644 index adbd95b3dc0..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_flip.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_flip.php.inc deleted file mode 100644 index 8ac9adeaea0..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_flip.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_with_parenthesis.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_with_parenthesis.php.inc deleted file mode 100644 index 57fbcd2daac..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/double_check_with_parenthesis.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/skip_complex_condition.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/skip_complex_condition.php.inc deleted file mode 100644 index 73e65f64106..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/skip_complex_condition.php.inc +++ /dev/null @@ -1,12 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_curl_init.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_curl_init.php.inc deleted file mode 100644 index cd60f2bb51a..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_curl_init.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_enhance_broker_init.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_enhance_broker_init.php.inc deleted file mode 100644 index 7ce5b966ba2..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_enhance_broker_init.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_imagecreate.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_imagecreate.php.inc deleted file mode 100644 index ffc996195de..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_imagecreate.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_inflate_init.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_inflate_init.php.inc deleted file mode 100644 index 9ae78f6b77f..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_inflate_init.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_openssl.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_openssl.php.inc deleted file mode 100644 index ca849831622..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_openssl.php.inc +++ /dev/null @@ -1,63 +0,0 @@ - "GB", - "stateOrProvinceName" => "Somerset", - "localityName" => "Glastonbury", - "organizationName" => "The Brain Room Limited", - "organizationalUnitName" => "PHP Documentation Team", - "commonName" => "Wez Furlong", - "emailAddress" => "wez@example.com" - ]; - - // Generate a new private (and public) key pair - $privkey = openssl_pkey_new([ - "private_key_bits" => 2048, - "private_key_type" => OPENSSL_KEYTYPE_RSA, - ]); - - // Generate a certificate signing request - $csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256')); - is_resource($csr); - } -} - -?> ------ - "GB", - "stateOrProvinceName" => "Somerset", - "localityName" => "Glastonbury", - "organizationName" => "The Brain Room Limited", - "organizationalUnitName" => "PHP Documentation Team", - "commonName" => "Wez Furlong", - "emailAddress" => "wez@example.com" - ]; - - // Generate a new private (and public) key pair - $privkey = openssl_pkey_new([ - "private_key_bits" => 2048, - "private_key_type" => OPENSSL_KEYTYPE_RSA, - ]); - - // Generate a certificate signing request - $csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256')); - $csr instanceof \OpenSSLCertificateSigningRequest; - } -} - -?> diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_shmop_open.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_shmop_open.php.inc deleted file mode 100644 index 5fff37094c5..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_shmop_open.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_socket.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_socket.php.inc deleted file mode 100644 index 3bc23cc6e64..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_socket.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create.php.inc deleted file mode 100644 index 4d9400ba895..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create_ns.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create_ns.php.inc deleted file mode 100644 index 65f5e11ece2..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xml_parser_create_ns.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xmlwriter_open_memory.php.inc b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xmlwriter_open_memory.php.inc deleted file mode 100644 index d6f938e8e98..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Fixture/some_xmlwriter_open_memory.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Php8ResourceReturnToObjectRectorTest.php b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Php8ResourceReturnToObjectRectorTest.php deleted file mode 100644 index f81c8a68bbe..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/Php8ResourceReturnToObjectRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/config/configured_rule.php b/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/config/configured_rule.php deleted file mode 100644 index b4b0b60450d..00000000000 --- a/rules-tests/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(Php8ResourceReturnToObjectRector::class); -}; diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_finfo_open.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_finfo_open.php.inc deleted file mode 100644 index 86a0d19c914..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_finfo_open.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ftp_connect.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ftp_connect.php.inc deleted file mode 100644 index cb55c8c704e..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ftp_connect.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_imap_open.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_imap_open.php.inc deleted file mode 100644 index 8d455676676..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_imap_open.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ldap_connect.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ldap_connect.php.inc deleted file mode 100644 index c67ea54afb5..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_ldap_connect.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pg_connect.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pg_connect.php.inc deleted file mode 100644 index 96e2b2eeb97..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pg_connect.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pspell.php.inc b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pspell.php.inc deleted file mode 100644 index ae84307b477..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Fixture/some_pspell.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Php81ResourceReturnToObjectRectorTest.php b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Php81ResourceReturnToObjectRectorTest.php deleted file mode 100644 index cef6ea3b527..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/Php81ResourceReturnToObjectRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/config/configured_rule.php b/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/config/configured_rule.php deleted file mode 100644 index e9b46b2ac68..00000000000 --- a/rules-tests/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(Php81ResourceReturnToObjectRector::class); -}; diff --git a/rules/Php80/NodeManipulator/ResourceReturnToObject.php b/rules/Php80/NodeManipulator/ResourceReturnToObject.php deleted file mode 100644 index c00e8b4ff9d..00000000000 --- a/rules/Php80/NodeManipulator/ResourceReturnToObject.php +++ /dev/null @@ -1,240 +0,0 @@ - $collectionFunctionToReturnObject - */ - public function refactor( - FuncCall|BooleanOr $node, - array $collectionFunctionToReturnObject - ): FuncCall|BooleanOr|Instanceof_|null { - if ($node instanceof FuncCall) { - return $this->processFuncCall($node, $collectionFunctionToReturnObject); - } - - return $this->processBooleanOr($node, $collectionFunctionToReturnObject); - } - - /** - * @param array $collectionFunctionToReturnObject - */ - private function processFuncCall(FuncCall $funcCall, array $collectionFunctionToReturnObject): ?Instanceof_ - { - $parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof BinaryOp && ! $parentNode instanceof BooleanOr) { - return null; - } - - if ($this->shouldSkip($funcCall)) { - return null; - } - - $objectInstanceCheck = $this->resolveObjectInstanceCheck($funcCall, $collectionFunctionToReturnObject); - if ($objectInstanceCheck === null) { - return null; - } - - $firstArg = $funcCall->getArgs()[0]; - - $argResourceValue = $firstArg->value; - - return new Instanceof_($argResourceValue, new FullyQualified($objectInstanceCheck)); - } - - /** - * @param array $collectionFunctionToReturnObject - */ - private function resolveArgValueType(FuncCall $funcCall, array $collectionFunctionToReturnObject): ?Type - { - $argResourceValue = $funcCall->getArgs()[0] -->value; - $argValueType = $this->nodeTypeResolver->getType($argResourceValue); - - // if detected type is not FullyQualifiedObjectType, it still can be a resource to object, when: - // - in the right position of BooleanOr, it be NeverType - // - the object changed after init - if (! $argValueType instanceof FullyQualifiedObjectType) { - return $this->resolveArgValueTypeFromPreviousAssign( - $funcCall, - $argResourceValue, - $collectionFunctionToReturnObject - ); - } - - return $argValueType; - } - - /** - * @param array $collectionFunctionToReturnObject - */ - private function resolveObjectInstanceCheck(FuncCall $funcCall, array $collectionFunctionToReturnObject): ?string - { - $argValueType = $this->resolveArgValueType($funcCall, $collectionFunctionToReturnObject); - if (! $argValueType instanceof FullyQualifiedObjectType) { - return null; - } - - $className = $argValueType->getClassName(); - foreach ($collectionFunctionToReturnObject as $singleCollectionFunctionToReturnObject) { - if ($className === $singleCollectionFunctionToReturnObject) { - return $singleCollectionFunctionToReturnObject; - } - } - - return null; - } - - /** - * @param array $collectionFunctionToReturnObject - */ - private function resolveArgValueTypeFromPreviousAssign( - FuncCall $funcCall, - Expr $expr, - array $collectionFunctionToReturnObject - ): ?FullyQualifiedObjectType { - $objectInstanceCheck = null; - - $foundNode = $this->betterNodeFinder->findFirstPrevious($funcCall, function (Node $subNode) use ( - &$objectInstanceCheck, - $expr, - $collectionFunctionToReturnObject - ): bool { - if (! $this->isAssignWithFuncCallExpr($subNode)) { - return false; - } - - /** @var Assign $subNode */ - if (! $this->nodeComparator->areNodesEqual($subNode->var, $expr)) { - return false; - } - - foreach ($collectionFunctionToReturnObject as $key => $value) { - if ($this->nodeNameResolver->isName($subNode->expr, $key)) { - $objectInstanceCheck = $value; - return true; - } - } - - return false; - }); - - if (! $foundNode instanceof Assign) { - return null; - } - - /** @var string $objectInstanceCheck */ - return new FullyQualifiedObjectType($objectInstanceCheck); - } - - private function isAssignWithFuncCallExpr(Node $node): bool - { - if (! $node instanceof Assign) { - return false; - } - - return $node->expr instanceof FuncCall; - } - - /** - * @param array $collectionFunctionToReturnObject - */ - private function processBooleanOr(BooleanOr $booleanOr, array $collectionFunctionToReturnObject): ?Instanceof_ - { - $left = $booleanOr->left; - $right = $booleanOr->right; - - $funCall = null; - $instanceof = null; - - if ($left instanceof FuncCall && $right instanceof Instanceof_) { - $funCall = $left; - $instanceof = $right; - } elseif ($left instanceof Instanceof_ && $right instanceof FuncCall) { - $funCall = $right; - $instanceof = $left; - } else { - return null; - } - - /** @var FuncCall $funCall */ - if ($this->shouldSkip($funCall)) { - return null; - } - - $objectInstanceCheck = $this->resolveObjectInstanceCheck($funCall, $collectionFunctionToReturnObject); - if ($objectInstanceCheck === null) { - return null; - } - - $argResourceValue = $funCall->getArgs()[0] -->value; - - /** @var Instanceof_ $instanceof */ - if (! $this->isInstanceOfObjectCheck($instanceof, $argResourceValue, $objectInstanceCheck)) { - return null; - } - - return $instanceof; - } - - private function isInstanceOfObjectCheck(Instanceof_ $instanceof, Expr $expr, string $objectInstanceCheck): bool - { - if (! $instanceof->class instanceof FullyQualified) { - return false; - } - - if (! $this->nodeComparator->areNodesEqual($expr, $instanceof->expr)) { - return false; - } - - return $this->nodeNameResolver->isName($instanceof->class, $objectInstanceCheck); - } - - private function shouldSkip(FuncCall $funcCall): bool - { - if (! $this->nodeNameResolver->isName($funcCall, 'is_resource')) { - return true; - } - - if ($funcCall->isFirstClassCallable()) { - return true; - } - - if (! isset($funcCall->getArgs()[0])) { - return true; - } - - $argResource = $funcCall->args[0]; - return ! $argResource instanceof Arg; - } -} diff --git a/rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php b/rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php deleted file mode 100644 index 756e8878ee2..00000000000 --- a/rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php +++ /dev/null @@ -1,150 +0,0 @@ - - */ - private const COLLECTION_FUNCTION_TO_RETURN_OBJECT = [ - // curl - 'curl_init' => 'CurlHandle', - 'curl_multi_init' => 'CurlMultiHandle', - 'curl_share_init' => 'CurlShareHandle', - - // socket - 'socket_create' => 'Socket', - 'socket_accept' => 'Socket', - 'socket_addrinfo_bind' => 'Socket', - 'socket_addrinfo_connect' => 'Socket', - 'socket_create_listen' => 'Socket', - 'socket_import_stream' => 'Socket', - 'socket_wsaprotocol_info_import' => 'Socket', - - // GD - 'imagecreate' => 'GdImage', - 'imagecreatefromavif' => 'GdImage', - 'imagecreatefrombmp' => 'GdImage', - 'imagecreatefromgd2' => 'GdImage', - 'imagecreatefromgd2part' => 'GdImage', - 'imagecreatefromgd' => 'GdImage', - 'imagecreatefromgif' => 'GdImage', - 'imagecreatefromjpeg' => 'GdImage', - 'imagecreatefrompng' => 'GdImage', - 'imagecreatefromstring' => 'GdImage', - 'imagecreatefromtga' => 'GdImage', - 'imagecreatefromwbmp' => 'GdImage', - 'imagecreatefromwebp' => 'GdImage', - 'imagecreatefromxbm' => 'GdImage', - 'imagecreatefromxpm' => 'GdImage', - 'imagecreatetruecolor' => 'GdImage', - 'imagecrop' => 'GdImage', - 'imagecropauto' => 'GdImage', - - // XMLWriter - 'xmlwriter_open_memory' => 'XMLWriter', - - // XMLParser - 'xml_parser_create' => 'XMLParser', - 'xml_parser_create_ns' => 'XMLParser', - - // Broker - 'enchant_broker_init' => 'EnchantBroker', - 'enchant_broker_request_dict' => 'EnchantDictionary', - 'enchant_broker_request_pwl_dict' => 'EnchantDictionary', - - // OpenSSL - 'openssl_x509_read' => 'OpenSSLCertificate', - 'openssl_csr_sign' => 'OpenSSLCertificate', - 'openssl_csr_new' => 'OpenSSLCertificateSigningRequest', - 'openssl_pkey_new' => 'OpenSSLAsymmetricKey', - - // Shmop - 'shmop_open' => 'Shmop', - - // MessageQueue - 'msg_get_queue' => 'SysvMessageQueue', - 'sem_get' => 'SysvSemaphore', - 'shm_attach' => 'SysvSharedMemory', - - // Inflate Deflate - 'inflate_init' => 'InflateContext', - 'deflate_init' => 'DeflateContext', - ]; - - public function __construct( - private readonly ResourceReturnToObject $resourceReturnToObject - ) { - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Change is_resource() to instanceof Object', - [ - new CodeSample( - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - $ch = curl_init(); - is_resource($ch); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - $ch = curl_init(); - $ch instanceof \CurlHandle; - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [FuncCall::class, BooleanOr::class]; - } - - /** - * @param FuncCall|BooleanOr $node - */ - public function refactor(Node $node): ?Node - { - return $this->resourceReturnToObject->refactor($node, self::COLLECTION_FUNCTION_TO_RETURN_OBJECT); - } - - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::PHP8_RESOURCE_TO_OBJECT; - } -} diff --git a/rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php b/rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php deleted file mode 100644 index bdbefb16fe1..00000000000 --- a/rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php +++ /dev/null @@ -1,115 +0,0 @@ - - */ - private const COLLECTION_FUNCTION_TO_RETURN_OBJECT = [ - // finfo - 'finfo_open' => 'finfo', - - // ftp - 'ftp_connect' => 'FTP\Connection', - - // imap_open - 'imap_open' => 'IMAP\Connection', - - // pspell - 'pspell_config_create' => 'PSpell\Config', - 'pspell_new_config' => 'PSpell\Dictionary', - - // ldap - 'ldap_connect' => 'LDAP\Connection', - 'ldap_read' => 'LDAP\Result', - 'ldap_first_entry' => 'LDAP\ResultEntry', - 'ldap_first_reference' => 'LDAP\ResultEntry', - 'ldap_next_entry' => 'LDAP\ResultEntry', - 'ldap_next_reference' => 'LDAP\ResultEntry', - - // psql - 'pg_pconnect' => 'PgSql\Connection', - 'pg_connect' => 'PgSql\Connection', - 'pg_query' => 'PgSql\Result', - 'pg_prepare' => 'PgSql\Result', - 'pg_execute' => 'PgSql\Result', - 'pg_lo_open' => 'PgSql\Lob', - ]; - - public function __construct( - private readonly ResourceReturnToObject $resourceReturnToObject - ) { - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Change is_resource() to instanceof Object', - [ - new CodeSample( - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - $f = finfo_open(); - is_resource($f); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - $f = finfo_open(); - $f instanceof \finfo; - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [FuncCall::class, BooleanOr::class]; - } - - /** - * @param FuncCall|BooleanOr $node - */ - public function refactor(Node $node): ?Node - { - return $this->resourceReturnToObject->refactor($node, self::COLLECTION_FUNCTION_TO_RETURN_OBJECT); - } - - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::PHP81_RESOURCE_TO_OBJECT; - } -} diff --git a/tests/Issues/EarlyReturnResourceToObject/EarlyReturnResourceToObjectTest.php b/tests/Issues/EarlyReturnResourceToObject/EarlyReturnResourceToObjectTest.php deleted file mode 100644 index b4e977c1697..00000000000 --- a/tests/Issues/EarlyReturnResourceToObject/EarlyReturnResourceToObjectTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check.php.inc b/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check.php.inc deleted file mode 100644 index 4090261b5a5..00000000000 --- a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_flip.php.inc b/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_flip.php.inc deleted file mode 100644 index b3a0790e675..00000000000 --- a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_flip.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_with_parenthesis.php.inc b/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_with_parenthesis.php.inc deleted file mode 100644 index 5ca090d7b2f..00000000000 --- a/tests/Issues/EarlyReturnResourceToObject/Fixture/double_check_with_parenthesis.php.inc +++ /dev/null @@ -1,46 +0,0 @@ - ------ - diff --git a/tests/Issues/EarlyReturnResourceToObject/config/configured_rule.php b/tests/Issues/EarlyReturnResourceToObject/config/configured_rule.php deleted file mode 100644 index f48b6e3827d..00000000000 --- a/tests/Issues/EarlyReturnResourceToObject/config/configured_rule.php +++ /dev/null @@ -1,12 +0,0 @@ -rule(Php8ResourceReturnToObjectRector::class); - $rectorConfig->sets([SetList::EARLY_RETURN]); -};