From 5a6fe9cb0574270fa33171e5410a6fc5529f9b42 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 8 Apr 2025 14:15:38 +0200 Subject: [PATCH] Inline array_map(fn, $arr) and use foreach-loop instead --- .../Nette/NetteContainer.php | 8 +- src/Reflection/ClassReflection.php | 9 +- .../ResolvedFunctionVariantWithOriginal.php | 94 +++++++++--------- ...ackUnresolvedMethodPrototypeReflection.php | 96 +++++++++++-------- src/Type/CallableType.php | 12 ++- src/Type/IntersectionType.php | 7 +- src/Type/IsSuperTypeOfResult.php | 13 ++- src/Type/UnionType.php | 6 +- 8 files changed, 142 insertions(+), 103 deletions(-) diff --git a/src/DependencyInjection/Nette/NetteContainer.php b/src/DependencyInjection/Nette/NetteContainer.php index 914d0a43f1..95bbd50537 100644 --- a/src/DependencyInjection/Nette/NetteContainer.php +++ b/src/DependencyInjection/Nette/NetteContainer.php @@ -6,7 +6,6 @@ use PHPStan\DependencyInjection\ParameterNotFoundException; use function array_key_exists; use function array_keys; -use function array_map; /** * @internal @@ -89,7 +88,12 @@ public function getParameter(string $parameterName) */ private function tagsToServices(array $tags): array { - return array_map(fn (string $serviceName) => $this->getService($serviceName), array_keys($tags)); + $services = []; + foreach (array_keys($tags) as $i => $serviceName) { + $services[$i] = $this->getService($serviceName); + } + + return $services; } } diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 05a9f9b6a6..f0f8dbc199 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -1026,20 +1026,23 @@ public function getTraits(bool $recursive = false): array $traits = $this->getNativeReflection()->getTraits(); } - $traits = array_map(fn (ReflectionClass $trait): ClassReflection => $this->reflectionProvider->getClass($trait->getName()), $traits); + $traitClasses = []; + foreach ($traits as $key => $trait) { + $traitClasses[$key] = $this->reflectionProvider->getClass($trait->getName()); + } if ($recursive) { $parentClass = $this->getNativeReflection()->getParentClass(); if ($parentClass !== false) { return array_merge( - $traits, + $traitClasses, $this->reflectionProvider->getClass($parentClass->getName())->getTraits(true), ); } } - return $traits; + return $traitClasses; } /** diff --git a/src/Reflection/ResolvedFunctionVariantWithOriginal.php b/src/Reflection/ResolvedFunctionVariantWithOriginal.php index d7d2f09acc..6f4a69fa68 100644 --- a/src/Reflection/ResolvedFunctionVariantWithOriginal.php +++ b/src/Reflection/ResolvedFunctionVariantWithOriginal.php @@ -17,7 +17,6 @@ use PHPStan\Type\TypeTraverser; use PHPStan\Type\TypeUtils; use function array_key_exists; -use function array_map; final class ResolvedFunctionVariantWithOriginal implements ResolvedFunctionVariant { @@ -70,61 +69,60 @@ public function getParameters(): array $parameters = $this->parameters; if ($parameters === null) { - $parameters = array_map( - function (ExtendedParameterReflection $param): ExtendedParameterReflection { - $paramType = TypeUtils::resolveLateResolvableTypes( + + $parameters = []; + foreach ($this->parametersAcceptor->getParameters() as $param) { + $paramType = TypeUtils::resolveLateResolvableTypes( + TemplateTypeHelper::resolveTemplateTypes( + $this->resolveConditionalTypesForParameter($param->getType()), + $this->resolvedTemplateTypeMap, + $this->callSiteVarianceMap, + TemplateTypeVariance::createContravariant(), + ), + false, + ); + + $paramOutType = $param->getOutType(); + if ($paramOutType !== null) { + $paramOutType = TypeUtils::resolveLateResolvableTypes( TemplateTypeHelper::resolveTemplateTypes( - $this->resolveConditionalTypesForParameter($param->getType()), + $this->resolveConditionalTypesForParameter($paramOutType), $this->resolvedTemplateTypeMap, $this->callSiteVarianceMap, - TemplateTypeVariance::createContravariant(), + TemplateTypeVariance::createCovariant(), ), false, ); + } - $paramOutType = $param->getOutType(); - if ($paramOutType !== null) { - $paramOutType = TypeUtils::resolveLateResolvableTypes( - TemplateTypeHelper::resolveTemplateTypes( - $this->resolveConditionalTypesForParameter($paramOutType), - $this->resolvedTemplateTypeMap, - $this->callSiteVarianceMap, - TemplateTypeVariance::createCovariant(), - ), - false, - ); - } - - $closureThisType = $param->getClosureThisType(); - if ($closureThisType !== null) { - $closureThisType = TypeUtils::resolveLateResolvableTypes( - TemplateTypeHelper::resolveTemplateTypes( - $this->resolveConditionalTypesForParameter($closureThisType), - $this->resolvedTemplateTypeMap, - $this->callSiteVarianceMap, - TemplateTypeVariance::createCovariant(), - ), - false, - ); - } - - return new ExtendedDummyParameter( - $param->getName(), - $paramType, - $param->isOptional(), - $param->passedByReference(), - $param->isVariadic(), - $param->getDefaultValue(), - $param->getNativeType(), - $param->getPhpDocType(), - $paramOutType, - $param->isImmediatelyInvokedCallable(), - $closureThisType, - $param->getAttributes(), + $closureThisType = $param->getClosureThisType(); + if ($closureThisType !== null) { + $closureThisType = TypeUtils::resolveLateResolvableTypes( + TemplateTypeHelper::resolveTemplateTypes( + $this->resolveConditionalTypesForParameter($closureThisType), + $this->resolvedTemplateTypeMap, + $this->callSiteVarianceMap, + TemplateTypeVariance::createCovariant(), + ), + false, ); - }, - $this->parametersAcceptor->getParameters(), - ); + } + + $parameters[] = new ExtendedDummyParameter( + $param->getName(), + $paramType, + $param->isOptional(), + $param->passedByReference(), + $param->isVariadic(), + $param->getDefaultValue(), + $param->getNativeType(), + $param->getPhpDocType(), + $paramOutType, + $param->isImmediatelyInvokedCallable(), + $closureThisType, + $param->getAttributes(), + ); + } $this->parameters = $parameters; } diff --git a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php index 980a3f293f..7d3d57aa69 100644 --- a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php +++ b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php @@ -6,14 +6,12 @@ use PHPStan\Reflection\Dummy\ChangedTypeMethodReflection; use PHPStan\Reflection\ExtendedFunctionVariant; use PHPStan\Reflection\ExtendedMethodReflection; -use PHPStan\Reflection\ExtendedParameterReflection; use PHPStan\Reflection\ExtendedParametersAcceptor; use PHPStan\Reflection\Php\ExtendedDummyParameter; use PHPStan\Reflection\ResolvedMethodReflection; use PHPStan\Type\ThisType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; -use function array_map; final class CallbackUnresolvedMethodPrototypeReflection implements UnresolvedMethodPrototypeReflection { @@ -85,56 +83,70 @@ public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflectio private function transformMethodWithStaticType(ClassReflection $declaringClass, ExtendedMethodReflection $method): ExtendedMethodReflection { $selfOutType = $method->getSelfOutType() !== null ? $this->transformStaticType($method->getSelfOutType()) : null; - $variantFn = function (ExtendedParametersAcceptor $acceptor) use (&$selfOutType): ExtendedParametersAcceptor { - $originalReturnType = $acceptor->getReturnType(); - if ($originalReturnType instanceof ThisType && $selfOutType !== null) { - $returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType)); - $selfOutType = $returnType; - } else { - $returnType = $this->transformStaticType($originalReturnType); - } - return new ExtendedFunctionVariant( - $acceptor->getTemplateTypeMap(), - $acceptor->getResolvedTemplateTypeMap(), - array_map( - fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter( - $parameter->getName(), - $this->transformStaticType($parameter->getType()), - $parameter->isOptional(), - $parameter->passedByReference(), - $parameter->isVariadic(), - $parameter->getDefaultValue(), - $parameter->getNativeType(), - $this->transformStaticType($parameter->getPhpDocType()), - $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, - $parameter->isImmediatelyInvokedCallable(), - $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, - $parameter->getAttributes(), - ), - $acceptor->getParameters(), - ), - $acceptor->isVariadic(), - $returnType, - $this->transformStaticType($acceptor->getPhpDocReturnType()), - $this->transformStaticType($acceptor->getNativeReturnType()), - $acceptor->getCallSiteVarianceMap(), - ); - }; - $variants = array_map($variantFn, $method->getVariants()); + + $variants = []; + foreach ($method->getVariants() as $variant) { + $variants[] = $this->transformVariant($variant, $selfOutType); + } + + $namedVariants = null; $namedArgumentVariants = $method->getNamedArgumentsVariants(); - $namedArgumentVariants = $namedArgumentVariants !== null - ? array_map($variantFn, $namedArgumentVariants) - : null; + if ($namedArgumentVariants !== null) { + $namedVariants = []; + foreach ($namedArgumentVariants as $namedArgumentVariant) { + $namedVariants[] = $this->transformVariant($namedArgumentVariant, $selfOutType); + } + } return new ChangedTypeMethodReflection( $declaringClass, $method, $variants, - $namedArgumentVariants, + $namedVariants, $selfOutType, ); } + private function transformVariant(ExtendedParametersAcceptor $acceptor, ?Type &$selfOutType): ExtendedParametersAcceptor + { + $originalReturnType = $acceptor->getReturnType(); + if ($originalReturnType instanceof ThisType && $selfOutType !== null) { + $returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType)); + $selfOutType = $returnType; + } else { + $returnType = $this->transformStaticType($originalReturnType); + } + + $parameters = []; + foreach ($acceptor->getParameters() as $parameter) { + $parameters[] = new ExtendedDummyParameter( + $parameter->getName(), + $this->transformStaticType($parameter->getType()), + $parameter->isOptional(), + $parameter->passedByReference(), + $parameter->isVariadic(), + $parameter->getDefaultValue(), + $parameter->getNativeType(), + $this->transformStaticType($parameter->getPhpDocType()), + $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, + $parameter->isImmediatelyInvokedCallable(), + $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, + $parameter->getAttributes(), + ); + } + + return new ExtendedFunctionVariant( + $acceptor->getTemplateTypeMap(), + $acceptor->getResolvedTemplateTypeMap(), + $parameters, + $acceptor->isVariadic(), + $returnType, + $this->transformStaticType($acceptor->getPhpDocReturnType()), + $this->transformStaticType($acceptor->getNativeReturnType()), + $acceptor->getCallSiteVarianceMap(), + ); + } + private function transformStaticType(Type $type): Type { $callback = $this->transformStaticTypeCallback; diff --git a/src/Type/CallableType.php b/src/Type/CallableType.php index 72784cf114..e380fed233 100644 --- a/src/Type/CallableType.php +++ b/src/Type/CallableType.php @@ -224,15 +224,21 @@ public function describe(VerbosityLevel $level): string static fn (): string => 'callable', function (): string { $printer = new Printer(); - $selfWithoutParameterNames = new self( - array_map(static fn (ParameterReflection $p): ParameterReflection => new DummyParameter( + + $parameters = []; + foreach ($this->parameters as $p) { + $parameters[] = new DummyParameter( '', $p->getType(), $p->isOptional() && !$p->isVariadic(), PassedByReference::createNo(), $p->isVariadic(), $p->getDefaultValue(), - ), $this->parameters), + ); + } + + $selfWithoutParameterNames = new self( + $parameters, $this->returnType, $this->variadic, $this->templateTypeMap, diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index 149536a573..4e85465a8a 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -242,7 +242,12 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult return $otherType->isSuperTypeOf($this); } - $result = IsSuperTypeOfResult::maxMin(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types)); + $isSuperTypeOf = []; + foreach ($this->types as $innerType) { + $isSuperTypeOf[] = $otherType->isSuperTypeOf($innerType); + } + + $result = IsSuperTypeOfResult::maxMin(...$isSuperTypeOf); if ($this->isOversizedArray()->yes()) { if (!$result->no()) { return IsSuperTypeOfResult::createYes(); diff --git a/src/Type/IsSuperTypeOfResult.php b/src/Type/IsSuperTypeOfResult.php index c1decae155..30db87287f 100644 --- a/src/Type/IsSuperTypeOfResult.php +++ b/src/Type/IsSuperTypeOfResult.php @@ -4,7 +4,6 @@ use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; -use function array_map; use function array_merge; use function array_unique; use function array_values; @@ -118,8 +117,12 @@ public static function extremeIdentity(self ...$operands): self throw new ShouldNotHappenException(); } - $result = TrinaryLogic::extremeIdentity(...array_map(static fn (self $result) => $result->result, $operands)); + $results = []; + foreach ($operands as $operand) { + $results[] = $operand->result; + } + $result = TrinaryLogic::extremeIdentity(...$results); return new self($result, self::mergeReasons($operands)); } @@ -129,8 +132,12 @@ public static function maxMin(self ...$operands): self throw new ShouldNotHappenException(); } - $result = TrinaryLogic::maxMin(...array_map(static fn (self $result) => $result->result, $operands)); + $results = []; + foreach ($operands as $operand) { + $results[] = $operand->result; + } + $result = TrinaryLogic::maxMin(...$results); return new self($result, self::mergeReasons($operands)); } diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 08d678152a..ecffc7f403 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -263,7 +263,11 @@ public function isSuperTypeOf(Type $otherType): IsSuperTypeOfResult public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult { - return IsSuperTypeOfResult::extremeIdentity(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types)); + $isSuperType = []; + foreach ($this->types as $innerType) { + $isSuperType[] = $otherType->isSuperTypeOf($innerType); + } + return IsSuperTypeOfResult::extremeIdentity(...$isSuperType); } public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult