Skip to content

Commit 29ba078

Browse files
committed
fix in IntersectionType
1 parent 30567c9 commit 29ba078

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

src/Type/Accessory/HasOffsetType.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
111111

112112
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
113113
{
114-
if ($acceptingType->isArray()->yes() && $acceptingType->isIterableAtLeastOnce()->yes()) {
115-
return AcceptsResult::createYes();
116-
}
117114
return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
118115
}
119116

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
122122

123123
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
124124
{
125-
if ($acceptingType->isArray()->yes() && $acceptingType->isIterableAtLeastOnce()->yes()) {
126-
return AcceptsResult::createYes();
127-
}
128-
129125
return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
130126
}
131127

src/Type/IntersectionType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ public function accepts(Type $otherType, bool $strictTypes): AcceptsResult
194194
$result = $result->and($type->accepts($otherType, $strictTypes));
195195
}
196196

197+
if (!$result->yes()
198+
&& $this->isArray()->yes()
199+
&& $this->isIterableAtLeastOnce()->yes()
200+
&& $otherType->isArray()->yes()
201+
&& $otherType->isIterableAtLeastOnce()->yes()
202+
&& $this->isSuperTypeOf($otherType)->yes()
203+
) {
204+
return AcceptsResult::createYes();
205+
}
206+
197207
if (!$result->yes()) {
198208
$isList = $otherType->isList();
199209
$reasons = $result->reasons;

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,8 +2074,12 @@ public function testBug12847(): void
20742074
'mixed is empty.',
20752075
],
20762076
[
2077-
"Parameter #1 \$array of function doSomethingWithInt expects non-empty-array<int>, non-empty-array&hasOffsetValue('foo', 'hello') given.",
2078-
73,
2077+
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
2078+
61,
2079+
],
2080+
[
2081+
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
2082+
67,
20792083
],
20802084
]);
20812085
}

tests/PHPStan/Rules/Functions/data/bug-12847.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ function doSomethingWithInt(array $array): void
5858

5959
function doFooBarInt(array $array):void {
6060
if (array_key_exists('foo', $array) && $array['foo'] === 17) {
61-
doSomethingWithInt($array);
61+
doSomethingWithInt($array); // expect error, because our array is not sealed
6262
}
6363
}
6464

6565
function doFooBarString(array $array):void {
6666
if (array_key_exists('foo', $array) && $array['foo'] === "hello") {
67-
doSomethingWithInt($array);
67+
doSomethingWithInt($array); // expect error, because our array is not sealed
6868
}
6969
}

0 commit comments

Comments
 (0)