Skip to content
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
13 changes: 9 additions & 4 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ public function getConstantStrings(): array

public function accepts(Type $type, bool $strictTypes): AcceptsResult
{
$isArray = $type->isArray();
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
$isNonEmptyArray = $isArray->and($isIterableAtLeastOnce);

if ($isNonEmptyArray->yes()) {
return AcceptsResult::createYes();
}

if ($type instanceof CompoundType) {
return $type->isAcceptedBy($this, $strictTypes);
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this. All Accessory types do this. So it has to stay.

But you can do another condition before this one, like AccessoryNonEmptyStringType does:

	public function accepts(Type $type, bool $strictTypes): AcceptsResult
	{
		if ($type->isNonEmptyString()->yes()) {
			return AcceptsResult::createYes();
		}
		if ($type instanceof CompoundType) {
			return $type->isAcceptedBy($this, $strictTypes);
		}

		return new AcceptsResult($type->isNonEmptyString(), []);
	}

You can also try doing the same thing (call related method before instanceof CompoundType) in other Accessory types so they all still look the same..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will look into other accessories in a separate PR

}

$isArray = $type->isArray();
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();

return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []);
return new AcceptsResult($isNonEmptyArray, []);
}

public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/ClosureReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ public function testBugFunctionMethodConstants(): void
$this->analyse([__DIR__ . '/data/bug-anonymous-function-method-constant.php'], []);
}

public function testBug13964(): void
{
$this->analyse([__DIR__ . '/data/bug-13964.php'], []);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13964.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug13964;

/** @var array<string, array<mixed>> $state */
$state = (fn()=>[])();

$state = array_map(function (array $item): array {
if (array_key_exists('type', $item) && array_key_exists('data', $item)) {
return $item;
}

return [
'type' => 'hello',
'data' => [],
];
}, $state);
26 changes: 26 additions & 0 deletions tests/PHPStan/Type/IntersectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Accessory\OversizedArrayType;
Expand Down Expand Up @@ -70,6 +72,30 @@ public static function dataAccepts(): Iterator
new CallableType(),
TrinaryLogic::createMaybe(),
];

yield [
TypeCombinator::intersect(
new ArrayType(new MixedType(), new MixedType()),
new NonEmptyArrayType(),
),
TypeCombinator::intersect(
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new ConstantStringType('some-key')),
),
TrinaryLogic::createYes(),
];

yield [
TypeCombinator::intersect(
new ArrayType(new MixedType(), new MixedType()),
new NonEmptyArrayType(),
),
TypeCombinator::intersect(
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetValueType(new ConstantStringType('some-key'), new IntegerType()),
),
TrinaryLogic::createYes(),
];
}

#[DataProvider('dataAccepts')]
Expand Down
Loading