Skip to content

Commit e22de35

Browse files
committed
Split ArrayFilterFunctionReturnTypeExtension to Helper
1 parent 9952fff commit e22de35

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,9 @@ services:
12071207
tags:
12081208
- phpstan.broker.dynamicFunctionReturnTypeExtension
12091209

1210+
-
1211+
class: PHPStan\Type\Php\ArrayFilterFunctionReturnTypeHelper
1212+
12101213
-
12111214
class: PHPStan\Type\Php\ArrayFilterFunctionReturnTypeExtension
12121215
tags:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
9+
use PHPStan\Type\Type;
10+
11+
final class ArrayFilterFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
12+
{
13+
14+
public function __construct(private ArrayFilterFunctionReturnTypeHelper $arrayFilterFunctionReturnTypeHelper)
15+
{
16+
}
17+
18+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
19+
{
20+
return $functionReflection->getName() === 'array_filter';
21+
}
22+
23+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
24+
{
25+
$arrayArg = $functionCall->getArgs()[0]->value ?? null;
26+
$callbackArg = $functionCall->getArgs()[1]->value ?? null;
27+
$flagArg = $functionCall->getArgs()[2]->value ?? null;
28+
29+
return $this->arrayFilterFunctionReturnTypeHelper->getType($scope, $arrayArg, $callbackArg, $flagArg);
30+
}
31+
32+
}

src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PhpParser\Node\Stmt\Return_;
1616
use PHPStan\Analyser\MutatingScope;
1717
use PHPStan\Analyser\Scope;
18-
use PHPStan\Reflection\FunctionReflection;
1918
use PHPStan\Reflection\ReflectionProvider;
2019
use PHPStan\ShouldNotHappenException;
2120
use PHPStan\Type\ArrayType;
@@ -24,7 +23,6 @@
2423
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
2524
use PHPStan\Type\Constant\ConstantBooleanType;
2625
use PHPStan\Type\Constant\ConstantIntegerType;
27-
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
2826
use PHPStan\Type\ErrorType;
2927
use PHPStan\Type\MixedType;
3028
use PHPStan\Type\NeverType;
@@ -40,7 +38,7 @@
4038
use function sprintf;
4139
use function substr;
4240

43-
final class ArrayFilterFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
41+
final class ArrayFilterFunctionReturnTypeHelper
4442
{
4543

4644
private const USE_BOTH = 1;
@@ -51,17 +49,8 @@ public function __construct(private ReflectionProvider $reflectionProvider)
5149
{
5250
}
5351

54-
public function isFunctionSupported(FunctionReflection $functionReflection): bool
52+
public function getType(Scope $scope, ?Expr $arrayArg, ?Expr $callbackArg, ?Expr $flagArg): Type
5553
{
56-
return $functionReflection->getName() === 'array_filter';
57-
}
58-
59-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
60-
{
61-
$arrayArg = $functionCall->getArgs()[0]->value ?? null;
62-
$callbackArg = $functionCall->getArgs()[1]->value ?? null;
63-
$flagArg = $functionCall->getArgs()[2]->value ?? null;
64-
6554
if ($arrayArg === null) {
6655
return new ArrayType(new MixedType(), new MixedType());
6756
}
@@ -151,7 +140,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
151140
return new ArrayType($keyType, $itemType);
152141
}
153142

154-
public function removeFalsey(Type $type): Type
143+
private function removeFalsey(Type $type): Type
155144
{
156145
$falseyTypes = StaticTypeFactory::falsey();
157146

0 commit comments

Comments
 (0)