Skip to content

Commit

Permalink
NullableTypeSniff: 'mixed' is already nullable
Browse files Browse the repository at this point in the history
The mixed type (PHP 8+) already includes 'null'. So trying to fix that
to '?mixed' fails with a type error.

Bug: T378552
Change-Id: I8e3034888942ed398d6459f45bc3404d358fc289
  • Loading branch information
supertassu committed Dec 9, 2024
1 parent fd6ecb5 commit 4621920
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions MediaWiki/Sniffs/Usage/NullableTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function process( File $phpcsFile, $stackPtr ) {
foreach ( $params as $param ) {
if (
$param['type_hint'] &&
$param['type_hint'] !== 'mixed' &&
$param['nullable_type'] === false &&
array_key_exists( 'default', $param ) &&
$param['default'] === 'null'
Expand Down
3 changes: 3 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function testClosure(): void {
public function testArrowFunctions(): void {
$c = fn ( MyClass $x = null ) => $x;
}

public function testMixedDefaultsToNull( mixed $x = null ): void {
}
}

class PassedExamples {
Expand Down
3 changes: 3 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class FailedExamples {
public function testArrowFunctions(): void {
$c = fn ( ?MyClass $x = null ) => $x;
}

public function testMixedDefaultsToNull( mixed $x = null ): void {
}
}

class PassedExamples {
Expand Down

0 comments on commit 4621920

Please sign in to comment.