Skip to content

Commit

Permalink
Promoted properties do not have a default value
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 16, 2021
1 parent 1d72885 commit 7c8e81d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ private function processStmtNode(
$param->var->name,
$param->flags,
$param->type,
$param->default,
null,
$phpDoc,
true,
$param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ public function testNewInInitializers(): void
]);
}

public function testDefaultValueForPromotedProperty(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires static reflection.');
}

$this->analyse([__DIR__ . '/data/default-value-for-promoted-property.php'], [
[
'Default value of the parameter #1 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
9,
],
[
'Default value of the parameter #2 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
10,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,4 @@ public function testDefaultValueForNativePropertyType(): void
]);
}

public function testDefaultValueForPromotedProperty(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires static reflection.');
}

$this->analyse([__DIR__ . '/data/default-value-for-promoted-property.php'], [
[
'Property DefaultValueForPromotedProperty\Foo::$foo (int) does not accept default value of type string.',
9,
],
[
'Property DefaultValueForPromotedProperty\Foo::$foo (int) does not accept default value of type string.',
10,
],
]);
}

}
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function dataRule(): array
'Readonly property cannot have a default value.',
10,
],
[
'Readonly properties are supported only on PHP 8.1 and later.',
16,
],
],
],
[
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Properties/data/read-only-property.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ class Foo
private readonly int $baz = 0;

}

final class ErrorResponse
{
public function __construct(public readonly string $message = '')
{
}
}

0 comments on commit 7c8e81d

Please sign in to comment.