Skip to content

Commit 7c8e81d

Browse files
committed
Promoted properties do not have a default value
1 parent 1d72885 commit 7c8e81d

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ private function processStmtNode(
521521
$param->var->name,
522522
$param->flags,
523523
$param->type,
524-
$param->default,
524+
null,
525525
$phpDoc,
526526
true,
527527
$param

tests/PHPStan/Rules/Methods/IncompatibleDefaultParameterTypeRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,22 @@ public function testNewInInitializers(): void
5959
]);
6060
}
6161

62+
public function testDefaultValueForPromotedProperty(): void
63+
{
64+
if (!self::$useStaticReflectionProvider) {
65+
$this->markTestSkipped('Test requires static reflection.');
66+
}
67+
68+
$this->analyse([__DIR__ . '/data/default-value-for-promoted-property.php'], [
69+
[
70+
'Default value of the parameter #1 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
71+
9,
72+
],
73+
[
74+
'Default value of the parameter #2 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
75+
10,
76+
],
77+
]);
78+
}
79+
6280
}

tests/PHPStan/Rules/Properties/DefaultValueTypesAssignedToPropertiesRuleTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,4 @@ public function testDefaultValueForNativePropertyType(): void
4747
]);
4848
}
4949

50-
public function testDefaultValueForPromotedProperty(): void
51-
{
52-
if (!self::$useStaticReflectionProvider) {
53-
$this->markTestSkipped('Test requires static reflection.');
54-
}
55-
56-
$this->analyse([__DIR__ . '/data/default-value-for-promoted-property.php'], [
57-
[
58-
'Property DefaultValueForPromotedProperty\Foo::$foo (int) does not accept default value of type string.',
59-
9,
60-
],
61-
[
62-
'Property DefaultValueForPromotedProperty\Foo::$foo (int) does not accept default value of type string.',
63-
10,
64-
],
65-
]);
66-
}
67-
6850
}

tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function dataRule(): array
4646
'Readonly property cannot have a default value.',
4747
10,
4848
],
49+
[
50+
'Readonly properties are supported only on PHP 8.1 and later.',
51+
16,
52+
],
4953
],
5054
],
5155
[

tests/PHPStan/Rules/Properties/data/read-only-property.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ class Foo
1010
private readonly int $baz = 0;
1111

1212
}
13+
14+
final class ErrorResponse
15+
{
16+
public function __construct(public readonly string $message = '')
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)