Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,15 +1068,18 @@ public function issetCheck(Expr $expr, callable $typeCallback, ?bool $result = n

if ($propertyReflection->hasNativeType() && !$propertyReflection->isVirtual()->yes()) {
if (!$this->hasExpressionType($expr)->yes()) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->issetCheckUndefined($expr->var);
}
$nativeReflection = $propertyReflection->getNativeReflection();
if ($nativeReflection === null || !$nativeReflection->isPromoted() || (!$nativeReflection->isReadOnly() && !$nativeReflection->isHooked())) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->issetCheckUndefined($expr->var);
}

if ($expr->class instanceof Expr) {
return $this->issetCheckUndefined($expr->class);
}
if ($expr->class instanceof Expr) {
return $this->issetCheckUndefined($expr->class);
}

return null;
return null;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ static function (Type $type) use ($typeMessageCallback): ?string {

if (!$scope->hasExpressionType($expr)->yes()) {
$nativeReflection = $propertyReflection->getNativeReflection();
if ($nativeReflection !== null && !$nativeReflection->getNativeReflection()->hasDefaultValue()) {
if (
$nativeReflection !== null
&& !$nativeReflection->getNativeReflection()->hasDefaultValue()
&& (!$nativeReflection->isPromoted() || (!$nativeReflection->isReadOnly() && !$nativeReflection->isHooked()))
) {
return null;
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,28 @@ public function testBug14458(): void
$this->analyse([__DIR__ . '/data/bug-14458.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug14459(): void
{
$this->analyse([__DIR__ . '/data/bug-14459.php'], [
[
'Property Bug14459\Dto::$policyholderId (stdClass) on left side of ?? is not nullable.',
34,
],
]);
}

#[RequiresPhp('>= 8.4')]
public function testBug14459Hooked(): void
{
$this->analyse([__DIR__ . '/data/bug-14459-hooked.php'], [
[
'Property Bug14459Hooked\DtoHooked::$policyholderId (stdClass) on left side of ?? is not nullable.',
21,
],
]);
}

public function testBug14393(): void
{
$this->analyse([__DIR__ . '/data/bug-14393.php'], [
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14459-hooked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // lint >= 8.4

declare(strict_types = 1);

namespace Bug14459Hooked;

final class DtoHooked
{
public function __construct(
public \stdClass $policyholderId {
set => $value;
},
public ?\stdClass $nullablePolicyholderId {
set => $value;
},
) {}
}

function testHooked(DtoHooked $dto): \stdClass
{
$x = $dto->policyholderId ?? new \stdClass();
return $x;
}

function testHookedNullable(DtoHooked $dto): \stdClass
{
$x = $dto->nullablePolicyholderId ?? new \stdClass();
return $x;
}
54 changes: 54 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14459.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14459;

final class Dto
{
public function __construct(
public readonly \stdClass $payerId,
public readonly \stdClass $policyholderId,
public readonly ?\stdClass $nullablePolicyholderId,
) {}
}

final class DtoNonReadonly
{
public function __construct(
public \stdClass $payerId,
) {}
}

class DtoNonPromotedReadonly
{
public readonly \stdClass $payerId;

public function __construct(\stdClass $payerId) {
$this->payerId = $payerId;
}
}

function test(Dto $dto): \stdClass
{
$x = $dto->policyholderId ?? $dto->payerId;
return $x;
}

function testNullable(Dto $dto): \stdClass
{
$x = $dto->nullablePolicyholderId ?? $dto->payerId;
return $x;
}

function testNonReadonly(DtoNonReadonly $dto): \stdClass
{
$x = $dto->payerId ?? new \stdClass();
return $x;
}

function testNonPromotedReadonly(DtoNonPromotedReadonly $dto): \stdClass
{
$x = $dto->payerId ?? new \stdClass();
return $x;
}
Loading