Skip to content

Commit

Permalink
Default value null does not make promoted property type nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 2, 2024
1 parent b89742c commit b2177e3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -72,6 +72,7 @@ lint:
--exclude tests/PHPStan/Rules/Keywords/data/declare-strict-nonsense-bool.php \
--exclude tests/PHPStan/Rules/Keywords/data/declare-inline-html.php \
--exclude tests/PHPStan/Rules/Classes/data/extends-readonly-class.php \
--exclude tests/PHPStan/Rules/Classes/data/instantiation-promoted-properties.php \
src tests

cs:
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -2664,7 +2664,7 @@ private function getRealParameterTypes(Node\FunctionLike $functionLike): array
}
$realParameterTypes[$parameter->var->name] = $this->getFunctionType(
$parameter->type,
$this->isParameterValueNullable($parameter),
$this->isParameterValueNullable($parameter) && $parameter->flags === 0,
false,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Methods/IncompatibleDefaultParameterTypeRule.php
Expand Up @@ -43,7 +43,8 @@ public function processNode(Node $node, Scope $scope): array
}

$defaultValueType = $scope->getType($param->default);
$parameterType = $parameters->getParameters()[$paramI]->getType();
$parameter = $parameters->getParameters()[$paramI];
$parameterType = $parameter->getType();
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);

$accepts = $parameterType->acceptsWithReason($defaultValueType, true);
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/data/promoted-properties-types.php
Expand Up @@ -103,3 +103,16 @@ function (Baz $baz): void {
assertType('array<int, string>', $baz->anotherPhpDocArray);
assertType('stdClass', $baz->templateProperty);
};

class PromotedPropertyNotNullable
{

public function __construct(
public int $intProp = null,
) {}

}

function (PromotedPropertyNotNullable $p) {
assertType('int', $p->intProp);
};
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Expand Up @@ -276,6 +276,10 @@ public function testPromotedProperties(): void
'Parameter #2 $bar of class InstantiationPromotedProperties\Bar constructor expects array<string>, array<int, int> given.',
33,
],
[
'Parameter #1 $intProp of class InstantiationPromotedProperties\PromotedPropertyNotNullable constructor expects int, null given.',
46,
],
]);
}

Expand Down
@@ -1,4 +1,4 @@
<?php // lint >= 8.0
<?php

namespace InstantiationPromotedProperties;

Expand Down Expand Up @@ -32,3 +32,16 @@ function () {
new Bar([], ['foo']);
new Bar([], [1]);
};

class PromotedPropertyNotNullable
{

public function __construct(
private int $intProp = null,
) {}

}

function () {
new PromotedPropertyNotNullable(null);
};
Expand Up @@ -66,6 +66,10 @@ public function testDefaultValueForPromotedProperty(): void
'Default value of the parameter #2 $foo (string) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
10,
],
[
'Default value of the parameter #4 $intProp (null) of method DefaultValueForPromotedProperty\Foo::__construct() is incompatible with type int.',
12,
],
]);
}

Expand Down
Expand Up @@ -8,7 +8,8 @@ class Foo
public function __construct(
private int $foo = 'foo',
/** @var int */ private $foo = '',
private int $baz = 1
private int $baz = 1,
private int $intProp = null,
) {}

}

0 comments on commit b2177e3

Please sign in to comment.