Skip to content

Commit edcc9e8

Browse files
canvuralondrejmirtes
authored andcommitted
fix: check for existence of second arg in CountCharsFunctionDynamicReturnTypeExtension
1 parent 1f411b9 commit edcc9e8

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Diff for: src/Type/Php/CountCharsFunctionDynamicReturnTypeExtension.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Reflection\FunctionReflection;
99
use PHPStan\Type\ArrayType;
1010
use PHPStan\Type\Constant\ConstantBooleanType;
11+
use PHPStan\Type\Constant\ConstantIntegerType;
1112
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1213
use PHPStan\Type\IntegerRangeType;
1314
use PHPStan\Type\IntegerType;
@@ -35,11 +36,13 @@ public function getTypeFromFunctionCall(
3536
Scope $scope,
3637
): ?Type
3738
{
38-
if (count($functionCall->getArgs()) < 1) {
39+
$args = $functionCall->getArgs();
40+
41+
if (count($args) < 1) {
3942
return null;
4043
}
4144

42-
$modeType = $scope->getType($functionCall->getArgs()[1]->value);
45+
$modeType = count($args) === 2 ? $scope->getType($args[1]->value) : new ConstantIntegerType(0);
4346

4447
if (IntegerRangeType::fromInterval(0, 2)->isSuperTypeOf($modeType)->yes()) {
4548
$arrayType = new ArrayType(new IntegerType(), new IntegerType());

Diff for: tests/PHPStan/Analyser/nsrt/count-chars-7.4.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class X {
88
const ABC = 'abcdef';
99

1010
function doFoo(): void {
11+
assertType('array<int, int>|false', count_chars(self::ABC));
1112
assertType('array<int, int>|false', count_chars(self::ABC, 0));
1213
assertType('array<int, int>|false', count_chars(self::ABC, 1));
1314
assertType('array<int, int>|false', count_chars(self::ABC, 2));

Diff for: tests/PHPStan/Analyser/nsrt/count-chars-8.0.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Y {
88
const ABC = 'abcdef';
99

1010
function doFoo(): void {
11+
assertType('array<int, int>', count_chars(self::ABC));
1112
assertType('array<int, int>', count_chars(self::ABC, 0));
1213
assertType('array<int, int>', count_chars(self::ABC, 1));
1314
assertType('array<int, int>', count_chars(self::ABC, 2));

0 commit comments

Comments
 (0)