Skip to content

Fix array_column inference #4201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
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
53 changes: 22 additions & 31 deletions src/Type/Php/ArrayColumnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
Expand Down Expand Up @@ -39,14 +38,10 @@ public function getReturnValueType(Type $arrayType, Type $columnType, Scope $sco
}

$iterableValueType = $arrayType->getIterableValueType();
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false);
[$returnValueType, $certainty] = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope);

if ($returnValueType === null) {
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, true);
if (!$certainty->yes()) {
$iterableAtLeastOnce = TrinaryLogic::createMaybe();
if ($returnValueType === null) {
throw new ShouldNotHappenException();
}
}

return [$returnValueType, $iterableAtLeastOnce];
Expand All @@ -57,15 +52,12 @@ public function getReturnIndexType(Type $arrayType, Type $indexType, Scope $scop
if (!$indexType->isNull()->yes()) {
$iterableValueType = $arrayType->getIterableValueType();

$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false);
if ($type !== null) {
[$type, $certainty] = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope);
if ($certainty->yes()) {
return $type;
}

$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
return TypeCombinator::union($type, new IntegerType());
}
return TypeCombinator::union($type, new IntegerType());
}

return new IntegerType();
Expand Down Expand Up @@ -96,25 +88,20 @@ public function handleConstantArray(ConstantArrayType $arrayType, Type $columnTy
$builder = ConstantArrayTypeBuilder::createEmpty();

foreach ($arrayType->getValueTypes() as $i => $iterableValueType) {
$valueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false);
if ($valueType === null) {
[$valueType, $certainty] = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope);
if (!$certainty->yes()) {
return null;
}
if ($valueType instanceof NeverType) {
continue;
}

if (!$indexType->isNull()->yes()) {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false);
if ($type !== null) {
[$type, $certainty] = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope);
if ($certainty->yes()) {
$keyType = $type;
} else {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
$keyType = TypeCombinator::union($type, new IntegerType());
} else {
$keyType = null;
}
$keyType = TypeCombinator::union($type, new IntegerType());
}
} else {
$keyType = null;
Expand All @@ -129,11 +116,14 @@ public function handleConstantArray(ConstantArrayType $arrayType, Type $columnTy
return $builder->getArray();
}

private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope, bool $allowMaybe): ?Type
/**
* @return array{Type, TrinaryLogic}
*/
private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope): array
{
$offsetIsNull = $offsetOrProperty->isNull();
if ($offsetIsNull->yes()) {
return $type;
return [$type, TrinaryLogic::createYes()];
}

$returnTypes = [];
Expand All @@ -145,13 +135,13 @@ private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $
if (!$type->canAccessProperties()->no()) {
$propertyTypes = $offsetOrProperty->getConstantStrings();
if ($propertyTypes === []) {
return new MixedType();
return [new MixedType(), TrinaryLogic::createMaybe()];
}
foreach ($propertyTypes as $propertyType) {
$propertyName = $propertyType->getValue();
$hasProperty = $type->hasProperty($propertyName);
if ($hasProperty->maybe()) {
return $allowMaybe ? new MixedType() : null;
return [new MixedType(), TrinaryLogic::createMaybe()];
}
if (!$hasProperty->yes()) {
continue;
Expand All @@ -161,21 +151,22 @@ private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $
}
}

$certainty = TrinaryLogic::createYes();
if ($type->isOffsetAccessible()->yes()) {
$hasOffset = $type->hasOffsetValueType($offsetOrProperty);
if (!$allowMaybe && $hasOffset->maybe()) {
return null;
if ($hasOffset->maybe()) {
$certainty = TrinaryLogic::createMaybe();
}
if (!$hasOffset->no()) {
$returnTypes[] = $type->getOffsetValueType($offsetOrProperty);
}
}

if ($returnTypes === []) {
return new NeverType();
return [new NeverType(), TrinaryLogic::createYes()];
}

return TypeCombinator::union(...$returnTypes);
return [TypeCombinator::union(...$returnTypes), $certainty];
}

private function castToArrayKeyType(Type $type): Type
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/EmptyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ public function testBug12658(): void
$this->analyse([__DIR__ . '/data/bug-12658.php'], []);
}

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

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

#[RequiresPhp('>= 8.0')]
public function testIssetAfterRememberedConstructor(): void
{
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10367.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug10367;

final class PackingSlipPdf
{
public function __construct(array $packingSlipItems, string $fullName)
{
if (empty($packingSlipItems)) {
error_log('$packingSlipItems is empty: check company: ' . $fullName);
} else {
$freightNumbers = array_column($packingSlipItems, 3);
if (empty($freightNumbers)) {
error_log('$freightNumbers is empty: check company: ' . $fullName);
error_log('$freightNumbers is empty: check dataset: ' . print_r($packingSlipItems[0], true));
} elseif (empty($freightNumbers[0])) {
error_log('$freightNumbers[0] is empty: check company: ' . $fullName);
error_log('$freightNumbers[0] is empty: check freigthnumbers: ' . print_r($freightNumbers, true));
error_log('$freightNumbers[0] is empty: check dataset: ' . print_r($packingSlipItems[0], true));
}
}
}
}

$testArray = [
[1 => 123, 2 => 413, 4 => 132],
[1 => 123, 2 => 413, 4 => 132],
];

$packingSlipPdf = new PackingSlipPdf($testArray, "TestName");
Loading