Skip to content

Commit 723e407

Browse files
authored
Add rector SetList::TYPE_DECLARATION (#2336)
1 parent 06ee405 commit 723e407

File tree

85 files changed

+288
-225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+288
-225
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,12 @@ class Foo
212212
{
213213
/**
214214
* Some attribute.
215-
*
216-
* @var string
217215
*/
218-
protected $bar;
216+
protected string $bar;
219217

220218
/**
221219
* Use $this for fluent setters when we expect the exact same object back.
222220
*
223-
* @param string $bar
224221
* @return $this
225222
*/
226223
public function setBar(string $bar): self

benchmarks/ASTUnserializationBench.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,9 @@ class ASTUnserializationBench
2727
}
2828
GRAPHQL;
2929

30-
/**
31-
* @var string
32-
*/
33-
protected $documentNode;
30+
protected string $documentNode;
3431

35-
/**
36-
* @var string
37-
*/
38-
protected $documentAST;
32+
protected string $documentAST;
3933

4034
public function prepareSchema(): void
4135
{

benchmarks/QueryBench.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function setUp(): void
2525

2626
/**
2727
* Return the full URL to the GraphQL endpoint.
28+
*
2829
* @param array<string, string> $routeParams Parameters to pass to the route
2930
*/
3031
protected function graphQLEndpointUrl(array $routeParams = []): string

rector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
1515
use Rector\PHPUnit\Set\PHPUnitSetList;
1616
use Rector\Set\ValueObject\SetList;
17+
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
18+
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
1719

1820
return static function (RectorConfig $rectorConfig): void {
1921
$rectorConfig->sets([
2022
SetList::CODE_QUALITY,
2123
SetList::CODING_STYLE,
24+
SetList::TYPE_DECLARATION,
2225
SetList::PHP_72,
2326
SetList::PHP_73,
2427
SetList::PHP_74,
@@ -52,5 +55,7 @@
5255
VarConstantCommentRector::class, // Noisy
5356
UnSpreadOperatorRector::class, // Breaks some public APIs
5457
AddArrayDefaultToArrayPropertyRector::class, // Break lazy initialization
58+
AddReturnTypeDeclarationFromYieldsRector::class, // iterable is fine
59+
ArrayShapeFromConstantArrayReturnRector::class, // Sometimes too specific in methods that can be overridden
5560
]);
5661
};

src/Auth/CanDirective.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function handleField(FieldValue $fieldValue): void
115115
$ability = $this->directiveArgValue('ability');
116116
$resolved = $this->directiveArgValue('resolved');
117117

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

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

239239
Utils::applyEach(
240-
static function ($ability) use ($gate, $arguments) {
240+
static function ($ability) use ($gate, $arguments): void {
241241
$response = $gate->inspect($ability, $arguments);
242242
if ($response->denied()) {
243243
throw new AuthorizationException($response->message(), $response->code());

src/Auth/GuardDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function definition(): string
4949

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

src/Cache/CacheDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function handleField(FieldValue $fieldValue): void
5151
$maxAge = $this->directiveArgValue('maxAge');
5252
$isPrivate = $this->directiveArgValue('private', false);
5353

54-
$fieldValue->wrapResolver(fn (callable $resolver) => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($rootCacheKey, $shouldUseTags, $resolver, $maxAge, $isPrivate) {
54+
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($rootCacheKey, $shouldUseTags, $resolver, $maxAge, $isPrivate) {
5555
$parentName = $resolveInfo->parentType->name;
5656
$rootID = null !== $root && null !== $rootCacheKey
5757
? data_get($root, $rootCacheKey)

src/CacheControl/CacheControlServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function boot(Dispatcher $dispatcher): void
3333

3434
$dispatcher->listen(
3535
StartExecution::class,
36-
function (StartExecution $startExecution) {
36+
function (StartExecution $startExecution): void {
3737
$typeInfo = new TypeInfo($startExecution->schema);
3838
$cacheControl = $this->app->make(CacheControl::class);
3939

src/Defer/DeferrableDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function handleField(FieldValue $fieldValue): void
4141
{
4242
$fieldType = $fieldValue->getField()->type;
4343

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

src/Execution/Arguments/ArgPartitioner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static function relationMethods(
8585
/**
8686
* Attach a nested argument resolver to an argument.
8787
*
88-
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model> $model
88+
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model>|null $model
8989
*/
9090
protected static function attachNestedArgResolver(string $name, Argument &$argument, ?\ReflectionClass $model): void
9191
{

src/Execution/ResolveInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, Qu
107107
});
108108

109109
Utils::applyEach(
110-
static function ($value) use (&$builder, $directiveFilter) {
110+
static function ($value) use (&$builder, $directiveFilter): void {
111111
if ($value instanceof ArgumentSet) {
112112
self::applyArgBuilderDirectives($value, $builder, $directiveFilter);
113113
}

src/Federation/Directives/ExternalDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function resolveField(FieldValue $fieldValue): callable
3232
$defaultFieldResolver = Executor::getDefaultFieldResolver();
3333

3434
// The parent might just hold a foreign key to the external object, in which case we just return that.
35-
return static fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) => is_scalar($root)
35+
return static fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): mixed => is_scalar($root)
3636
? $root
3737
: $defaultFieldResolver($root, $args, $context, $resolveInfo);
3838
}

src/Federation/Types/FieldSet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
*/
1111
class FieldSet extends ScalarType
1212
{
13-
public function serialize($value)
13+
public function serialize(mixed $value): void
1414
{
1515
}
1616

17-
public function parseValue($value)
17+
public function parseValue(mixed $value): void
1818
{
1919
}
2020

21-
public function parseLiteral(Node $valueNode, ?array $variables = null)
21+
public function parseLiteral(Node $valueNode, ?array $variables = null): void
2222
{
2323
}
2424
}

src/GraphQL.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @api
3939
*
4040
* @phpstan-import-type ErrorsHandler from \GraphQL\Executor\ExecutionResult
41+
* @phpstan-import-type SerializableResult from \GraphQL\Executor\ExecutionResult
4142
*/
4243
class GraphQL
4344
{
@@ -260,7 +261,7 @@ public function executeParsedQuery(
260261
*
261262
* @api
262263
*
263-
* @return array<string, mixed>
264+
* @return SerializableResult
264265
*/
265266
public function serializable(ExecutionResult $result): array
266267
{

src/LighthouseServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function boot(ConfigRepository $configRepository): void
138138
// @phpstan-ignore-next-line larastan overly eager assumes this will always be a concrete instance
139139
if ($exceptionHandler instanceof ExceptionHandler) {
140140
$exceptionHandler->renderable(
141-
function (ClientAware $error) {
141+
function (ClientAware $error): JsonResponse {
142142
assert($error instanceof \Throwable);
143143

144144
if (! $error instanceof Error) {

src/Pagination/ConnectionField.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ class ConnectionField
1616
/**
1717
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator<mixed> $paginator
1818
*
19-
* @return array<string, mixed>
19+
* @return array{
20+
* hasNextPage: bool,
21+
* hasPreviousPage: bool,
22+
* startCursor: string|null,
23+
* endCursor: string|null,
24+
* total: int,
25+
* count: int,
26+
* currentPage: int,
27+
* lastPage: int,
28+
* }
2029
*/
2130
public function pageInfoResolver(LengthAwarePaginator $paginator): array
2231
{

src/Pagination/PaginatorField.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ class PaginatorField
1010
/**
1111
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator<mixed> $paginator
1212
*
13-
* @return array<string, mixed>
13+
* @return array{
14+
* count: int,
15+
* currentPage: int,
16+
* firstItem: int,
17+
* hasMorePages: bool,
18+
* lastItem: int,
19+
* lastPage: int,
20+
* perPage: int,
21+
* total: int,
22+
* }
1423
*/
1524
public function paginatorInfoResolver(LengthAwarePaginator $paginator): array
1625
{

src/Pagination/SimplePaginatorField.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class SimplePaginatorField
1212
*
1313
* @param \Illuminate\Pagination\Paginator<mixed> $root
1414
*
15-
* @return array<string, mixed>
15+
* @return array{
16+
* count: int,
17+
* currentPage: int,
18+
* firstItem: int,
19+
* lastItem: int,
20+
* perPage: int,
21+
* hasMorePages: bool,
22+
* }
1623
*/
1724
public function paginatorInfoResolver(Paginator $root): array
1825
{

src/Schema/AST/ASTHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function getUnderlyingNamedTypeNode(Node $node): NamedTypeNode
137137
/**
138138
* Extract a named argument from a given directive node.
139139
*
140-
* @param mixed $default is returned if the directive does not have the argument
140+
* @param mixed $default is returned if the directive does not have the argument
141141
*/
142142
public static function directiveArgValue(DirectiveNode $directive, string $name, mixed $default = null): mixed
143143
{

src/Schema/Directives/AggregateDirective.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nuwave\Lighthouse\Schema\Directives;
44

5+
use GraphQL\Deferred;
56
use GraphQL\Language\AST\FieldDefinitionNode;
67
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
78
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
@@ -108,7 +109,7 @@ public function resolveField(FieldValue $fieldValue): callable
108109

109110
$relation = $this->directiveArgValue('relation');
110111
if (is_string($relation)) {
111-
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
112+
return function (Model $parent, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Deferred {
112113
$relationBatchLoader = BatchLoaderRegistry::instance(
113114
array_merge(
114115
$this->qualifyPath($args, $resolveInfo),

src/Schema/Directives/BaseDirective.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function directiveHasArgument(string $name): bool
122122
*
123123
* @api
124124
*
125-
* @param mixed $default Use this over `??` to preserve explicit `null`
125+
* @param mixed $default Use this over `??` to preserve explicit `null`
126126
*
127127
* @return mixed The argument value or the default
128128
*/
@@ -165,7 +165,7 @@ protected function getModelClass(string $argumentName = 'model'): string
165165
* @api
166166
*
167167
* @param array<string> $namespacesToTry
168-
* @param callable(string $className): bool $determineMatch
168+
* @param (callable(string $className): bool)|null $determineMatch
169169
*
170170
* @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
171171
*
@@ -174,7 +174,7 @@ protected function getModelClass(string $argumentName = 'model'): string
174174
protected function namespaceClassName(
175175
string $classCandidate,
176176
array $namespacesToTry = [],
177-
callable $determineMatch = null
177+
?callable $determineMatch = null
178178
): string {
179179
$namespaceForDirective = ASTHelper::namespaceForDirective(
180180
$this->definitionNode,
@@ -190,17 +190,11 @@ protected function namespaceClassName(
190190
$determineMatch = 'class_exists';
191191
}
192192

193-
$className = Utils::namespaceClassname(
194-
$classCandidate,
195-
$namespacesToTry,
196-
$determineMatch
197-
);
193+
$className = Utils::namespaceClassname($classCandidate, $namespacesToTry, $determineMatch);
198194

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

206200
return $className;

src/Schema/Directives/CountDirective.php

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

33
namespace Nuwave\Lighthouse\Schema\Directives;
44

5+
use GraphQL\Deferred;
56
use GraphQL\Language\AST\FieldDefinitionNode;
67
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
78
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
@@ -84,7 +85,7 @@ public function resolveField(FieldValue $fieldValue): callable
8485

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

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

104103
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType): void

src/Schema/Directives/DropArgsDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function drop(ArgumentSet &$argumentSet): ArgumentSet
3737
// We look for further ArgumentSet instances, they
3838
// might be contained within an array.
3939
Utils::applyEach(
40-
function ($value) {
40+
function ($value): void {
4141
if ($value instanceof ArgumentSet) {
4242
$this->drop($value);
4343
}

src/Schema/Directives/InjectDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function handleField(FieldValue $fieldValue): void
4646
throw new DefinitionException("The `inject` directive on {$fieldValue->getParentName()} [{$fieldValue->getFieldName()}] must have a `name` argument");
4747
}
4848

49-
$fieldValue->wrapResolver(static fn (callable $resolver) => static function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($contextAttributeName, $argumentName, $resolver) {
49+
$fieldValue->wrapResolver(static fn (callable $resolver): \Closure => static function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($contextAttributeName, $argumentName, $resolver) {
5050
$valueFromContext = data_get($context, $contextAttributeName);
5151
$argumentSet = $resolveInfo->argumentSet;
5252
$argumentSet->addValue($argumentName, $valueFromContext);

src/Schema/Directives/MutationExecutorDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function executeMutation(Model $model, ArgumentSet|array $args, ?Relat
7474
$update = new ResolveNested($this->makeExecutionFunction($parentRelation));
7575

7676
return Utils::mapEach(
77-
static fn (ArgumentSet $argumentSet) => $update($model->newInstance(), $argumentSet),
77+
static fn (ArgumentSet $argumentSet): mixed => $update($model->newInstance(), $argumentSet),
7878
$args
7979
);
8080
}

src/Schema/Directives/NestDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __invoke(mixed $root, $args): mixed
3030
$resolveNested = new ResolveNested();
3131

3232
return Utils::mapEach(
33-
static fn (ArgumentSet $argumentSet) => $resolveNested($root, $argumentSet),
33+
static fn (ArgumentSet $argumentSet): mixed => $resolveNested($root, $argumentSet),
3434
$args
3535
);
3636
}

src/Schema/Directives/RenameArgsDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function rename(ArgumentSet &$argumentSet): ArgumentSet
3232
// We look for further ArgumentSet instances, they
3333
// might be contained within an array.
3434
Utils::applyEach(
35-
function ($value) {
35+
function ($value): void {
3636
if ($value instanceof ArgumentSet) {
3737
$this->rename($value);
3838
}

src/Schema/Directives/ThrottleDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function handleField(FieldValue $fieldValue): void
8989
];
9090
}
9191

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

0 commit comments

Comments
 (0)