Skip to content

Commit

Permalink
Standalone null with default value null does not make parameter impli…
Browse files Browse the repository at this point in the history
…citly nullable
  • Loading branch information
ondrejmirtes committed Feb 3, 2025
1 parent c126d48 commit eded2c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,13 @@ private function checkImplicitlyNullableType(
return null;
}

if ($type instanceof Identifier && strtolower($type->name) === 'null') {
return null;
}
if ($type instanceof Name && $type->toLowerString() === 'null') {
return null;
}

if ($type instanceof UnionType) {
foreach ($type->types as $innerType) {
if ($innerType instanceof Identifier && strtolower($innerType->name) === 'null') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,12 @@ public function testDeprecatedImplicitlyNullableParameterType(): void
]);
}

public function testBug12501(): void
{
if (PHP_VERSION_ID < 80400) {
self::markTestSkipped('This test needs PHP 8.4.');
}
$this->analyse([__DIR__ . '/data/bug-12501.php'], []);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12501.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // lint >= 8.4

declare(strict_types = 1);

namespace Bug12501;

final readonly class EmptyObject {
public function __construct(
public null $value1 = null,
) {}
}

0 comments on commit eded2c3

Please sign in to comment.