Skip to content

Commit f249159

Browse files
herndlmondrejmirtes
authored andcommitted
Use TypeUtils::getOldConstantArrays in array pointer functions extension
1 parent 5e54dbd commit f249159

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/Type/Php/ArrayPointerFunctionsDynamicReturnTypeExtension.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,18 @@ public function getTypeFromFunctionCall(
4444
return new ConstantBooleanType(false);
4545
}
4646

47-
$constantArrays = TypeUtils::getConstantArrays($argType);
47+
$constantArrays = TypeUtils::getOldConstantArrays($argType);
4848
if (count($constantArrays) > 0) {
4949
$keyTypes = [];
5050
foreach ($constantArrays as $constantArray) {
51-
$arrayKeyTypes = $constantArray->getKeyTypes();
52-
if (count($arrayKeyTypes) === 0) {
51+
if ($constantArray->isEmpty()) {
5352
$keyTypes[] = new ConstantBooleanType(false);
5453
continue;
5554
}
5655

57-
$valueOffset = $functionReflection->getName() === 'reset'
58-
? $arrayKeyTypes[0]
59-
: $arrayKeyTypes[count($arrayKeyTypes) - 1];
60-
61-
$keyTypes[] = $constantArray->getOffsetValueType($valueOffset);
56+
$keyTypes[] = $functionReflection->getName() === 'reset'
57+
? $constantArray->getFirstValueType()
58+
: $constantArray->getLastValueType();
6259
}
6360

6461
return TypeCombinator::union(...$keyTypes);

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7298,6 +7298,18 @@ public function dataArrayPointerFunctions(): array
72987298
'\'baz\'|\'foo\'',
72997299
'reset($conditionalArray)',
73007300
],
7301+
[
7302+
'0|1',
7303+
'reset($constantArrayOptionalKeys1)',
7304+
],
7305+
[
7306+
'0',
7307+
'reset($constantArrayOptionalKeys2)',
7308+
],
7309+
[
7310+
'0',
7311+
'reset($constantArrayOptionalKeys3)',
7312+
],
73017313
[
73027314
'mixed',
73037315
'end()',
@@ -7322,6 +7334,18 @@ public function dataArrayPointerFunctions(): array
73227334
'\'bar\'|\'baz\'',
73237335
'end($secondConditionalArray)',
73247336
],
7337+
[
7338+
'2',
7339+
'end($constantArrayOptionalKeys1)',
7340+
],
7341+
[
7342+
'2',
7343+
'end($constantArrayOptionalKeys2)',
7344+
],
7345+
[
7346+
'1|2',
7347+
'end($constantArrayOptionalKeys3)',
7348+
],
73257349
];
73267350
}
73277351

tests/PHPStan/Analyser/data/array-pointer-functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ResetDynamicReturnTypeExtension;
44

5+
use function PHPStan\Testing\assertType;
6+
57
class Foo
68
{
79

@@ -16,6 +18,12 @@ public function doFoo(array $generalArray, $somethingElse)
1618
'a' => 1,
1719
'b' => 2,
1820
];
21+
/** @var array{a?: 0, b: 1, c: 2} $constantArrayOptionalKeys1 */
22+
$constantArrayOptionalKeys1 = [];
23+
/** @var array{a: 0, b?: 1, c: 2} $constantArrayOptionalKeys2 */
24+
$constantArrayOptionalKeys2 = [];
25+
/** @var array{a: 0, b: 1, c?: 2} $constantArrayOptionalKeys3 */
26+
$constantArrayOptionalKeys3 = [];
1927

2028
$conditionalArray = ['foo', 'bar'];
2129
if (doFoo()) {

0 commit comments

Comments
 (0)