Skip to content

Inline array_map(fn, $arr) and use foreach-loop instead #3926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/DependencyInjection/Nette/NetteContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\DependencyInjection\ParameterNotFoundException;
use function array_key_exists;
use function array_keys;
use function array_map;

/**
* @internal
Expand Down Expand Up @@ -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;
}

}
9 changes: 6 additions & 3 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
94 changes: 46 additions & 48 deletions src/Reflection/ResolvedFunctionVariantWithOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
96 changes: 54 additions & 42 deletions src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 10 additions & 3 deletions src/Type/IsSuperTypeOfResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
6 changes: 5 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading