Skip to content

Commit e18d52d

Browse files
authored
Fix PHPStan level 5 errors for tests (#1014)
1 parent dfe07a9 commit e18d52d

15 files changed

+105
-101
lines changed

src/Executor/Promise/Adapter/SyncPromiseAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace GraphQL\Executor\Promise\Adapter;
66

7+
use GraphQL\Deferred;
78
use GraphQL\Error\InvariantViolation;
89
use GraphQL\Executor\ExecutionResult;
910
use GraphQL\Executor\Promise\Promise;
@@ -28,7 +29,10 @@ public function convertThenable($thenable): Promise
2829
{
2930
if (! $thenable instanceof SyncPromise) {
3031
// End-users should always use Deferred (and don't use SyncPromise directly)
31-
throw new InvariantViolation('Expected instance of GraphQL\Deferred, got ' . Utils::printSafe($thenable));
32+
$deferred = Deferred::class;
33+
$safeThenable = Utils::printSafe($thenable);
34+
35+
throw new InvariantViolation("Expected instance of {$deferred}, got {$safeThenable}");
3236
}
3337

3438
return new Promise($thenable, $this);

src/Type/Definition/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static function getStandardTypes(): array
183183
}
184184

185185
/**
186-
* @param array<string, ScalarType> $types
186+
* @param array<ScalarType> $types
187187
*/
188188
public static function overrideStandardTypes(array $types): void
189189
{

src/Utils/Utils.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,9 @@ public static function printSafe($var): string
263263
}
264264

265265
/**
266-
* UTF-8 compatible chr()
267-
*
268-
* @param string $ord
269-
* @param string $encoding
266+
* UTF-8 compatible chr().
270267
*/
271-
public static function chr($ord, $encoding = 'UTF-8'): string
268+
public static function chr(int $ord, string $encoding = 'UTF-8'): string
272269
{
273270
if ($encoding === 'UCS-4BE') {
274271
return pack('N', $ord);
@@ -278,11 +275,9 @@ public static function chr($ord, $encoding = 'UTF-8'): string
278275
}
279276

280277
/**
281-
* UTF-8 compatible ord()
282-
*
283-
* @return mixed
278+
* UTF-8 compatible ord().
284279
*/
285-
public static function ord(string $char, string $encoding = 'UTF-8')
280+
public static function ord(string $char, string $encoding = 'UTF-8'): int
286281
{
287282
if (! isset($char[1])) {
288283
return ord($char);

src/Validator/Rules/OverlappingFieldsCanBeMerged.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -864,29 +864,27 @@ static function ($allFields, $conflict): array {
864864
}
865865

866866
/**
867-
* @param string $responseName
868-
* @param string $reason
867+
* @param string|array<array{string, string|array<array{string, string}>}> $reasonOrReasons
869868
*/
870-
public static function fieldsConflictMessage($responseName, $reason)
869+
public static function fieldsConflictMessage(string $responseName, $reasonOrReasons): string
871870
{
872-
$reasonMessage = static::reasonMessage($reason);
871+
$reasonMessage = static::reasonMessage($reasonOrReasons);
873872

874-
return sprintf(
875-
'Fields "%s" conflict because %s. Use different aliases on the fields to fetch both if this was intentional.',
876-
$responseName,
877-
$reasonMessage
878-
);
873+
return "Fields \"{$responseName}\" conflict because {$reasonMessage}. Use different aliases on the fields to fetch both if this was intentional.";
879874
}
880875

881-
public static function reasonMessage($reasonOrReasons)
876+
/**
877+
* @param string|array<array{string, string|array<array{string, string}>}> $reasonOrReasons
878+
*/
879+
public static function reasonMessage($reasonOrReasons): string
882880
{
883881
if (is_array($reasonOrReasons)) {
884882
$reasons = array_map(
885883
static function (array $reason): string {
886884
[$responseName, $subReason] = $reason;
887885
$reasonMessage = static::reasonMessage($subReason);
888886

889-
return sprintf('subfields "%s" conflict because %s', $responseName, $reasonMessage);
887+
return "subfields \"{$responseName}\" conflict because {$reasonMessage}";
890888
},
891889
$reasonOrReasons
892890
);

tests/Executor/Promise/SyncPromiseAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
class SyncPromiseAdapterTest extends TestCase
1818
{
19-
/** @var SyncPromiseAdapter */
20-
private $promises;
19+
private SyncPromiseAdapter $promises;
2120

2221
public function setUp(): void
2322
{
@@ -51,6 +50,7 @@ public function testConvert(): void
5150

5251
$this->expectException(InvariantViolation::class);
5352
$this->expectExceptionMessage('Expected instance of GraphQL\Deferred, got (empty string)');
53+
// @phpstan-ignore-next-line purposefully wrong
5454
$this->promises->convertThenable('');
5555
}
5656

tests/Executor/Promise/SyncPromiseTestTestCaseBase.php renamed to tests/Executor/Promise/SyncPromiseTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
use Exception;
88
use GraphQL\Executor\Promise\Adapter\SyncPromise;
99
use GraphQL\Tests\TestCaseBase;
10-
use PHPUnit\Framework\Error\Error;
1110
use Throwable;
1211

1312
use function uniqid;
1413

15-
class SyncPromiseTestTestCaseBase extends TestCaseBase
14+
class SyncPromiseTest extends TestCaseBase
1615
{
1716
public function getFulfilledPromiseResolveData()
1817
{
@@ -347,10 +346,9 @@ public function testPendingPromise(): void
347346
self::assertEquals(SyncPromise::PENDING, $promise->state);
348347

349348
try {
349+
// @phpstan-ignore-next-line purposefully wrong
350350
$promise->reject('a');
351351
self::fail('Expected exception not thrown');
352-
} catch (Error $e) {
353-
throw $e;
354352
} catch (Throwable $e) {
355353
self::assertEquals(SyncPromise::PENDING, $promise->state);
356354
}

tests/Server/QueryExecutionTest.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,28 +375,25 @@ public function testAllowsPersistedQueries(): void
375375

376376
public function testProhibitsInvalidPersistedQueryLoader(): void
377377
{
378-
$this->expectException(InvariantViolation::class);
379-
$this->expectExceptionMessage(
378+
// @phpstan-ignore-next-line purposefully wrong
379+
$this->config->setPersistedQueryLoader(static fn (): array => ['err' => 'err']);
380+
381+
$this->expectExceptionObject(new InvariantViolation(
380382
'Persisted query loader must return query string or instance of GraphQL\Language\AST\DocumentNode ' .
381383
'but got: {"err":"err"}'
382-
);
383-
$this->config->setPersistedQueryLoader(static function (): array {
384-
return ['err' => 'err'];
385-
});
384+
));
386385
$this->executePersistedQuery('some-id');
387386
}
388387

389388
public function testPersistedQueriesAreStillValidatedByDefault(): void
390389
{
391-
$this->config->setPersistedQueryLoader(static function (): string {
392-
return '{invalid}';
393-
});
390+
$this->config->setPersistedQueryLoader(static fn (): string => '{ invalid }');
394391
$result = $this->executePersistedQuery('some-id');
395392
$expected = [
396393
'errors' => [
397394
[
398395
'message' => 'Cannot query field "invalid" on type "Query".',
399-
'locations' => [['line' => 1, 'column' => 2]],
396+
'locations' => [['line' => 1, 'column' => 3]],
400397
],
401398
],
402399
];
@@ -455,12 +452,13 @@ public function testExecutesQueryWhenQueryAndQueryIdArePassed(): void
455452

456453
public function testProhibitsUnexpectedValidationRules(): void
457454
{
458-
$this->expectException(InvariantViolation::class);
459-
$this->expectExceptionMessage('Expecting validation rules to be array or callable returning array, but got: instance of stdClass');
460-
$this->config->setValidationRules(static function (OperationParams $params): stdClass {
461-
return new stdClass();
462-
});
463-
$this->executeQuery('{f1}');
455+
// @phpstan-ignore-next-line purposefully wrong
456+
$this->config->setValidationRules(static fn (): stdClass => new stdClass());
457+
458+
$this->expectExceptionObject(new InvariantViolation(
459+
'Expecting validation rules to be array or callable returning array, but got: instance of stdClass'
460+
));
461+
$this->executeQuery('{ f1 }');
464462
}
465463

466464
public function testExecutesBatchedQueries(): void

tests/Server/RequestParsingTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,22 @@ public function testParsesGraphqlRequest(): void
3535
}
3636

3737
/**
38-
* @param string $contentType
39-
* @param string $content
40-
*
41-
* @return OperationParams|OperationParams[]
38+
* @return OperationParams|array<int, OperationParams>
4239
*/
43-
private function parseRawRequest($contentType, $content, string $method = 'POST')
40+
private function parseRawRequest(?string $contentType, string $content, string $method = 'POST')
4441
{
4542
$_SERVER['CONTENT_TYPE'] = $contentType;
4643
$_SERVER['REQUEST_METHOD'] = $method;
4744

4845
$helper = new Helper();
4946

50-
return $helper->parseHttpRequest(static function () use ($content): string {
51-
return $content;
52-
});
47+
return $helper->parseHttpRequest(static fn (): string => $content);
5348
}
5449

5550
/**
56-
* @param string $contentType
57-
* @param string $content
58-
*
59-
* @return OperationParams|OperationParams[]
51+
* @return OperationParams|array<int, OperationParams>
6052
*/
61-
private function parsePsrRequest($contentType, $content, string $method = 'POST')
53+
private function parsePsrRequest(?string $contentType, string $content, string $method = 'POST')
6254
{
6355
$psrRequest = new Request(
6456
$method,

tests/Server/ServerConfigTest.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace GraphQL\Tests\Server;
66

7+
use Closure;
78
use GraphQL\Error\DebugFlag;
89
use GraphQL\Error\InvariantViolation;
910
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
1011
use GraphQL\Server\ServerConfig;
1112
use GraphQL\Type\Definition\ObjectType;
1213
use GraphQL\Type\Definition\Type;
1314
use GraphQL\Type\Schema;
15+
use GraphQL\Validator\Rules\UniqueEnumValueNames;
1416
use PHPUnit\Framework\TestCase;
1517
use stdClass;
1618

@@ -75,28 +77,42 @@ public function testAllowsSettingErrorFormatter(): void
7577
{
7678
$config = ServerConfig::create();
7779

78-
$formatter = static function (): void {
79-
};
80-
$config->setErrorFormatter($formatter);
81-
self::assertSame($formatter, $config->getErrorFormatter());
80+
$callable = [self::class, 'formatError'];
81+
$config->setErrorFormatter($callable);
82+
self::assertSame($callable, $config->getErrorFormatter());
83+
84+
$closure = Closure::fromCallable($callable);
85+
$config->setErrorFormatter($closure);
86+
self::assertSame($closure, $config->getErrorFormatter());
87+
}
8288

83-
$formatter = 'date'; // test for callable
84-
$config->setErrorFormatter($formatter);
85-
self::assertSame($formatter, $config->getErrorFormatter());
89+
/**
90+
* @return array<string, mixed>
91+
*/
92+
public static function formatError(): array
93+
{
94+
return [];
8695
}
8796

8897
public function testAllowsSettingErrorsHandler(): void
8998
{
9099
$config = ServerConfig::create();
91100

92-
$handler = static function (): void {
93-
};
94-
$config->setErrorsHandler($handler);
95-
self::assertSame($handler, $config->getErrorsHandler());
101+
$callable = [self::class, 'handleError'];
102+
$config->setErrorsHandler($callable);
103+
self::assertSame($callable, $config->getErrorsHandler());
104+
105+
$closure = Closure::fromCallable($callable);
106+
$config->setErrorsHandler($closure);
107+
self::assertSame($closure, $config->getErrorsHandler());
108+
}
96109

97-
$handler = 'date'; // test for callable
98-
$config->setErrorsHandler($handler);
99-
self::assertSame($handler, $config->getErrorsHandler());
110+
/**
111+
* @return array<int, array<string, mixed>>
112+
*/
113+
public static function handleError(): array
114+
{
115+
return [];
100116
}
101117

102118
public function testAllowsSettingPromiseAdapter(): void
@@ -120,19 +136,13 @@ public function testAllowsSettingValidationRules(): void
120136
$config->setValidationRules($rules);
121137
self::assertSame($rules, $config->getValidationRules());
122138

123-
$rules = [
124-
static function (): void {
125-
},
126-
];
139+
$rules = [new UniqueEnumValueNames()];
127140
$config->setValidationRules($rules);
128141
self::assertSame($rules, $config->getValidationRules());
129142

130-
$rules = static function (): array {
131-
return [
132-
static function (): void {
133-
},
134-
];
135-
};
143+
$rules = static fn (): array => [
144+
new UniqueEnumValueNames(),
145+
];
136146
$config->setValidationRules($rules);
137147
self::assertSame($rules, $config->getValidationRules());
138148
}
@@ -155,14 +165,18 @@ public function testAllowsSettingPersistedQueryLoader(): void
155165
{
156166
$config = ServerConfig::create();
157167

158-
$loader = static function (): void {
159-
};
160-
$config->setPersistedQueryLoader($loader);
161-
self::assertSame($loader, $config->getPersistedQueryLoader());
168+
$callable = [self::class, 'loadPersistedQuery'];
169+
$config->setPersistedQueryLoader($callable);
170+
self::assertSame($callable, $config->getPersistedQueryLoader());
171+
172+
$closure = Closure::fromCallable($callable);
173+
$config->setPersistedQueryLoader($closure);
174+
self::assertSame($closure, $config->getPersistedQueryLoader());
175+
}
162176

163-
$loader = 'date'; // test for callable
164-
$config->setPersistedQueryLoader($loader);
165-
self::assertSame($loader, $config->getPersistedQueryLoader());
177+
public static function loadPersistedQuery(): string
178+
{
179+
return '{ foo }';
166180
}
167181

168182
public function testAllowsSettingCatchPhpErrors(): void

tests/Type/LazyTypeLoaderTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ public function testWorksWithTypeLoader(): void
274274
self::assertEquals([], $this->calls);
275275

276276
$node = $schema->getType('Node');
277-
self::assertSame(Schema::resolveType($this->node), $node);
277+
self::assertInstanceOf(InterfaceType::class, $node);
278+
$resolvedNode = Schema::resolveType($this->node);
279+
self::assertInstanceOf(InterfaceType::class, $resolvedNode);
280+
self::assertSame($resolvedNode, $node);
278281
self::assertEquals(['Node'], $this->calls);
279282

280283
$content = $schema->getType('Content');
@@ -285,11 +288,10 @@ public function testWorksWithTypeLoader(): void
285288
self::assertSame(Schema::resolveType($this->postStoryMutationInput), $input);
286289
self::assertEquals(['Node', 'Content', 'PostStoryMutationInput'], $this->calls);
287290

288-
$result = $schema->isSubType(
289-
Schema::resolveType($this->node),
290-
Schema::resolveType($this->blogStory)
291-
);
292-
self::assertTrue($result);
291+
$resolvedBlogStory = Schema::resolveType($this->blogStory);
292+
self::assertInstanceOf(ObjectType::class, $resolvedBlogStory);
293+
294+
self::assertTrue($schema->isSubType($resolvedNode, $resolvedBlogStory));
293295
self::assertEquals(
294296
[
295297
'Node',

tests/Type/SchemaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public function resolveType($objectValue, $context, ResolveInfo $info)
145145
};
146146

147147
$this->schema->isSubType(
148+
// @phpstan-ignore-next-line purposefully wrong
148149
$anonymousAbstractType,
149150
new InterfaceType(['name' => 'Interface'])
150151
);

0 commit comments

Comments
 (0)