From 603714518b3894a57b464920b906a13332f23c02 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 31 Jan 2022 14:37:42 +0200 Subject: [PATCH 1/4] Revert "PHP 8.1: Report missing typehints in overridden native methods" --- config.xsd | 1 - docs/running_psalm/error_levels.md | 1 - docs/running_psalm/issues.md | 1 - .../MethodSignatureMustProvideReturnType.md | 17 --------- .../Internal/Analyzer/MethodComparator.php | 35 +------------------ .../MethodSignatureMustProvideReturnType.php | 9 ----- tests/DocumentationTest.php | 4 --- tests/MethodSignatureTest.php | 31 ---------------- 8 files changed, 1 insertion(+), 98 deletions(-) delete mode 100644 docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md delete mode 100644 src/Psalm/Issue/MethodSignatureMustProvideReturnType.php diff --git a/config.xsd b/config.xsd index a4ba114c2b5..3c8257bc1df 100644 --- a/config.xsd +++ b/config.xsd @@ -331,7 +331,6 @@ - diff --git a/docs/running_psalm/error_levels.md b/docs/running_psalm/error_levels.md index c58cca026ff..3babb92488a 100644 --- a/docs/running_psalm/error_levels.md +++ b/docs/running_psalm/error_levels.md @@ -56,7 +56,6 @@ Level 5 and above allows a more non-verifiable code, and higher levels are even - [InvalidThrow](issues/InvalidThrow.md) - [LoopInvalidation](issues/LoopInvalidation.md) - [MethodSignatureMustOmitReturnType](issues/MethodSignatureMustOmitReturnType.md) - - [MethodSignatureMustProvideReturnType](issues/MethodSignatureMustProvideReturnType.md) - [MissingDependency](issues/MissingDependency.md) - [MissingFile](issues/MissingFile.md) - [MissingImmutableAnnotation](issues/MissingImmutableAnnotation.md) diff --git a/docs/running_psalm/issues.md b/docs/running_psalm/issues.md index 50385f60e31..c471c86f4cb 100644 --- a/docs/running_psalm/issues.md +++ b/docs/running_psalm/issues.md @@ -99,7 +99,6 @@ - [LoopInvalidation](issues/LoopInvalidation.md) - [MethodSignatureMismatch](issues/MethodSignatureMismatch.md) - [MethodSignatureMustOmitReturnType](issues/MethodSignatureMustOmitReturnType.md) - - [MethodSignatureMustProvideReturnType](issues/MethodSignatureMustProvideReturnType.md) - [MismatchingDocblockParamType](issues/MismatchingDocblockParamType.md) - [MismatchingDocblockPropertyType](issues/MismatchingDocblockPropertyType.md) - [MismatchingDocblockReturnType](issues/MismatchingDocblockReturnType.md) diff --git a/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md b/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md deleted file mode 100644 index 80aa53c8ac2..00000000000 --- a/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md +++ /dev/null @@ -1,17 +0,0 @@ -# MethodSignatureMustProvideReturnType - -In PHP 8.1+, [most non-final internal methods now require overriding methods to declare a compatible return type, otherwise a deprecated notice is emitted during inheritance validation](https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.core.type-compatibility-internal). - -This issue is emitted when a method overriding a native method is defined without a return type. - -**Only if** the return type cannot be declared to keep support for PHP 7, a `#[ReturnTypeWillChange]` attribute can be added to silence the PHP deprecation notice and Psalm issue. - -```php - 'A']; - } -} -``` diff --git a/src/Psalm/Internal/Analyzer/MethodComparator.php b/src/Psalm/Internal/Analyzer/MethodComparator.php index dd0b6ee6497..16ff2d9d98b 100644 --- a/src/Psalm/Internal/Analyzer/MethodComparator.php +++ b/src/Psalm/Internal/Analyzer/MethodComparator.php @@ -21,14 +21,12 @@ use Psalm\Issue\ImplementedReturnTypeMismatch; use Psalm\Issue\LessSpecificImplementedReturnType; use Psalm\Issue\MethodSignatureMismatch; -use Psalm\Issue\MethodSignatureMustProvideReturnType; use Psalm\Issue\MissingImmutableAnnotation; use Psalm\Issue\MoreSpecificImplementedParamType; use Psalm\Issue\OverriddenMethodAccess; use Psalm\Issue\ParamNameMismatch; use Psalm\Issue\TraitMethodSignatureMismatch; use Psalm\IssueBuffer; -use Psalm\Storage\AttributeStorage; use Psalm\Storage\ClassLikeStorage; use Psalm\Storage\FunctionLikeParameter; use Psalm\Storage\MethodStorage; @@ -37,7 +35,6 @@ use Psalm\Type\Atomic\TTemplateParam; use Psalm\Type\Union; -use function array_filter; use function in_array; use function strpos; use function strtolower; @@ -116,29 +113,6 @@ public static function compare( ); } - if (!$guide_classlike_storage->user_defined - && $implementer_classlike_storage->user_defined - && $codebase->analysis_php_version_id >= 80100 - && ($guide_method_storage->return_type - || $guide_method_storage->signature_return_type - ) - && !$implementer_method_storage->signature_return_type - && !array_filter( - $implementer_method_storage->attributes, - function (AttributeStorage $s) { - return $s->fq_class_name === 'ReturnTypeWillChange'; - } - ) - ) { - IssueBuffer::maybeAdd( - new MethodSignatureMustProvideReturnType( - 'Method ' . $cased_implementer_method_id . ' must have a return type signature!', - $implementer_method_storage->location ?: $code_location - ), - $suppressed_issues + $implementer_classlike_storage->suppressed_issues - ); - } - if ($guide_method_storage->return_type && $implementer_method_storage->return_type && !$implementer_method_storage->inherited_return_type @@ -888,14 +862,7 @@ private static function compareMethodSignatureReturnTypes( $implementer_signature_return_type, $guide_signature_return_type ) - : (!$implementer_signature_return_type - && $guide_signature_return_type->isMixed() - ? false - : UnionTypeComparator::isContainedByInPhp( - $implementer_signature_return_type, - $guide_signature_return_type - ) - ); + : UnionTypeComparator::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type); if (!$is_contained_by) { if ($codebase->php_major_version >= 8 diff --git a/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php b/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php deleted file mode 100644 index ab05f06c0d4..00000000000 --- a/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php +++ /dev/null @@ -1,9 +0,0 @@ -markTestSkipped(); } - if (strpos($error_message, 'MethodSignatureMustProvideReturnType') !== false) { - $php_version = '8.1'; - } - $this->project_analyzer->setPhpVersion($php_version, 'tests'); if ($check_references) { diff --git a/tests/MethodSignatureTest.php b/tests/MethodSignatureTest.php index 87c3bc71dbf..11f217b172f 100644 --- a/tests/MethodSignatureTest.php +++ b/tests/MethodSignatureTest.php @@ -1569,37 +1569,6 @@ public function bar(string ...$_args): void {} ', 'error_message' => 'MethodSignatureMismatch', ], - 'noMixedTypehintInDescendant' => [ - ' 'MethodSignatureMismatch', - [], - false, - '8.0' - ], - 'noTypehintInNativeDescendant' => [ - ' 'MethodSignatureMustProvideReturnType', - [], - false, - '8.1' - ], ]; } } From 3ae067795dc54a8eca6f391836d8fa334c9d6470 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 31 Jan 2022 15:11:06 +0200 Subject: [PATCH 2/4] Update baseline --- psalm-baseline.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index dcd0433d3dd..ab3669360b9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + $comment_block->tags['variablesfrom'][0] @@ -21,6 +21,9 @@ $matches[0] $symbol_parts[1] + + $analysis_php_version_id + From f61193b7981f6e9c5f2050360d60991c73712a55 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 31 Jan 2022 23:38:15 +0200 Subject: [PATCH 3/4] Reinstate MethodSignatureMustProvideReturnType It's never emitted, but is there for BC. --- config.xsd | 1 + docs/running_psalm/error_levels.md | 1 + docs/running_psalm/issues.md | 1 + .../MethodSignatureMustProvideReturnType.md | 16 ++++++++++++++++ .../MethodSignatureMustProvideReturnType.php | 9 +++++++++ tests/DocumentationTest.php | 4 ++++ 6 files changed, 32 insertions(+) create mode 100644 docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md create mode 100644 src/Psalm/Issue/MethodSignatureMustProvideReturnType.php diff --git a/config.xsd b/config.xsd index 3c8257bc1df..a4ba114c2b5 100644 --- a/config.xsd +++ b/config.xsd @@ -331,6 +331,7 @@ + diff --git a/docs/running_psalm/error_levels.md b/docs/running_psalm/error_levels.md index 3babb92488a..c58cca026ff 100644 --- a/docs/running_psalm/error_levels.md +++ b/docs/running_psalm/error_levels.md @@ -56,6 +56,7 @@ Level 5 and above allows a more non-verifiable code, and higher levels are even - [InvalidThrow](issues/InvalidThrow.md) - [LoopInvalidation](issues/LoopInvalidation.md) - [MethodSignatureMustOmitReturnType](issues/MethodSignatureMustOmitReturnType.md) + - [MethodSignatureMustProvideReturnType](issues/MethodSignatureMustProvideReturnType.md) - [MissingDependency](issues/MissingDependency.md) - [MissingFile](issues/MissingFile.md) - [MissingImmutableAnnotation](issues/MissingImmutableAnnotation.md) diff --git a/docs/running_psalm/issues.md b/docs/running_psalm/issues.md index c471c86f4cb..50385f60e31 100644 --- a/docs/running_psalm/issues.md +++ b/docs/running_psalm/issues.md @@ -99,6 +99,7 @@ - [LoopInvalidation](issues/LoopInvalidation.md) - [MethodSignatureMismatch](issues/MethodSignatureMismatch.md) - [MethodSignatureMustOmitReturnType](issues/MethodSignatureMustOmitReturnType.md) + - [MethodSignatureMustProvideReturnType](issues/MethodSignatureMustProvideReturnType.md) - [MismatchingDocblockParamType](issues/MismatchingDocblockParamType.md) - [MismatchingDocblockPropertyType](issues/MismatchingDocblockPropertyType.md) - [MismatchingDocblockReturnType](issues/MismatchingDocblockReturnType.md) diff --git a/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md b/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md new file mode 100644 index 00000000000..0c85db90d36 --- /dev/null +++ b/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md @@ -0,0 +1,16 @@ +# MethodSignatureMustProvideReturnType + +In PHP 8.1+, [most non-final internal methods now require overriding methods to declare a compatible return type, otherwise a deprecated notice is emitted during inheritance validation](https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.core.type-compatibility-internal). + +This issue is emitted when a method overriding a native method is defined without a return type. + +**Only if** the return type cannot be declared to keep support for PHP 7, a `#[ReturnTypeWillChange]` attribute can be added to silence the PHP deprecation notice and Psalm issue. + +```php + 'A']; + } +} +``` diff --git a/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php b/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php new file mode 100644 index 00000000000..ab05f06c0d4 --- /dev/null +++ b/src/Psalm/Issue/MethodSignatureMustProvideReturnType.php @@ -0,0 +1,9 @@ + Date: Tue, 1 Feb 2022 00:06:25 +0200 Subject: [PATCH 4/4] Suppress UnusedClass --- psalm-baseline.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ab3669360b9..3b9bb725ee3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -662,6 +662,11 @@ array_keys($template_type_map[$template_param_name])[0] + + + MethodSignatureMustProvideReturnType + + VirtualClass