Skip to content

Commit 4eeb352

Browse files
authored
Merge branch refs/heads/1.10.x into 1.11.x
2 parents d1834e6 + 4a4c739 commit 4eeb352

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

src/Type/BenevolentUnionType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ protected function unionTypes(callable $getType): Type
4343
return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$resultTypes));
4444
}
4545

46-
protected function pickFromTypes(callable $getValues): array
46+
protected function pickFromTypes(
47+
callable $getValues,
48+
callable $criteria,
49+
): array
4750
{
4851
$values = [];
4952
foreach ($this->getTypes() as $type) {
5053
$innerValues = $getValues($type);
54+
if ($innerValues === [] && $criteria($type)) {
55+
return [];
56+
}
57+
5158
foreach ($innerValues as $innerType) {
5259
$values[] = $innerType;
5360
}

src/Type/UnionType.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,42 @@ public function getReferencedClasses(): array
123123

124124
public function getObjectClassNames(): array
125125
{
126-
return array_values(array_unique($this->pickFromTypes(static fn (Type $type) => $type->getObjectClassNames())));
126+
return array_values(array_unique($this->pickFromTypes(
127+
static fn (Type $type) => $type->getObjectClassNames(),
128+
static fn (Type $type) => $type->isObject()->yes(),
129+
)));
127130
}
128131

129132
public function getObjectClassReflections(): array
130133
{
131-
return $this->pickFromTypes(static fn (Type $type) => $type->getObjectClassReflections());
134+
return $this->pickFromTypes(
135+
static fn (Type $type) => $type->getObjectClassReflections(),
136+
static fn (Type $type) => $type->isObject()->yes(),
137+
);
132138
}
133139

134140
public function getArrays(): array
135141
{
136-
return $this->pickFromTypes(static fn (Type $type) => $type->getArrays());
142+
return $this->pickFromTypes(
143+
static fn (Type $type) => $type->getArrays(),
144+
static fn (Type $type) => $type->isArray()->yes(),
145+
);
137146
}
138147

139148
public function getConstantArrays(): array
140149
{
141-
return $this->pickFromTypes(static fn (Type $type) => $type->getConstantArrays());
150+
return $this->pickFromTypes(
151+
static fn (Type $type) => $type->getConstantArrays(),
152+
static fn (Type $type) => $type->isArray()->yes(),
153+
);
142154
}
143155

144156
public function getConstantStrings(): array
145157
{
146-
return $this->pickFromTypes(static fn (Type $type) => $type->getConstantStrings());
158+
return $this->pickFromTypes(
159+
static fn (Type $type) => $type->getConstantStrings(),
160+
static fn (Type $type) => $type->isString()->yes(),
161+
);
147162
}
148163

149164
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
@@ -718,7 +733,10 @@ public function shuffleArray(): Type
718733

719734
public function getEnumCases(): array
720735
{
721-
return $this->pickFromTypes(static fn (Type $type) => $type->getEnumCases());
736+
return $this->pickFromTypes(
737+
static fn (Type $type) => $type->getEnumCases(),
738+
static fn (Type $type) => $type->isObject()->yes(),
739+
);
722740
}
723741

724742
public function isCallable(): TrinaryLogic
@@ -1069,15 +1087,19 @@ protected function unionTypes(callable $getType): Type
10691087
*/
10701088
protected function pickTypes(callable $getTypes): array
10711089
{
1072-
return $this->pickFromTypes($getTypes);
1090+
return $this->pickFromTypes($getTypes, static fn () => false);
10731091
}
10741092

10751093
/**
10761094
* @template T
10771095
* @param callable(Type $type): list<T> $getValues
1096+
* @param callable(Type $type): bool $criteria
10781097
* @return list<T>
10791098
*/
1080-
protected function pickFromTypes(callable $getValues): array
1099+
protected function pickFromTypes(
1100+
callable $getValues,
1101+
callable $criteria,
1102+
): array
10811103
{
10821104
$values = [];
10831105
foreach ($this->types as $type) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ public function dataFileAsserts(): iterable
771771
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6404.php');
772772
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6399.php');
773773
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4357.php');
774+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10863.php');
774775
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5817.php');
775776

776777
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-chunk.php');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug10863;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param __benevolent<int|false> $b
12+
*/
13+
public function doFoo($b): void
14+
{
15+
assertType('non-falsy-string', '@' . $b);
16+
}
17+
18+
/**
19+
* @param int|false $b
20+
*/
21+
public function doFoo2($b): void
22+
{
23+
assertType('non-falsy-string', '@' . $b);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)