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
74 changes: 74 additions & 0 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand All @@ -60,6 +61,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use TypeError;
use function array_key_last;
use function array_merge;
Expand Down Expand Up @@ -281,6 +283,13 @@
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $identicalSpecifiedTypes, $falseyType);
}

$conditionalExpressions = $this->processArrayValueExtractionForConditionalExpressions(
$scope,
$var->name,
$conditionalExpressions,
$assignedExpr,
);

$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval, $storage);
$scope = $scope->assignVariable($var->name, $type, $scope->getNativeType($assignedExpr), TrinaryLogic::createYes());
foreach ($conditionalExpressions as $exprString => $holders) {
Expand Down Expand Up @@ -1076,4 +1085,69 @@
return false;
}

/**
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @return array<string, ConditionalExpressionHolder[]>
*/
private function processArrayValueExtractionForConditionalExpressions(
Scope $scope,
string $variableName,
array $conditionalExpressions,
Expr $assignedExpr,
): array
{
if (
!$assignedExpr instanceof Expr\FuncCall
|| !$assignedExpr->name instanceof Name
|| !in_array($assignedExpr->name->toLowerString(), ['reset', 'current', 'end'], true)
|| count($assignedExpr->getArgs()) < 1
) {
return $conditionalExpressions;
}

$arrayArg = $assignedExpr->getArgs()[0]->value;
if (!$arrayArg instanceof Variable || !is_string($arrayArg->name)) {
return $conditionalExpressions;
}

$arrayArgType = $scope->getType($arrayArg);
if (!$arrayArgType->isArray()->yes()) {

Check warning on line 1114 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $arrayArgType = $scope->getType($arrayArg); - if (!$arrayArgType->isArray()->yes()) { + if ($arrayArgType->isArray()->no()) { return $conditionalExpressions; }

Check warning on line 1114 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $arrayArgType = $scope->getType($arrayArg); - if (!$arrayArgType->isArray()->yes()) { + if ($arrayArgType->isArray()->no()) { return $conditionalExpressions; }
return $conditionalExpressions;
}

$valueType = $arrayArgType->getIterableValueType();
if (!$valueType instanceof UnionType) {
return $conditionalExpressions;
}

$valueTypeMembers = $valueType->getTypes();
if (count($valueTypeMembers) < 2) {
return $conditionalExpressions;
}

$arrayExprString = '$' . $arrayArg->name;
$keyType = $arrayArgType->getIterableKeyType();

foreach ($valueTypeMembers as $memberType) {
$narrowedArrayType = TypeCombinator::intersect(
$arrayArgType,
new ArrayType($keyType, $memberType),
);

$holder = new ConditionalExpressionHolder(
['$' . $variableName => ExpressionTypeHolder::createYes(
new Variable($variableName),
$memberType,
)],
ExpressionTypeHolder::createYes(
$arrayArg,
$narrowedArrayType,
),
);
$conditionalExpressions[$arrayExprString][$holder->getKey()] = $holder;
}

return $conditionalExpressions;
}

}
101 changes: 101 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14201.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14201;

use function PHPStan\Testing\assertType;

class Foo { public function __construct(public string $fooName) {}}
class Bar { public function __construct(public string $barName) {}}

class HelloWorld
{
/**
* @param Foo[]|Bar[] $items
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need another test with array<Foo|Bar>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already confirmed — both PHPStan and tests passed. The commit with the array<Foo|Bar> test cases has been pushed.

*/
public function doitMatch(array $items): void
{
if ([] === $items) {return; }

$first = reset($items);
match (true) {
$first instanceOf Foo => array_map(function ($i) {
assertType('Bug14201\Foo', $i);
return $i->fooName;
}, $items),
$first instanceOf Bar => array_map(function ($i) {
assertType('Bug14201\Bar', $i);
return $i->barName;
}, $items),
default => throw new \RuntimeException('None of Foo nor Bar')
};
}

/**
* @param Foo[]|Bar[] $items
*/
public function doitIf(array $items): void
{
if ([] === $items) {return; }

$first = reset($items);
if ($first instanceof Foo) {
assertType('non-empty-array<Bug14201\Foo>', $items);
array_map(function ($i) {
assertType('Bug14201\Foo', $i);
return $i->fooName;
}, $items);
} elseif ($first instanceof Bar) {
assertType('non-empty-array<Bug14201\Bar>', $items);
array_map(function ($i) {
assertType('Bug14201\Bar', $i);
return $i->barName;
}, $items);
}
}

/**
* @param array<Foo|Bar> $items
*/
public function doitMatchArrayUnion(array $items): void
{
if ([] === $items) {return; }

$first = reset($items);
match (true) {
$first instanceOf Foo => array_map(function ($i) {
assertType('Bug14201\Foo', $i);
return $i->fooName;
}, $items),
$first instanceOf Bar => array_map(function ($i) {
assertType('Bug14201\Bar', $i);
return $i->barName;
}, $items),
default => throw new \RuntimeException('None of Foo nor Bar')
};
}

/**
* @param array<Foo|Bar> $items
*/
public function doitIfArrayUnion(array $items): void
{
if ([] === $items) {return; }

$first = reset($items);
if ($first instanceof Foo) {
assertType('non-empty-array<Bug14201\Foo>', $items);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I processed this review but have nothing to report.

array_map(function ($i) {
assertType('Bug14201\Foo', $i);
return $i->fooName;
}, $items);
} elseif ($first instanceof Bar) {
assertType('non-empty-array<Bug14201\Bar>', $items);
array_map(function ($i) {
assertType('Bug14201\Bar', $i);
return $i->barName;
}, $items);
}
}
}
Loading