Skip to content

Commit 5a6fe9c

Browse files
committed
Inline array_map(fn, $arr) and use foreach-loop instead
1 parent 9a0bfd2 commit 5a6fe9c

File tree

8 files changed

+142
-103
lines changed

8 files changed

+142
-103
lines changed

src/DependencyInjection/Nette/NetteContainer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\DependencyInjection\ParameterNotFoundException;
77
use function array_key_exists;
88
use function array_keys;
9-
use function array_map;
109

1110
/**
1211
* @internal
@@ -89,7 +88,12 @@ public function getParameter(string $parameterName)
8988
*/
9089
private function tagsToServices(array $tags): array
9190
{
92-
return array_map(fn (string $serviceName) => $this->getService($serviceName), array_keys($tags));
91+
$services = [];
92+
foreach (array_keys($tags) as $i => $serviceName) {
93+
$services[$i] = $this->getService($serviceName);
94+
}
95+
96+
return $services;
9397
}
9498

9599
}

src/Reflection/ClassReflection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,20 +1026,23 @@ public function getTraits(bool $recursive = false): array
10261026
$traits = $this->getNativeReflection()->getTraits();
10271027
}
10281028

1029-
$traits = array_map(fn (ReflectionClass $trait): ClassReflection => $this->reflectionProvider->getClass($trait->getName()), $traits);
1029+
$traitClasses = [];
1030+
foreach ($traits as $key => $trait) {
1031+
$traitClasses[$key] = $this->reflectionProvider->getClass($trait->getName());
1032+
}
10301033

10311034
if ($recursive) {
10321035
$parentClass = $this->getNativeReflection()->getParentClass();
10331036

10341037
if ($parentClass !== false) {
10351038
return array_merge(
1036-
$traits,
1039+
$traitClasses,
10371040
$this->reflectionProvider->getClass($parentClass->getName())->getTraits(true),
10381041
);
10391042
}
10401043
}
10411044

1042-
return $traits;
1045+
return $traitClasses;
10431046
}
10441047

10451048
/**

src/Reflection/ResolvedFunctionVariantWithOriginal.php

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use PHPStan\Type\TypeTraverser;
1818
use PHPStan\Type\TypeUtils;
1919
use function array_key_exists;
20-
use function array_map;
2120

2221
final class ResolvedFunctionVariantWithOriginal implements ResolvedFunctionVariant
2322
{
@@ -70,61 +69,60 @@ public function getParameters(): array
7069
$parameters = $this->parameters;
7170

7271
if ($parameters === null) {
73-
$parameters = array_map(
74-
function (ExtendedParameterReflection $param): ExtendedParameterReflection {
75-
$paramType = TypeUtils::resolveLateResolvableTypes(
72+
73+
$parameters = [];
74+
foreach ($this->parametersAcceptor->getParameters() as $param) {
75+
$paramType = TypeUtils::resolveLateResolvableTypes(
76+
TemplateTypeHelper::resolveTemplateTypes(
77+
$this->resolveConditionalTypesForParameter($param->getType()),
78+
$this->resolvedTemplateTypeMap,
79+
$this->callSiteVarianceMap,
80+
TemplateTypeVariance::createContravariant(),
81+
),
82+
false,
83+
);
84+
85+
$paramOutType = $param->getOutType();
86+
if ($paramOutType !== null) {
87+
$paramOutType = TypeUtils::resolveLateResolvableTypes(
7688
TemplateTypeHelper::resolveTemplateTypes(
77-
$this->resolveConditionalTypesForParameter($param->getType()),
89+
$this->resolveConditionalTypesForParameter($paramOutType),
7890
$this->resolvedTemplateTypeMap,
7991
$this->callSiteVarianceMap,
80-
TemplateTypeVariance::createContravariant(),
92+
TemplateTypeVariance::createCovariant(),
8193
),
8294
false,
8395
);
96+
}
8497

85-
$paramOutType = $param->getOutType();
86-
if ($paramOutType !== null) {
87-
$paramOutType = TypeUtils::resolveLateResolvableTypes(
88-
TemplateTypeHelper::resolveTemplateTypes(
89-
$this->resolveConditionalTypesForParameter($paramOutType),
90-
$this->resolvedTemplateTypeMap,
91-
$this->callSiteVarianceMap,
92-
TemplateTypeVariance::createCovariant(),
93-
),
94-
false,
95-
);
96-
}
97-
98-
$closureThisType = $param->getClosureThisType();
99-
if ($closureThisType !== null) {
100-
$closureThisType = TypeUtils::resolveLateResolvableTypes(
101-
TemplateTypeHelper::resolveTemplateTypes(
102-
$this->resolveConditionalTypesForParameter($closureThisType),
103-
$this->resolvedTemplateTypeMap,
104-
$this->callSiteVarianceMap,
105-
TemplateTypeVariance::createCovariant(),
106-
),
107-
false,
108-
);
109-
}
110-
111-
return new ExtendedDummyParameter(
112-
$param->getName(),
113-
$paramType,
114-
$param->isOptional(),
115-
$param->passedByReference(),
116-
$param->isVariadic(),
117-
$param->getDefaultValue(),
118-
$param->getNativeType(),
119-
$param->getPhpDocType(),
120-
$paramOutType,
121-
$param->isImmediatelyInvokedCallable(),
122-
$closureThisType,
123-
$param->getAttributes(),
98+
$closureThisType = $param->getClosureThisType();
99+
if ($closureThisType !== null) {
100+
$closureThisType = TypeUtils::resolveLateResolvableTypes(
101+
TemplateTypeHelper::resolveTemplateTypes(
102+
$this->resolveConditionalTypesForParameter($closureThisType),
103+
$this->resolvedTemplateTypeMap,
104+
$this->callSiteVarianceMap,
105+
TemplateTypeVariance::createCovariant(),
106+
),
107+
false,
124108
);
125-
},
126-
$this->parametersAcceptor->getParameters(),
127-
);
109+
}
110+
111+
$parameters[] = new ExtendedDummyParameter(
112+
$param->getName(),
113+
$paramType,
114+
$param->isOptional(),
115+
$param->passedByReference(),
116+
$param->isVariadic(),
117+
$param->getDefaultValue(),
118+
$param->getNativeType(),
119+
$param->getPhpDocType(),
120+
$paramOutType,
121+
$param->isImmediatelyInvokedCallable(),
122+
$closureThisType,
123+
$param->getAttributes(),
124+
);
125+
}
128126

129127
$this->parameters = $parameters;
130128
}

src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
use PHPStan\Reflection\Dummy\ChangedTypeMethodReflection;
77
use PHPStan\Reflection\ExtendedFunctionVariant;
88
use PHPStan\Reflection\ExtendedMethodReflection;
9-
use PHPStan\Reflection\ExtendedParameterReflection;
109
use PHPStan\Reflection\ExtendedParametersAcceptor;
1110
use PHPStan\Reflection\Php\ExtendedDummyParameter;
1211
use PHPStan\Reflection\ResolvedMethodReflection;
1312
use PHPStan\Type\ThisType;
1413
use PHPStan\Type\Type;
1514
use PHPStan\Type\TypeCombinator;
16-
use function array_map;
1715

1816
final class CallbackUnresolvedMethodPrototypeReflection implements UnresolvedMethodPrototypeReflection
1917
{
@@ -85,56 +83,70 @@ public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflectio
8583
private function transformMethodWithStaticType(ClassReflection $declaringClass, ExtendedMethodReflection $method): ExtendedMethodReflection
8684
{
8785
$selfOutType = $method->getSelfOutType() !== null ? $this->transformStaticType($method->getSelfOutType()) : null;
88-
$variantFn = function (ExtendedParametersAcceptor $acceptor) use (&$selfOutType): ExtendedParametersAcceptor {
89-
$originalReturnType = $acceptor->getReturnType();
90-
if ($originalReturnType instanceof ThisType && $selfOutType !== null) {
91-
$returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType));
92-
$selfOutType = $returnType;
93-
} else {
94-
$returnType = $this->transformStaticType($originalReturnType);
95-
}
96-
return new ExtendedFunctionVariant(
97-
$acceptor->getTemplateTypeMap(),
98-
$acceptor->getResolvedTemplateTypeMap(),
99-
array_map(
100-
fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter(
101-
$parameter->getName(),
102-
$this->transformStaticType($parameter->getType()),
103-
$parameter->isOptional(),
104-
$parameter->passedByReference(),
105-
$parameter->isVariadic(),
106-
$parameter->getDefaultValue(),
107-
$parameter->getNativeType(),
108-
$this->transformStaticType($parameter->getPhpDocType()),
109-
$parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null,
110-
$parameter->isImmediatelyInvokedCallable(),
111-
$parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null,
112-
$parameter->getAttributes(),
113-
),
114-
$acceptor->getParameters(),
115-
),
116-
$acceptor->isVariadic(),
117-
$returnType,
118-
$this->transformStaticType($acceptor->getPhpDocReturnType()),
119-
$this->transformStaticType($acceptor->getNativeReturnType()),
120-
$acceptor->getCallSiteVarianceMap(),
121-
);
122-
};
123-
$variants = array_map($variantFn, $method->getVariants());
86+
87+
$variants = [];
88+
foreach ($method->getVariants() as $variant) {
89+
$variants[] = $this->transformVariant($variant, $selfOutType);
90+
}
91+
92+
$namedVariants = null;
12493
$namedArgumentVariants = $method->getNamedArgumentsVariants();
125-
$namedArgumentVariants = $namedArgumentVariants !== null
126-
? array_map($variantFn, $namedArgumentVariants)
127-
: null;
94+
if ($namedArgumentVariants !== null) {
95+
$namedVariants = [];
96+
foreach ($namedArgumentVariants as $namedArgumentVariant) {
97+
$namedVariants[] = $this->transformVariant($namedArgumentVariant, $selfOutType);
98+
}
99+
}
128100

129101
return new ChangedTypeMethodReflection(
130102
$declaringClass,
131103
$method,
132104
$variants,
133-
$namedArgumentVariants,
105+
$namedVariants,
134106
$selfOutType,
135107
);
136108
}
137109

110+
private function transformVariant(ExtendedParametersAcceptor $acceptor, ?Type &$selfOutType): ExtendedParametersAcceptor
111+
{
112+
$originalReturnType = $acceptor->getReturnType();
113+
if ($originalReturnType instanceof ThisType && $selfOutType !== null) {
114+
$returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType));
115+
$selfOutType = $returnType;
116+
} else {
117+
$returnType = $this->transformStaticType($originalReturnType);
118+
}
119+
120+
$parameters = [];
121+
foreach ($acceptor->getParameters() as $parameter) {
122+
$parameters[] = new ExtendedDummyParameter(
123+
$parameter->getName(),
124+
$this->transformStaticType($parameter->getType()),
125+
$parameter->isOptional(),
126+
$parameter->passedByReference(),
127+
$parameter->isVariadic(),
128+
$parameter->getDefaultValue(),
129+
$parameter->getNativeType(),
130+
$this->transformStaticType($parameter->getPhpDocType()),
131+
$parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null,
132+
$parameter->isImmediatelyInvokedCallable(),
133+
$parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null,
134+
$parameter->getAttributes(),
135+
);
136+
}
137+
138+
return new ExtendedFunctionVariant(
139+
$acceptor->getTemplateTypeMap(),
140+
$acceptor->getResolvedTemplateTypeMap(),
141+
$parameters,
142+
$acceptor->isVariadic(),
143+
$returnType,
144+
$this->transformStaticType($acceptor->getPhpDocReturnType()),
145+
$this->transformStaticType($acceptor->getNativeReturnType()),
146+
$acceptor->getCallSiteVarianceMap(),
147+
);
148+
}
149+
138150
private function transformStaticType(Type $type): Type
139151
{
140152
$callback = $this->transformStaticTypeCallback;

src/Type/CallableType.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,21 @@ public function describe(VerbosityLevel $level): string
224224
static fn (): string => 'callable',
225225
function (): string {
226226
$printer = new Printer();
227-
$selfWithoutParameterNames = new self(
228-
array_map(static fn (ParameterReflection $p): ParameterReflection => new DummyParameter(
227+
228+
$parameters = [];
229+
foreach ($this->parameters as $p) {
230+
$parameters[] = new DummyParameter(
229231
'',
230232
$p->getType(),
231233
$p->isOptional() && !$p->isVariadic(),
232234
PassedByReference::createNo(),
233235
$p->isVariadic(),
234236
$p->getDefaultValue(),
235-
), $this->parameters),
237+
);
238+
}
239+
240+
$selfWithoutParameterNames = new self(
241+
$parameters,
236242
$this->returnType,
237243
$this->variadic,
238244
$this->templateTypeMap,

src/Type/IntersectionType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
242242
return $otherType->isSuperTypeOf($this);
243243
}
244244

245-
$result = IsSuperTypeOfResult::maxMin(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types));
245+
$isSuperTypeOf = [];
246+
foreach ($this->types as $innerType) {
247+
$isSuperTypeOf[] = $otherType->isSuperTypeOf($innerType);
248+
}
249+
250+
$result = IsSuperTypeOfResult::maxMin(...$isSuperTypeOf);
246251
if ($this->isOversizedArray()->yes()) {
247252
if (!$result->no()) {
248253
return IsSuperTypeOfResult::createYes();

src/Type/IsSuperTypeOfResult.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\ShouldNotHappenException;
66
use PHPStan\TrinaryLogic;
7-
use function array_map;
87
use function array_merge;
98
use function array_unique;
109
use function array_values;
@@ -118,8 +117,12 @@ public static function extremeIdentity(self ...$operands): self
118117
throw new ShouldNotHappenException();
119118
}
120119

121-
$result = TrinaryLogic::extremeIdentity(...array_map(static fn (self $result) => $result->result, $operands));
120+
$results = [];
121+
foreach ($operands as $operand) {
122+
$results[] = $operand->result;
123+
}
122124

125+
$result = TrinaryLogic::extremeIdentity(...$results);
123126
return new self($result, self::mergeReasons($operands));
124127
}
125128

@@ -129,8 +132,12 @@ public static function maxMin(self ...$operands): self
129132
throw new ShouldNotHappenException();
130133
}
131134

132-
$result = TrinaryLogic::maxMin(...array_map(static fn (self $result) => $result->result, $operands));
135+
$results = [];
136+
foreach ($operands as $operand) {
137+
$results[] = $operand->result;
138+
}
133139

140+
$result = TrinaryLogic::maxMin(...$results);
134141
return new self($result, self::mergeReasons($operands));
135142
}
136143

src/Type/UnionType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ public function isSuperTypeOf(Type $otherType): IsSuperTypeOfResult
263263

264264
public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
265265
{
266-
return IsSuperTypeOfResult::extremeIdentity(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types));
266+
$isSuperType = [];
267+
foreach ($this->types as $innerType) {
268+
$isSuperType[] = $otherType->isSuperTypeOf($innerType);
269+
}
270+
return IsSuperTypeOfResult::extremeIdentity(...$isSuperType);
267271
}
268272

269273
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult

0 commit comments

Comments
 (0)