Skip to content
Closed
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
15 changes: 15 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,7 @@
$expr = NullsafeOperatorHelper::getNullsafeShortcircuitedExpr($expr);
}

$coalescePropertyFetchTypes = null;
if (
!$context->null()
&& $expr instanceof Expr\BinaryOp\Coalesce
Expand All @@ -2207,6 +2208,17 @@
|| ($context->false() && $type->isSuperTypeOf($scope->getType($expr->right))->yes())
) {
$expr = $expr->left;

if (
$context->true()

Check warning on line 2213 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $expr = $expr->left; if ( - $context->true() + $context->truthy() && $expr instanceof PropertyFetch && $expr->name instanceof Node\Identifier ) {

Check warning on line 2213 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $expr = $expr->left; if ( - $context->true() + $context->truthy() && $expr instanceof PropertyFetch && $expr->name instanceof Node\Identifier ) {
&& $expr instanceof PropertyFetch
&& $expr->name instanceof Node\Identifier
) {
$coalescePropertyFetchTypes = $this->create($expr->var, new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($expr->name->toString()),
]), TypeSpecifierContext::createTruthy(), $scope)->setRootExpr($originalExpr);
}
}
}

Expand Down Expand Up @@ -2321,6 +2333,9 @@
}

$types = new SpecifiedTypes($sureTypes, $sureNotTypes);
if ($coalescePropertyFetchTypes !== null) {
$types = $types->unionWith($coalescePropertyFetchTypes);
}
if (isset($containsNull) && !$containsNull) {
return $this->createNullsafeTypes($originalExpr, $scope, $context, $type)->unionWith($types);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11610.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace Bug11610;

use function PHPStan\Testing\assertType;

function sayHello(string $value): void
{
/** @var object{containers?: array<int>} $responseJson */
$responseJson = json_decode($value);
if (!is_array($responseJson->containers ?? null)) {
throw new \Exception();
}
assertType('object{containers: array<int>}', $responseJson);
}

function sayHello2(string $value): void
{
/** @var object{containers?: array<int>} $responseJson */
$responseJson = json_decode($value);
if (!isset($responseJson->containers) || !is_array($responseJson->containers)) {
throw new \Exception();
}
assertType('object{containers: array<int>}', $responseJson);
}
Loading