Skip to content

Commit 175307e

Browse files
committed
Introduce CombinationsHelper
1 parent 65ff783 commit 175307e

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/Internal/CombinationsHelper.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use function array_shift;
6+
7+
final class CombinationsHelper
8+
{
9+
10+
/**
11+
* @param array<mixed> $arrays
12+
* @return iterable<mixed>
13+
*/
14+
public static function combinations(array $arrays): iterable
15+
{
16+
// from https://stackoverflow.com/a/70800936/565782 by Arnaud Le Blanc
17+
if ($arrays === []) {
18+
yield [];
19+
return;
20+
}
21+
22+
$head = array_shift($arrays);
23+
24+
foreach ($head as $elem) {
25+
foreach (self::combinations($arrays) as $combination) {
26+
$comb = [$elem];
27+
foreach ($combination as $c) {
28+
$comb[] = $c;
29+
}
30+
yield $comb;
31+
}
32+
}
33+
}
34+
35+
}

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

+2-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Internal\CombinationsHelper;
78
use PHPStan\Reflection\FunctionReflection;
89
use PHPStan\Reflection\InitializerExprTypeResolver;
910
use PHPStan\Reflection\ParametersAcceptorSelector;
@@ -94,7 +95,7 @@ public function getTypeFromFunctionCall(
9495
$values[] = $argType->getConstantScalarValues();
9596
}
9697

97-
$combinations = $this->combinations($values);
98+
$combinations = CombinationsHelper::combinations($values);
9899
$returnTypes = [];
99100
foreach ($combinations as $combination) {
100101
$format = array_shift($combination);
@@ -120,29 +121,4 @@ public function getTypeFromFunctionCall(
120121
return TypeCombinator::union(...$returnTypes);
121122
}
122123

123-
/**
124-
* @param array<mixed> $arrays
125-
* @return iterable<mixed>
126-
*/
127-
private function combinations(array $arrays): iterable
128-
{
129-
// from https://stackoverflow.com/a/70800936/565782 by Arnaud Le Blanc
130-
if ($arrays === []) {
131-
yield [];
132-
return;
133-
}
134-
135-
$head = array_shift($arrays);
136-
137-
foreach ($head as $elem) {
138-
foreach ($this->combinations($arrays) as $combination) {
139-
$comb = [$elem];
140-
foreach ($combination as $c) {
141-
$comb[] = $c;
142-
}
143-
yield $comb;
144-
}
145-
}
146-
}
147-
148124
}

0 commit comments

Comments
 (0)