Skip to content

Commit

Permalink
Bleeding edge - enforce @no-named-arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 5, 2024
1 parent f3810d9 commit 74ba8c2
Show file tree
Hide file tree
Showing 50 changed files with 392 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/Analyser/ArgumentsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class ArgumentsNormalizer
public const ORIGINAL_ARG_ATTRIBUTE = 'originalArg';

/**
* @return array{ParametersAcceptor, FuncCall}|null
* @return array{ParametersAcceptor, FuncCall, bool}|null
*/
public static function reorderCallUserFuncArguments(
FuncCall $callUserFuncCall,
Expand Down Expand Up @@ -65,18 +65,24 @@ public static function reorderCallUserFuncArguments(
return null;
}

$callableParametersAcceptors = $calledOnType->getCallableParametersAcceptors($scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$passThruArgs,
$calledOnType->getCallableParametersAcceptors($scope),
$callableParametersAcceptors,
null,
);

$acceptsNamedArguments = true;
foreach ($callableParametersAcceptors as $callableParametersAcceptor) {
$acceptsNamedArguments = $acceptsNamedArguments && $callableParametersAcceptor->acceptsNamedArguments();
}

return [$parametersAcceptor, new FuncCall(
$callbackArg->value,
$passThruArgs,
$callUserFuncCall->getAttributes(),
)];
), $acceptsNamedArguments];
}

public static function reorderFuncArguments(
Expand Down
7 changes: 7 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$cachedClosureData['impurePoints'],
$cachedClosureData['invalidateExpressions'],
$cachedClosureData['usedVariables'],
true,
);
}
if (self::$resolveClosureTypeDepth >= 2) {
Expand Down Expand Up @@ -1575,6 +1576,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$impurePointsForClosureType,
$invalidateExpressions,
$usedVariables,
true,
);
} elseif ($node instanceof New_) {
if ($node->class instanceof Name) {
Expand Down Expand Up @@ -2523,9 +2525,11 @@ private function createFirstClassCallable(

$throwPoints = [];
$impurePoints = [];
$acceptsNamedArguments = true;
if ($variant instanceof CallableParametersAcceptor) {
$throwPoints = $variant->getThrowPoints();
$impurePoints = $variant->getImpurePoints();
$acceptsNamedArguments = $variant->acceptsNamedArguments();
} elseif ($function !== null) {
$returnTypeForThrow = $variant->getReturnType();
$throwType = $function->getThrowType();
Expand All @@ -2549,6 +2553,8 @@ private function createFirstClassCallable(
if ($impurePoint !== null) {
$impurePoints[] = $impurePoint;
}

$acceptsNamedArguments = $function->acceptsNamedArguments();
}

$parameters = $variant->getParameters();
Expand All @@ -2564,6 +2570,7 @@ private function createFirstClassCallable(
$impurePoints,
[],
[],
$acceptsNamedArguments,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ function (CallableTypeParameterNode $parameterNode) use ($nameScope, &$isVariadi
),
]);
} elseif ($mainType instanceof ClosureType) {
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, null, null, $templateTags, [], $mainType->getImpurePoints());
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, null, null, $templateTags, [], $mainType->getImpurePoints(), $mainType->getInvalidateExpressions(), $mainType->getUsedVariables(), $mainType->acceptsNamedArguments());
if ($closure->isPure()->yes() && $returnType->isVoid()->yes()) {
return new ErrorType();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function acceptsNamedArguments(): bool
{
return $this->declaringClass->acceptsNamedArguments();
}

public function getSelfOutType(): ?Type
{
return null;
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ private function getCustomFunction(string $functionName): PhpFunctionReflection
$isFinal = false;
$isPure = null;
$asserts = Assertions::createEmpty();
$acceptsNamedArguments = true;
$phpDocComment = null;
$phpDocParameterOutTags = [];
$phpDocParameterImmediatelyInvokedCallable = [];
Expand All @@ -305,6 +306,7 @@ private function getCustomFunction(string $functionName): PhpFunctionReflection
if ($resolvedPhpDoc->hasPhpDocString()) {
$phpDocComment = $resolvedPhpDoc->getPhpDocString();
}
$acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments();
$phpDocParameterOutTags = $resolvedPhpDoc->getParamOutTags();
$phpDocParameterImmediatelyInvokedCallable = $resolvedPhpDoc->getParamsImmediatelyInvokedCallable();
$phpDocParameterClosureThisTypeTags = $resolvedPhpDoc->getParamClosureThisTags();
Expand All @@ -323,6 +325,7 @@ private function getCustomFunction(string $functionName): PhpFunctionReflection
$reflectionFunction->getFileName() !== false ? $reflectionFunction->getFileName() : null,
$isPure,
$asserts,
$acceptsNamedArguments,
$phpDocComment,
array_map(static fn (ParamOutTag $paramOutTag): Type => $paramOutTag->getType(), $phpDocParameterOutTags),
$phpDocParameterImmediatelyInvokedCallable,
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/CallableFunctionVariantWithPhpDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
private array $impurePoints,
private array $invalidateExpressions,
private array $usedVariables,
private bool $acceptsNamedArguments,
)
{
parent::__construct(
Expand Down Expand Up @@ -74,4 +75,9 @@ public function getUsedVariables(): array
return $this->usedVariables;
}

public function acceptsNamedArguments(): bool
{
return $this->acceptsNamedArguments;
}

}
2 changes: 2 additions & 0 deletions src/Reflection/Callables/CallableParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getThrowPoints(): array;

public function isPure(): TrinaryLogic;

public function acceptsNamedArguments(): bool;

/**
* @return SimpleImpurePoint[]
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Callables/FunctionCallableVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ public function getUsedVariables(): array
return [];
}

public function acceptsNamedArguments(): bool
{
return $this->function->acceptsNamedArguments();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public function getAsserts(): Assertions
return $this->reflection->getAsserts();
}

public function acceptsNamedArguments(): bool
{
return $this->reflection->acceptsNamedArguments();
}

public function getSelfOutType(): ?Type
{
return $this->reflection->getSelfOutType();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function acceptsNamedArguments(): bool
{
return $this->declaringClass->acceptsNamedArguments();
}

public function getSelfOutType(): ?Type
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function acceptsNamedArguments(): bool
{
return true;
}

public function getSelfOutType(): ?Type
{
return null;
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/ExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function getVariants(): array;
*/
public function getNamedArgumentsVariants(): ?array;

public function acceptsNamedArguments(): bool;

public function getAsserts(): Assertions;

public function getSelfOutType(): ?Type;
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function getVariants(): array;
*/
public function getNamedArgumentsVariants(): ?array;

public function acceptsNamedArguments(): bool;

public function isDeprecated(): TrinaryLogic;

public function getDeprecatedDescription(): ?string;
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/FunctionReflectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function create(
?string $filename,
?bool $isPure,
Assertions $asserts,
bool $acceptsNamedArguments,
?string $phpDocComment,
array $phpDocParameterOutTypes,
array $phpDocParameterImmediatelyInvokedCallable,
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/GenericParametersAcceptorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
$originalParametersAcceptor->getImpurePoints(),
$originalParametersAcceptor->getInvalidateExpressions(),
$originalParametersAcceptor->getUsedVariables(),
$originalParametersAcceptor->acceptsNamedArguments(),
);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Reflection/InaccessibleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
final class InaccessibleMethod implements CallableParametersAcceptor
{

public function __construct(private MethodReflection $methodReflection)
public function __construct(private ExtendedMethodReflection $methodReflection)
{
}

public function getMethod(): MethodReflection
public function getMethod(): ExtendedMethodReflection
{
return $this->methodReflection;
}
Expand Down Expand Up @@ -86,4 +86,9 @@ public function getUsedVariables(): array
return [];
}

public function acceptsNamedArguments(): bool
{
return $this->methodReflection->acceptsNamedArguments();
}

}
6 changes: 6 additions & 0 deletions src/Reflection/Native/NativeFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(
?Assertions $assertions = null,
private ?string $phpDocComment = null,
?TrinaryLogic $returnsByReference = null,
private bool $acceptsNamedArguments = true,
)
{
$this->assertions = $assertions ?? Assertions::createEmpty();
Expand Down Expand Up @@ -132,4 +133,9 @@ public function returnsByReference(): TrinaryLogic
return $this->returnsByReference;
}

public function acceptsNamedArguments(): bool
{
return $this->acceptsNamedArguments;
}

}
6 changes: 6 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
private TrinaryLogic $hasSideEffects,
private ?Type $throwType,
private Assertions $assertions,
private bool $acceptsNamedArguments,
private ?Type $selfOutType,
private ?string $phpDocComment,
)
Expand Down Expand Up @@ -187,6 +188,11 @@ public function getAsserts(): Assertions
return $this->assertions;
}

public function acceptsNamedArguments(): bool
{
return $this->declaringClass->acceptsNamedArguments() && $this->acceptsNamedArguments;
}

public function getSelfOutType(): ?Type
{
return $this->selfOutType;
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ public static function combineAcceptors(array $acceptors): ParametersAcceptorWit
$impurePoints = [];
$invalidateExpressions = [];
$usedVariables = [];
$acceptsNamedArguments = false;

foreach ($acceptors as $acceptor) {
$returnTypes[] = $acceptor->getReturnType();
Expand All @@ -621,6 +622,7 @@ public static function combineAcceptors(array $acceptors): ParametersAcceptorWit
$impurePoints = array_merge($impurePoints, $acceptor->getImpurePoints());
$invalidateExpressions = array_merge($invalidateExpressions, $acceptor->getInvalidateExpressions());
$usedVariables = array_merge($usedVariables, $acceptor->getUsedVariables());
$acceptsNamedArguments = $acceptsNamedArguments || $acceptor->acceptsNamedArguments();
}
$isVariadic = $isVariadic || $acceptor->isVariadic();

Expand Down Expand Up @@ -722,6 +724,7 @@ public static function combineAcceptors(array $acceptors): ParametersAcceptorWit
$impurePoints,
$invalidateExpressions,
$usedVariables,
$acceptsNamedArguments,
);
}

Expand Down Expand Up @@ -757,6 +760,7 @@ private static function wrapAcceptor(ParametersAcceptor $acceptor): ParametersAc
$acceptor->getImpurePoints(),
$acceptor->getInvalidateExpressions(),
$acceptor->getUsedVariables(),
$acceptor->acceptsNamedArguments(),
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public function getAsserts(): Assertions
return $this->nativeMethodReflection->getAsserts();
}

public function acceptsNamedArguments(): bool
{
return $this->nativeMethodReflection->acceptsNamedArguments();
}

public function getSelfOutType(): ?Type
{
return $this->nativeMethodReflection->getSelfOutType();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function acceptsNamedArguments(): bool
{
return $this->declaringClass->acceptsNamedArguments();
}

public function getSelfOutType(): ?Type
{
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ private function createMethod(
$reflectionMethod = null;
$throwType = null;
$asserts = Assertions::createEmpty();
$acceptsNamedArguments = true;
$selfOutType = null;
$phpDocComment = null;
if ($classReflection->getNativeReflection()->hasMethod($methodReflection->getName())) {
Expand Down Expand Up @@ -539,6 +540,7 @@ private function createMethod(
}

$asserts = Assertions::createFromResolvedPhpDocBlock($stubPhpDoc);
$acceptsNamedArguments = $stubPhpDoc->acceptsNamedArguments();

$selfOutTypeTag = $stubPhpDoc->getSelfOutTag();
if ($selfOutTypeTag !== null) {
Expand Down Expand Up @@ -583,6 +585,7 @@ private function createMethod(
$phpDocParameterTypes[$name] = $paramTag->getType();
}
$asserts = Assertions::createFromResolvedPhpDocBlock($phpDocBlock);
$acceptsNamedArguments = $phpDocBlock->acceptsNamedArguments();

$selfOutTypeTag = $phpDocBlock->getSelfOutTag();
if ($selfOutTypeTag !== null) {
Expand Down Expand Up @@ -625,6 +628,7 @@ private function createMethod(
$hasSideEffects,
$throwType,
$asserts,
$acceptsNamedArguments,
$selfOutType,
$phpDocComment,
);
Expand Down Expand Up @@ -773,6 +777,7 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
$isFinal = $resolvedPhpDoc->isFinal();
$isPure = $resolvedPhpDoc->isPure();
$asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc);
$acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments();
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
$phpDocComment = null;
if ($resolvedPhpDoc->hasPhpDocString()) {
Expand All @@ -793,6 +798,7 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
$isFinal,
$isPure,
$asserts,
$acceptsNamedArguments,
$selfOutType,
$phpDocComment,
$phpDocParameterOutTypes,
Expand Down
Loading

0 comments on commit 74ba8c2

Please sign in to comment.