Skip to content

Commit b2177e3

Browse files
committed
Default value null does not make promoted property type nullable
1 parent b89742c commit b2177e3

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ lint:
7272
--exclude tests/PHPStan/Rules/Keywords/data/declare-strict-nonsense-bool.php \
7373
--exclude tests/PHPStan/Rules/Keywords/data/declare-inline-html.php \
7474
--exclude tests/PHPStan/Rules/Classes/data/extends-readonly-class.php \
75+
--exclude tests/PHPStan/Rules/Classes/data/instantiation-promoted-properties.php \
7576
src tests
7677

7778
cs:

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ private function getRealParameterTypes(Node\FunctionLike $functionLike): array
26642664
}
26652665
$realParameterTypes[$parameter->var->name] = $this->getFunctionType(
26662666
$parameter->type,
2667-
$this->isParameterValueNullable($parameter),
2667+
$this->isParameterValueNullable($parameter) && $parameter->flags === 0,
26682668
false,
26692669
);
26702670
}

src/Rules/Methods/IncompatibleDefaultParameterTypeRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function processNode(Node $node, Scope $scope): array
4343
}
4444

4545
$defaultValueType = $scope->getType($param->default);
46-
$parameterType = $parameters->getParameters()[$paramI]->getType();
46+
$parameter = $parameters->getParameters()[$paramI];
47+
$parameterType = $parameter->getType();
4748
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);
4849

4950
$accepts = $parameterType->acceptsWithReason($defaultValueType, true);

tests/PHPStan/Analyser/data/promoted-properties-types.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,16 @@ function (Baz $baz): void {
103103
assertType('array<int, string>', $baz->anotherPhpDocArray);
104104
assertType('stdClass', $baz->templateProperty);
105105
};
106+
107+
class PromotedPropertyNotNullable
108+
{
109+
110+
public function __construct(
111+
public int $intProp = null,
112+
) {}
113+
114+
}
115+
116+
function (PromotedPropertyNotNullable $p) {
117+
assertType('int', $p->intProp);
118+
};

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ public function testPromotedProperties(): void
276276
'Parameter #2 $bar of class InstantiationPromotedProperties\Bar constructor expects array<string>, array<int, int> given.',
277277
33,
278278
],
279+
[
280+
'Parameter #1 $intProp of class InstantiationPromotedProperties\PromotedPropertyNotNullable constructor expects int, null given.',
281+
46,
282+
],
279283
]);
280284
}
281285

tests/PHPStan/Rules/Classes/data/instantiation-promoted-properties.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php // lint >= 8.0
1+
<?php
22

33
namespace InstantiationPromotedProperties;
44

@@ -32,3 +32,16 @@ function () {
3232
new Bar([], ['foo']);
3333
new Bar([], [1]);
3434
};
35+
36+
class PromotedPropertyNotNullable
37+
{
38+
39+
public function __construct(
40+
private int $intProp = null,
41+
) {}
42+
43+
}
44+
45+
function () {
46+
new PromotedPropertyNotNullable(null);
47+
};

tests/PHPStan/Rules/Methods/IncompatibleDefaultParameterTypeRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function testDefaultValueForPromotedProperty(): void
6666
'Default value of the parameter #2 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
6767
10,
6868
],
69+
[
70+
'Default value of the parameter #4 $intProp (null) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
71+
12,
72+
],
6973
]);
7074
}
7175

tests/PHPStan/Rules/Methods/data/default-value-for-promoted-property.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class Foo
88
public function __construct(
99
private int $foo = 'foo',
1010
/** @var int */ private $foo = '',
11-
private int $baz = 1
11+
private int $baz = 1,
12+
private int $intProp = null,
1213
) {}
1314

1415
}

0 commit comments

Comments
 (0)