Skip to content

Commit

Permalink
Add rector SetList::TYPE_DECLARATION (#2336)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Mar 2, 2023
1 parent 06ee405 commit 723e407
Show file tree
Hide file tree
Showing 85 changed files with 288 additions and 225 deletions.
5 changes: 1 addition & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,12 @@ class Foo
{
/**
* Some attribute.
*
* @var string
*/
protected $bar;
protected string $bar;

/**
* Use $this for fluent setters when we expect the exact same object back.
*
* @param string $bar
* @return $this
*/
public function setBar(string $bar): self
Expand Down
10 changes: 2 additions & 8 deletions benchmarks/ASTUnserializationBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ class ASTUnserializationBench
}
GRAPHQL;

/**
* @var string
*/
protected $documentNode;
protected string $documentNode;

/**
* @var string
*/
protected $documentAST;
protected string $documentAST;

public function prepareSchema(): void
{
Expand Down
1 change: 1 addition & 0 deletions benchmarks/QueryBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function setUp(): void

/**
* Return the full URL to the GraphQL endpoint.
*
* @param array<string, string> $routeParams Parameters to pass to the route
*/
protected function graphQLEndpointUrl(array $routeParams = []): string
Expand Down
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::PHP_72,
SetList::PHP_73,
SetList::PHP_74,
Expand Down Expand Up @@ -52,5 +55,7 @@
VarConstantCommentRector::class, // Noisy
UnSpreadOperatorRector::class, // Breaks some public APIs
AddArrayDefaultToArrayPropertyRector::class, // Break lazy initialization
AddReturnTypeDeclarationFromYieldsRector::class, // iterable is fine
ArrayShapeFromConstantArrayReturnRector::class, // Sometimes too specific in methods that can be overridden
]);
};
4 changes: 2 additions & 2 deletions src/Auth/CanDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function handleField(FieldValue $fieldValue): void
$ability = $this->directiveArgValue('ability');
$resolved = $this->directiveArgValue('resolved');

$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $ability, $resolved) {
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $ability, $resolved) {
$gate = $this->gate->forUser($context->user());
$checkArguments = $this->buildCheckArguments($args);

Expand Down Expand Up @@ -237,7 +237,7 @@ protected function authorize(Gate $gate, string|array $ability, $model, array $a
array_unshift($arguments, $model);

Utils::applyEach(
static function ($ability) use ($gate, $arguments) {
static function ($ability) use ($gate, $arguments): void {
$response = $gate->inspect($ability, $arguments);
if ($response->denied()) {
throw new AuthorizationException($response->message(), $response->code());
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/GuardDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function definition(): string

public function handleField(FieldValue $fieldValue): void
{
$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
$with = $this->directiveArgValue('with', (array) AuthServiceProvider::guard());
$context->setUser($this->authenticate($with));

Expand Down
2 changes: 1 addition & 1 deletion src/Cache/CacheDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handleField(FieldValue $fieldValue): void
$maxAge = $this->directiveArgValue('maxAge');
$isPrivate = $this->directiveArgValue('private', false);

$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($rootCacheKey, $shouldUseTags, $resolver, $maxAge, $isPrivate) {
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($rootCacheKey, $shouldUseTags, $resolver, $maxAge, $isPrivate) {
$parentName = $resolveInfo->parentType->name;
$rootID = null !== $root && null !== $rootCacheKey
? data_get($root, $rootCacheKey)
Expand Down
2 changes: 1 addition & 1 deletion src/CacheControl/CacheControlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function boot(Dispatcher $dispatcher): void

$dispatcher->listen(
StartExecution::class,
function (StartExecution $startExecution) {
function (StartExecution $startExecution): void {
$typeInfo = new TypeInfo($startExecution->schema);
$cacheControl = $this->app->make(CacheControl::class);

Expand Down
2 changes: 1 addition & 1 deletion src/Defer/DeferrableDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handleField(FieldValue $fieldValue): void
{
$fieldType = $fieldValue->getField()->type;

$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $fieldType) {
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $fieldType) {
$wrappedResolver = static fn (): mixed => $resolver($root, $args, $context, $resolveInfo);
$path = implode('.', $resolveInfo->path);

Expand Down
2 changes: 1 addition & 1 deletion src/Execution/Arguments/ArgPartitioner.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function relationMethods(
/**
* Attach a nested argument resolver to an argument.
*
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model> $model
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model>|null $model
*/
protected static function attachNestedArgResolver(string $name, Argument &$argument, ?\ReflectionClass $model): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Execution/ResolveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, Qu
});

Utils::applyEach(
static function ($value) use (&$builder, $directiveFilter) {
static function ($value) use (&$builder, $directiveFilter): void {
if ($value instanceof ArgumentSet) {
self::applyArgBuilderDirectives($value, $builder, $directiveFilter);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Federation/Directives/ExternalDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function resolveField(FieldValue $fieldValue): callable
$defaultFieldResolver = Executor::getDefaultFieldResolver();

// The parent might just hold a foreign key to the external object, in which case we just return that.
return static fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) => is_scalar($root)
return static fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): mixed => is_scalar($root)
? $root
: $defaultFieldResolver($root, $args, $context, $resolveInfo);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Federation/Types/FieldSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
*/
class FieldSet extends ScalarType
{
public function serialize($value)
public function serialize(mixed $value): void
{
}

public function parseValue($value)
public function parseValue(mixed $value): void
{
}

public function parseLiteral(Node $valueNode, ?array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null): void
{
}
}
3 changes: 2 additions & 1 deletion src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @api
*
* @phpstan-import-type ErrorsHandler from \GraphQL\Executor\ExecutionResult
* @phpstan-import-type SerializableResult from \GraphQL\Executor\ExecutionResult
*/
class GraphQL
{
Expand Down Expand Up @@ -260,7 +261,7 @@ public function executeParsedQuery(
*
* @api
*
* @return array<string, mixed>
* @return SerializableResult
*/
public function serializable(ExecutionResult $result): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/LighthouseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function boot(ConfigRepository $configRepository): void
// @phpstan-ignore-next-line larastan overly eager assumes this will always be a concrete instance
if ($exceptionHandler instanceof ExceptionHandler) {
$exceptionHandler->renderable(
function (ClientAware $error) {
function (ClientAware $error): JsonResponse {
assert($error instanceof \Throwable);

if (! $error instanceof Error) {
Expand Down
11 changes: 10 additions & 1 deletion src/Pagination/ConnectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ class ConnectionField
/**
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator<mixed> $paginator
*
* @return array<string, mixed>
* @return array{
* hasNextPage: bool,
* hasPreviousPage: bool,
* startCursor: string|null,
* endCursor: string|null,
* total: int,
* count: int,
* currentPage: int,
* lastPage: int,
* }
*/
public function pageInfoResolver(LengthAwarePaginator $paginator): array
{
Expand Down
11 changes: 10 additions & 1 deletion src/Pagination/PaginatorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ class PaginatorField
/**
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator<mixed> $paginator
*
* @return array<string, mixed>
* @return array{
* count: int,
* currentPage: int,
* firstItem: int,
* hasMorePages: bool,
* lastItem: int,
* lastPage: int,
* perPage: int,
* total: int,
* }
*/
public function paginatorInfoResolver(LengthAwarePaginator $paginator): array
{
Expand Down
9 changes: 8 additions & 1 deletion src/Pagination/SimplePaginatorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class SimplePaginatorField
*
* @param \Illuminate\Pagination\Paginator<mixed> $root
*
* @return array<string, mixed>
* @return array{
* count: int,
* currentPage: int,
* firstItem: int,
* lastItem: int,
* perPage: int,
* hasMorePages: bool,
* }
*/
public function paginatorInfoResolver(Paginator $root): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/AST/ASTHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function getUnderlyingNamedTypeNode(Node $node): NamedTypeNode
/**
* Extract a named argument from a given directive node.
*
* @param mixed $default is returned if the directive does not have the argument
* @param mixed $default is returned if the directive does not have the argument
*/
public static function directiveArgValue(DirectiveNode $directive, string $name, mixed $default = null): mixed
{
Expand Down
3 changes: 2 additions & 1 deletion src/Schema/Directives/AggregateDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nuwave\Lighthouse\Schema\Directives;

use GraphQL\Deferred;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function resolveField(FieldValue $fieldValue): callable

$relation = $this->directiveArgValue('relation');
if (is_string($relation)) {
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Deferred {
$relationBatchLoader = BatchLoaderRegistry::instance(
array_merge(
$this->qualifyPath($args, $resolveInfo),
Expand Down
16 changes: 5 additions & 11 deletions src/Schema/Directives/BaseDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function directiveHasArgument(string $name): bool
*
* @api
*
* @param mixed $default Use this over `??` to preserve explicit `null`
* @param mixed $default Use this over `??` to preserve explicit `null`
*
* @return mixed The argument value or the default
*/
Expand Down Expand Up @@ -165,7 +165,7 @@ protected function getModelClass(string $argumentName = 'model'): string
* @api
*
* @param array<string> $namespacesToTry
* @param callable(string $className): bool $determineMatch
* @param (callable(string $className): bool)|null $determineMatch
*
* @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
*
Expand All @@ -174,7 +174,7 @@ protected function getModelClass(string $argumentName = 'model'): string
protected function namespaceClassName(
string $classCandidate,
array $namespacesToTry = [],
callable $determineMatch = null
?callable $determineMatch = null
): string {
$namespaceForDirective = ASTHelper::namespaceForDirective(
$this->definitionNode,
Expand All @@ -190,17 +190,11 @@ protected function namespaceClassName(
$determineMatch = 'class_exists';
}

$className = Utils::namespaceClassname(
$classCandidate,
$namespacesToTry,
$determineMatch
);
$className = Utils::namespaceClassname($classCandidate, $namespacesToTry, $determineMatch);

if (null === $className) {
$consideredNamespaces = implode(', ', $namespacesToTry);
throw new DefinitionException(
"Failed to find class {$classCandidate} in namespaces [{$consideredNamespaces}] for directive @{$this->name()}."
);
throw new DefinitionException("Failed to find class {$classCandidate} in namespaces [{$consideredNamespaces}] for directive @{$this->name()}.");
}

return $className;
Expand Down
7 changes: 3 additions & 4 deletions src/Schema/Directives/CountDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nuwave\Lighthouse\Schema\Directives;

use GraphQL\Deferred;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function resolveField(FieldValue $fieldValue): callable

$relation = $this->directiveArgValue('relation');
if (is_string($relation)) {
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Deferred {
$relationBatchLoader = BatchLoaderRegistry::instance(
[...$this->qualifyPath($args, $resolveInfo), 'count'],
fn (): RelationBatchLoader => new RelationBatchLoader(
Expand All @@ -96,9 +97,7 @@ public function resolveField(FieldValue $fieldValue): callable
};
}

throw new DefinitionException(
"A `model` or `relation` argument must be assigned to the '{$this->name()}' directive on '{$this->nodeName()}'."
);
throw new DefinitionException("A `model` or `relation` argument must be assigned to the '{$this->name()}' directive on '{$this->nodeName()}'.");
}

public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType): void
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/DropArgsDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function drop(ArgumentSet &$argumentSet): ArgumentSet
// We look for further ArgumentSet instances, they
// might be contained within an array.
Utils::applyEach(
function ($value) {
function ($value): void {
if ($value instanceof ArgumentSet) {
$this->drop($value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/InjectDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handleField(FieldValue $fieldValue): void
throw new DefinitionException("The `inject` directive on {$fieldValue->getParentName()} [{$fieldValue->getFieldName()}] must have a `name` argument");
}

$fieldValue->wrapResolver(static fn (callable $resolver) => static function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($contextAttributeName, $argumentName, $resolver) {
$fieldValue->wrapResolver(static fn (callable $resolver): \Closure => static function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($contextAttributeName, $argumentName, $resolver) {
$valueFromContext = data_get($context, $contextAttributeName);
$argumentSet = $resolveInfo->argumentSet;
$argumentSet->addValue($argumentName, $valueFromContext);
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/MutationExecutorDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function executeMutation(Model $model, ArgumentSet|array $args, ?Relat
$update = new ResolveNested($this->makeExecutionFunction($parentRelation));

return Utils::mapEach(
static fn (ArgumentSet $argumentSet) => $update($model->newInstance(), $argumentSet),
static fn (ArgumentSet $argumentSet): mixed => $update($model->newInstance(), $argumentSet),
$args
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/NestDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __invoke(mixed $root, $args): mixed
$resolveNested = new ResolveNested();

return Utils::mapEach(
static fn (ArgumentSet $argumentSet) => $resolveNested($root, $argumentSet),
static fn (ArgumentSet $argumentSet): mixed => $resolveNested($root, $argumentSet),
$args
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/RenameArgsDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function rename(ArgumentSet &$argumentSet): ArgumentSet
// We look for further ArgumentSet instances, they
// might be contained within an array.
Utils::applyEach(
function ($value) {
function ($value): void {
if ($value instanceof ArgumentSet) {
$this->rename($value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/ThrottleDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function handleField(FieldValue $fieldValue): void
];
}

$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $limits) {
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $limits) {
foreach ($limits as $limit) {
$this->handleLimit(
$limit['key'],
Expand Down
Loading

0 comments on commit 723e407

Please sign in to comment.