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
8 changes: 8 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,14 @@ public function issetCheck(Expr $expr, callable $typeCallback, ?bool $result = n
}

if ($result !== null) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->issetCheck($expr->var, $typeCallback, $result);
}

if ($expr->class instanceof Expr) {
return $this->issetCheck($expr->class, $typeCallback, $result);
}

return $result;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ static function (Type $type) use ($typeMessageCallback): ?string {
$propertyDescription = $this->propertyDescriptor->describeProperty($propertyReflection, $scope, $expr);
$propertyType = $propertyReflection->getWritableType();
if ($error !== null) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->check($expr->var, $scope, $operatorDescription, $identifier, $typeMessageCallback, $error);
}

if ($expr->class instanceof Expr) {
return $this->check($expr->class, $scope, $operatorDescription, $identifier, $typeMessageCallback, $error);
}

return $error;
}
if (!$this->checkAdvancedIsset) {
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14555.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14555Nsrt;

use function PHPStan\Testing\assertType;

class ValueObject {
function __construct(
public readonly string $value,
) {}
}

class SomeDTO {
function __construct(
public readonly ValueObject $value,
) {}
}

/** @param array<string, list<SomeDTO>> $array */
function testCoalesceType(array $array): void
{
$someValue = $array['someKey'][0]->value->value ?? null;
assertType('string|null', $someValue);
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ public function testBug9503(): void
$this->analyse([__DIR__ . '/data/bug-9503.php'], []);
}

public function testBug14555(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-14555.php'], []);
}

public function testBug14393(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ public function testBug14458(): void
$this->analyse([__DIR__ . '/data/bug-14458.php'], []);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug14555(): void
{
$this->analyse([__DIR__ . '/data/bug-14555.php'], []);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug14459(): void
{
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14555.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14555;

class ValueObject {
function __construct(
public readonly string $value,
) {}
}

class SomeDTO {
function __construct(
public readonly ValueObject $value,
) {}
}

class StaticHolder {
public static ValueObject $value;
}

/** @param array<string, list<SomeDTO>> $array */
function exampleNullCoalesce(array $array): void
{
$someValue = $array['someKey'][0]->value->value ?? null;

$dto = $array['someKey'][0] ?? null;
$someValue2 = $dto->value->value ?? null;
}

/** @param array<string, list<SomeDTO>> $array */
function exampleIsset(array $array): void
{
if (isset($array['someKey'][0]->value->value)) {
echo 'yes';
}
}

/** @param array<string, list<SomeDTO>> $array */
function exampleNullCoalesceAssign(array $array): void
{
$someValue = $array['someKey'][0]->value->value ??= 'default';
}

/** @param array<string, list<StaticHolder>> $array */
function exampleStaticProperty(array $array): void
{
$someValue = $array['someKey'][0]::$value->value ?? null;
}
Loading