Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Psalm\Issue; | ||
|
||
class MethodSignatureMustProvideReturnType extends CodeIssue | ||
{ | ||
public const ERROR_LEVEL = -1; | ||
public const SHORTCODE = 282; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters