Skip to content

Commit

Permalink
Add separate issue
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 19, 2022
1 parent e54d666 commit d970661
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.xsd
Expand Up @@ -325,6 +325,7 @@
<xs:element name="LoopInvalidation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MethodSignatureMismatch" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MethodSignatureMustOmitReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MethodSignatureMustProvideReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockParamType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockPropertyType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockReturnType" type="IssueHandlerType" minOccurs="0" />
Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/error_levels.md
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/issues.md
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md
@@ -0,0 +1,48 @@
# 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.


```php
<?php

class A implements JsonSerializable {
public function jsonSerialize() {
return random_int(0, 1) ? 'test' : 123;
}
}
```

Fix by specifying the correct typehint:

```php
<?php

class A implements JsonSerializable {
public function jsonSerialize(): string|int {
return random_int(0, 1) ? 'test' : 123;
}
}
```

In case the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, a `#[ReturnTypeWillChange]` attribute can be added to silence the PHP deprecation notice and Psalm issue.

```php
<?php

use ReturnTypeWillChange;

class A implements JsonSerializable {
/**
* TODO: Remove this attribute once PHP 7 support is dropped.
*
* @return "test"|123
*/
#[ReturnTypeWillChange]
public function jsonSerialize() {
return random_int(0, 1) ? 'test' : 123;
}
}
```
5 changes: 3 additions & 2 deletions src/Psalm/Internal/Analyzer/MethodComparator.php
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -130,8 +131,8 @@ function (AttributeStorage $s) {
)
) {
IssueBuffer::maybeAdd(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' is missing a return type signature!',
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
Expand Down
9 changes: 9 additions & 0 deletions src/Psalm/Issue/MethodSignatureMustProvideReturnType.php
@@ -0,0 +1,9 @@
<?php

namespace Psalm\Issue;

class MethodSignatureMustProvideReturnType extends CodeIssue
{
public const ERROR_LEVEL = -1;
public const SHORTCODE = 282;
}
4 changes: 2 additions & 2 deletions tests/MethodSignatureTest.php
Expand Up @@ -1582,7 +1582,7 @@ public function test() {
}
}
',
'error_message' => 'MethodSignatureMismatch',
'error_message' => 'MethodSignatureMustProvideReturnType',
[],
false,
'8.0'
Expand All @@ -1595,7 +1595,7 @@ public function jsonSerialize() {
}
}
',
'error_message' => 'MethodSignatureMismatch',
'error_message' => 'MethodSignatureMustProvideReturnType',
[],
false,
'8.1'
Expand Down

0 comments on commit d970661

Please sign in to comment.