Skip to content

Commit baad32b

Browse files
shmaxvladar
authored andcommitted
Phpstan Level 3 (#566)
1 parent 7cc72be commit baad32b

Some content is hidden

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

57 files changed

+447
-622
lines changed

docs/type-system/scalar-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class EmailType extends ScalarType
100100
* @return string
101101
* @throws Error
102102
*/
103-
public function parseLiteral($valueNode, array $variables = null)
103+
public function parseLiteral(Node $valueNode, ?array $variables = null)
104104
{
105105
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
106106
// error location in query:

examples/01-blog/Blog/Type/Scalar/UrlType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function parseValue($value)
4848
* @return null|string
4949
* @throws Error
5050
*/
51-
public function parseLiteral($valueNode, array $variables = null)
51+
public function parseLiteral(Node $valueNode, ?array $variables = null)
5252
{
5353
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
5454
// error location in query:

phpstan.neon.dist

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
parameters:
2-
level: 2
2+
level: 3
3+
4+
reportUnmatchedIgnoredErrors: false
5+
6+
inferPrivatePropertyTypeFromConstructor: true
37

48
paths:
59
- %currentWorkingDirectory%/src
@@ -23,6 +27,20 @@ parameters:
2327
- "~Access to an undefined property GraphQL\\\\Language\\\\AST\\\\Node::\\$value~"
2428

2529
includes:
26-
- vendor/phpstan/phpstan-phpunit/extension.neon
27-
- vendor/phpstan/phpstan-phpunit/rules.neon
28-
- vendor/phpstan/phpstan-strict-rules/rules.neon
30+
- vendor/phpstan/phpstan-phpunit/extension.neon
31+
- vendor/phpstan/phpstan-phpunit/rules.neon
32+
- vendor/phpstan/phpstan-strict-rules/rules.neon
33+
34+
services:
35+
-
36+
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsInputTypeStaticMethodTypeSpecifyingExtension
37+
tags:
38+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
39+
-
40+
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsOutputTypeStaticMethodTypeSpecifyingExtension
41+
tags:
42+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
43+
-
44+
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsCompositeTypeStaticMethodTypeSpecifyingExtension
45+
tags:
46+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension

src/Error/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ public function getExtensions()
330330
* @deprecated Use FormattedError::createFromException() instead
331331
*
332332
* @return mixed[]
333+
*
334+
* @codeCoverageIgnore
333335
*/
334336
public function toSerializableArray()
335337
{

src/Error/FormattedError.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ static function ($loc) {
427427
* @deprecated as of v0.10.0, use general purpose method createFromException() instead
428428
*
429429
* @return mixed[]
430+
*
431+
* @codeCoverageIgnore
430432
*/
431433
public static function createFromPHPError(ErrorException $e)
432434
{

src/Executor/ReferenceExecutor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function buildResponse($data)
238238
*
239239
* @param mixed $rootValue
240240
*
241-
* @return Promise|stdClass|mixed[]
241+
* @return Promise|stdClass|mixed[]|null
242242
*/
243243
private function executeOperation(OperationDefinitionNode $operation, $rootValue)
244244
{
@@ -993,7 +993,7 @@ private function completeAbstractValue(AbstractType $returnType, $fieldNodes, Re
993993
* @param mixed|null $contextValue
994994
* @param InterfaceType|UnionType $abstractType
995995
*
996-
* @return ObjectType|Promise|null
996+
* @return Promise|Type|string|null
997997
*/
998998
private function defaultTypeResolver($value, $contextValue, ResolveInfo $info, AbstractType $abstractType)
999999
{

src/Executor/Values.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ public static function getArgumentValuesForMap($fieldDefinition, $argumentValueM
303303
* @param mixed[]|null $variables
304304
*
305305
* @return mixed[]|stdClass|null
306+
*
307+
* @codeCoverageIgnore
306308
*/
307309
public static function valueFromAST(ValueNode $valueNode, InputType $type, ?array $variables = null)
308310
{
@@ -316,6 +318,7 @@ public static function valueFromAST(ValueNode $valueNode, InputType $type, ?arra
316318
*
317319
* @return string[]
318320
*
321+
* @codeCoverageIgnore
319322
* @paarm ScalarType|EnumType|InputObjectType|ListOfType|NonNull $type
320323
*/
321324
public static function isValidPHPValue($value, InputType $type)

src/Experimental/Executor/Collector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Collector
4646
/** @var FieldNode[][] */
4747
private $fields;
4848

49-
/** @var string[] */
49+
/** @var array<string, bool> */
5050
private $visitedFragments;
5151

5252
public function __construct(Schema $schema, Runtime $runtime)

src/Experimental/Executor/CoroutineContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CoroutineContext
2727
/** @var string[] */
2828
public $path;
2929

30-
/** @var ResolveInfo|null */
30+
/** @var ResolveInfo */
3131
public $resolveInfo;
3232

3333
/** @var string[]|null */

src/Experimental/Executor/CoroutineExecutor.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
use GraphQL\Type\Introspection;
3434
use GraphQL\Type\Schema;
3535
use GraphQL\Utils\AST;
36+
use GraphQL\Utils\TypeInfo;
3637
use GraphQL\Utils\Utils;
3738
use SplQueue;
3839
use stdClass;
3940
use Throwable;
4041
use function is_array;
4142
use function is_string;
43+
use function json_decode;
44+
use function json_encode;
4245
use function sprintf;
4346

4447
class CoroutineExecutor implements Runtime, ExecutorImplementation
@@ -73,10 +76,10 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation
7376
/** @var string|null */
7477
private $operationName;
7578

76-
/** @var Collector */
79+
/** @var Collector|null */
7780
private $collector;
7881

79-
/** @var Error[] */
82+
/** @var array<Error> */
8083
private $errors;
8184

8285
/** @var SplQueue */
@@ -85,10 +88,10 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation
8588
/** @var SplQueue */
8689
private $schedule;
8790

88-
/** @var stdClass */
91+
/** @var stdClass|null */
8992
private $rootResult;
9093

91-
/** @var int */
94+
/** @var int|null */
9295
private $pending;
9396

9497
/** @var callable */
@@ -108,6 +111,9 @@ public function __construct(
108111
self::$undefined = Utils::undefined();
109112
}
110113

114+
$this->errors = [];
115+
$this->queue = new SplQueue();
116+
$this->schedule = new SplQueue();
111117
$this->schema = $schema;
112118
$this->fieldResolver = $fieldResolver;
113119
$this->promiseAdapter = $promiseAdapter;
@@ -143,10 +149,11 @@ public static function create(
143149
private static function resultToArray($value, $emptyObjectAsStdClass = true)
144150
{
145151
if ($value instanceof stdClass) {
146-
$array = [];
147-
foreach ($value as $propertyName => $propertyValue) {
152+
$array = (array) $value;
153+
foreach ($array as $propertyName => $propertyValue) {
148154
$array[$propertyName] = self::resultToArray($propertyValue);
149155
}
156+
150157
if ($emptyObjectAsStdClass && empty($array)) {
151158
return new stdClass();
152159
}
@@ -237,9 +244,9 @@ public function doExecute() : Promise
237244
private function finishExecute($value, array $errors) : ExecutionResult
238245
{
239246
$this->rootResult = null;
240-
$this->errors = null;
241-
$this->queue = null;
242-
$this->schedule = null;
247+
$this->errors = [];
248+
$this->queue = new SplQueue();
249+
$this->schedule = new SplQueue();
243250
$this->pending = null;
244251
$this->collector = null;
245252
$this->variableValues = null;
@@ -825,11 +832,16 @@ private function completeValue(CoroutineContext $ctx, Type $type, $value, array
825832
} else {
826833
$childContexts = [];
827834

835+
$fields = [];
836+
if ($this->collector !== null) {
837+
$fields = $this->collector->collectFields(
838+
$objectType,
839+
$ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx)
840+
);
841+
}
842+
828843
/** @var CoroutineContextShared $childShared */
829-
foreach ($this->collector->collectFields(
830-
$objectType,
831-
$ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx)
832-
) as $childShared) {
844+
foreach ($fields as $childShared) {
833845
$childPath = $path;
834846
$childPath[] = $childShared->resultName; // !!! uses array COW semantics
835847
$childCtx = new CoroutineContext(
@@ -938,7 +950,7 @@ private function resolveTypeSlow(CoroutineContext $ctx, $value, AbstractType $ab
938950
$selectedType = null;
939951
foreach ($possibleTypes as $type) {
940952
$typeCheck = yield $type->isTypeOf($value, $this->contextValue, $ctx->resolveInfo);
941-
if ($selectedType !== null || $typeCheck !== true) {
953+
if ($selectedType !== null || ! $typeCheck) {
942954
continue;
943955
}
944956

0 commit comments

Comments
 (0)