From 90d0156291d4d6100f13911cc2274ed14557eaf9 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 9 Oct 2018 16:51:23 +0200 Subject: [PATCH] Use PHPStan strict rules Two rules excluded: missing type hints and empty() usage --- composer.json | 1 + phpstan.neon.dist | 5 +++ src/Executor/Executor.php | 39 ++++++++++++------- src/Executor/Values.php | 2 +- src/Language/Visitor.php | 10 ++--- src/Type/Definition/InputObjectField.php | 2 +- src/Type/Definition/ObjectType.php | 2 +- src/Type/Definition/Type.php | 2 +- src/Type/Definition/UnionType.php | 5 +-- src/Type/Introspection.php | 14 +++---- src/Utils/AST.php | 8 +++- src/Utils/BreakingChangesFinder.php | 21 +++++----- src/Utils/SchemaExtender.php | 4 +- src/Utils/TypeInfo.php | 3 +- .../Rules/KnownArgumentNamesOnDirectives.php | 2 +- src/Validator/Rules/KnownDirectives.php | 24 +++++++++--- src/Validator/Rules/QueryComplexity.php | 21 +++++----- src/Validator/Rules/QueryDepth.php | 2 +- .../Rules/VariablesInAllowedPosition.php | 2 +- src/Validator/ValidationContext.php | 16 ++++---- tests/Executor/DeferredFieldsTest.php | 2 +- .../Promise/ReactPromiseAdapterTest.php | 2 +- tests/Executor/Promise/SyncPromiseTest.php | 8 ++-- tests/Language/LexerTest.php | 8 ++-- tests/Language/ParserTest.php | 20 +++++----- tests/Server/RequestParsingTest.php | 6 +-- tests/Server/RequestValidationTest.php | 2 +- tests/StarWarsSchema.php | 2 +- tests/Type/DefinitionTest.php | 8 ++-- tests/Type/SchemaTest.php | 2 +- tests/Type/ValidationTest.php | 6 +-- tests/Utils/SchemaExtenderTest.php | 10 ++--- tests/Validator/ValidationTest.php | 4 +- 33 files changed, 152 insertions(+), 113 deletions(-) diff --git a/composer.json b/composer.json index bc8f8b2..c1f4afe 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "phpbench/phpbench": "^0.14.0", "phpstan/phpstan": "^0.10.3", "phpstan/phpstan-phpunit": "^0.10.0", + "phpstan/phpstan-strict-rules": "^0.10.1", "phpunit/phpunit": "^7.2", "psr/http-message": "^1.0", "react/promise": "2.*" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b458ec3..af2455b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,6 +5,11 @@ parameters: - %currentWorkingDirectory%/src - %currentWorkingDirectory%/tests + ignoreErrors: + - "~Construct empty\\(\\) is not allowed\\. Use more strict comparison~" + - "~(Method|Property) .+::.+(\\(\\))? (has parameter \\$\\w+ with no|has no return|has no) typehint specified~" + includes: - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon + - vendor/phpstan/phpstan-strict-rules/rules.neon diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 4567fa7..a29a6e0 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -258,14 +258,14 @@ class Executor $rawVariableValues ?: [] ); - if ($coercedVariableValues['errors']) { - $errors = array_merge($errors, $coercedVariableValues['errors']); - } else { + if (empty($coercedVariableValues['errors'])) { $variableValues = $coercedVariableValues['coerced']; + } else { + $errors = array_merge($errors, $coercedVariableValues['errors']); } } - if ($errors) { + if (! empty($errors)) { return $errors; } @@ -322,6 +322,7 @@ class Executor if ($data !== null) { $data = (array) $data; } + return new ExecutionResult($data, $this->exeContext->errors); } @@ -354,6 +355,7 @@ class Executor null, function ($error) { $this->exeContext->addError($error); + return $this->exeContext->promises->createFulfilled(null); } ); @@ -362,6 +364,7 @@ class Executor return $result; } catch (Error $error) { $this->exeContext->addError($error); + return null; } } @@ -586,10 +589,12 @@ class Executor if ($promise) { return $promise->then(static function ($resolvedResult) use ($responseName, $results) { $results[$responseName] = $resolvedResult; + return $results; }); } $results[$responseName] = $result; + return $results; }, [] @@ -599,6 +604,7 @@ class Executor return self::fixResultsIfEmptyArray($resolvedResults); }); } + return self::fixResultsIfEmptyArray($result); } @@ -1028,15 +1034,20 @@ class Executor */ private function promiseReduce(array $values, callable $callback, $initialValue) { - return array_reduce($values, function ($previous, $value) use ($callback) { - $promise = $this->getPromise($previous); - if ($promise) { - return $promise->then(static function ($resolved) use ($callback, $value) { - return $callback($resolved, $value); - }); - } - return $callback($previous, $value); - }, $initialValue); + return array_reduce( + $values, + function ($previous, $value) use ($callback) { + $promise = $this->getPromise($previous); + if ($promise) { + return $promise->then(static function ($resolved) use ($callback, $value) { + return $callback($resolved, $value); + }); + } + + return $callback($previous, $value); + }, + $initialValue + ); } /** @@ -1326,6 +1337,7 @@ class Executor &$result ) { $subFieldNodes = $this->collectSubFields($returnType, $fieldNodes); + return $this->executeFields($returnType, $result, $path, $subFieldNodes); } @@ -1353,6 +1365,7 @@ class Executor } $this->subFieldCache[$returnType][$fieldNodes] = $subFieldNodes; } + return $this->subFieldCache[$returnType][$fieldNodes]; } diff --git a/src/Executor/Values.php b/src/Executor/Values.php index 5550544..cfb0886 100644 --- a/src/Executor/Values.php +++ b/src/Executor/Values.php @@ -132,7 +132,7 @@ class Values } ); - if ($directiveNode) { + if ($directiveNode !== null) { return self::getArgumentValues($directiveDef, $directiveNode, $variableValues); } } diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index e652d66..45f8fff 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -251,12 +251,12 @@ class Visitor $inArray = $stack['inArray']; $stack = $stack['prev']; } else { - $key = $parent ? ($inArray ? $index : $keys[$index]) : $UNDEFINED; - $node = $parent ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot; + $key = $parent !== null ? ($inArray ? $index : $keys[$index]) : $UNDEFINED; + $node = $parent !== null ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot; if ($node === null || $node === $UNDEFINED) { continue; } - if ($parent) { + if ($parent !== null) { $path[] = $key; } } @@ -321,7 +321,7 @@ class Visitor $keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: []; $index = -1; $edits = []; - if ($parent) { + if ($parent !== null) { $ancestors[] = $parent; } $parent = $node; @@ -464,7 +464,7 @@ class Visitor if ($fn) { $result = call_user_func_array($fn, func_get_args()); - if ($result) { + if ($result !== null) { $typeInfo->leave($node); if ($result instanceof Node) { $typeInfo->enter($result); diff --git a/src/Type/Definition/InputObjectField.php b/src/Type/Definition/InputObjectField.php index 6f01c94..82fa570 100644 --- a/src/Type/Definition/InputObjectField.php +++ b/src/Type/Definition/InputObjectField.php @@ -21,7 +21,7 @@ class InputObjectField /** @var string|null */ public $description; - /** @var callable|InputType */ + /** @var mixed */ public $type; /** @var InputValueDefinitionNode|null */ diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 5cb9556..ba0260a 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -173,7 +173,7 @@ class ObjectType extends Type implements OutputType, CompositeType, NamedType $interfaces = $this->config['interfaces'] ?? []; $interfaces = is_callable($interfaces) ? call_user_func($interfaces) : $interfaces; - if ($interfaces && ! is_array($interfaces)) { + if ($interfaces !== null && ! is_array($interfaces)) { throw new InvariantViolation( sprintf('%s interfaces must be an Array or a callable which returns an Array.', $this->name) ); diff --git a/src/Type/Definition/Type.php b/src/Type/Definition/Type.php index f9c12ad..9d9a0a8 100644 --- a/src/Type/Definition/Type.php +++ b/src/Type/Definition/Type.php @@ -152,7 +152,7 @@ abstract class Type implements JsonSerializable */ public static function isBuiltInType(Type $type) { - return in_array($type->name, array_keys(self::getAllBuiltInTypes())); + return in_array($type->name, array_keys(self::getAllBuiltInTypes()), true); } /** diff --git a/src/Type/Definition/UnionType.php b/src/Type/Definition/UnionType.php index 221c828..a8e6fd7 100644 --- a/src/Type/Definition/UnionType.php +++ b/src/Type/Definition/UnionType.php @@ -51,10 +51,7 @@ class UnionType extends Type implements AbstractType, OutputType, CompositeType, $this->config = $config; } - /** - * @return mixed - */ - public function isPossibleType(Type $type) + public function isPossibleType(Type $type) : bool { if (! $type instanceof ObjectType) { return false; diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index 6e3d78e..c19c0c4 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -571,25 +571,25 @@ EOD; 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => static function ($d) { - return in_array(DirectiveLocation::QUERY, $d->locations) || - in_array(DirectiveLocation::MUTATION, $d->locations) || - in_array(DirectiveLocation::SUBSCRIPTION, $d->locations); + return in_array(DirectiveLocation::QUERY, $d->locations, true) || + in_array(DirectiveLocation::MUTATION, $d->locations, true) || + in_array(DirectiveLocation::SUBSCRIPTION, $d->locations, true); }, ], 'onFragment' => [ 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => static function ($d) { - return in_array(DirectiveLocation::FRAGMENT_SPREAD, $d->locations) || - in_array(DirectiveLocation::INLINE_FRAGMENT, $d->locations) || - in_array(DirectiveLocation::FRAGMENT_DEFINITION, $d->locations); + return in_array(DirectiveLocation::FRAGMENT_SPREAD, $d->locations, true) || + in_array(DirectiveLocation::INLINE_FRAGMENT, $d->locations, true) || + in_array(DirectiveLocation::FRAGMENT_DEFINITION, $d->locations, true); }, ], 'onField' => [ 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => static function ($d) { - return in_array(DirectiveLocation::FIELD, $d->locations); + return in_array(DirectiveLocation::FIELD, $d->locations, true); }, ], ], diff --git a/src/Utils/AST.php b/src/Utils/AST.php index aa3d421..5ef4233 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -414,7 +414,7 @@ class AST $fieldName = $field->name; $fieldNode = $fieldNodes[$fieldName] ?? null; - if (! $fieldNode || self::isMissingVariable($fieldNode->value, $variables)) { + if ($fieldNode === null || self::isMissingVariable($fieldNode->value, $variables)) { if ($field->defaultValueExists()) { $coercedObj[$fieldName] = $field->defaultValue; } elseif ($field->getType() instanceof NonNull) { @@ -424,7 +424,11 @@ class AST continue; } - $fieldValue = self::valueFromAST($fieldNode ? $fieldNode->value : null, $field->getType(), $variables); + $fieldValue = self::valueFromAST( + $fieldNode !== null ? $fieldNode->value : null, + $field->getType(), + $variables + ); if ($undefined === $fieldValue) { // Invalid: intentionally return no value. diff --git a/src/Utils/BreakingChangesFinder.php b/src/Utils/BreakingChangesFinder.php index 885ee32..597cd16 100644 --- a/src/Utils/BreakingChangesFinder.php +++ b/src/Utils/BreakingChangesFinder.php @@ -371,7 +371,7 @@ class BreakingChangesFinder )) || // moving from non-null to nullable of the same underlying type is safe ! ($newType instanceof NonNull) && - self::isChangeSafeForInputObjectFieldOrFieldArg($oldType->getWrappedType(), $newType); + self::isChangeSafeForInputObjectFieldOrFieldArg($oldType->getWrappedType(), $newType); } return false; @@ -496,7 +496,7 @@ class BreakingChangesFinder return $arg->name === $oldArgDef->name; } ); - if ($newArgDef) { + if ($newArgDef !== null) { $isSafe = self::isChangeSafeForInputObjectFieldOrFieldArg( $oldArgDef->getType(), $newArgDef->getType() @@ -536,7 +536,7 @@ class BreakingChangesFinder } ); - if ($oldArgDef) { + if ($oldArgDef !== null) { continue; } @@ -584,12 +584,13 @@ class BreakingChangesFinder $oldInterfaces = $oldType->getInterfaces(); $newInterfaces = $newType->getInterfaces(); foreach ($oldInterfaces as $oldInterface) { - if (Utils::find( + $interface = Utils::find( $newInterfaces, - static function (InterfaceType $interface) use ($oldInterface) { + static function (InterfaceType $interface) use ($oldInterface) : bool { return $interface->name === $oldInterface->name; } - )) { + ); + if ($interface !== null) { continue; } @@ -850,12 +851,14 @@ class BreakingChangesFinder $oldInterfaces = $oldType->getInterfaces(); $newInterfaces = $newType->getInterfaces(); foreach ($newInterfaces as $newInterface) { - if (Utils::find( + $interface = Utils::find( $oldInterfaces, - static function (InterfaceType $interface) use ($newInterface) { + static function (InterfaceType $interface) use ($newInterface) : bool { return $interface->name === $newInterface->name; } - )) { + ); + + if ($interface !== null) { continue; } diff --git a/src/Utils/SchemaExtender.php b/src/Utils/SchemaExtender.php index 1812b7d..be5deef 100644 --- a/src/Utils/SchemaExtender.php +++ b/src/Utils/SchemaExtender.php @@ -74,7 +74,7 @@ class SchemaExtender */ protected static function checkExtensionNode(Type $type, Node $node) : void { - switch ($node->kind ?? null) { + switch ($node->kind) { case NodeKind::OBJECT_TYPE_EXTENSION: if (! ($type instanceof ObjectType)) { throw new Error( @@ -589,7 +589,7 @@ class SchemaExtender foreach ($schemaExtension->operationTypes as $operationType) { $operation = $operationType->operation; - if ($operationTypes[$operation]) { + if (isset($operationTypes[$operation])) { throw new Error('Must provide only one ' . $operation . ' type in schema.'); } $operationTypes[$operation] = static::$astBuilder->buildType($operationType->type); diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 76b8019..722195d 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -210,6 +210,7 @@ class TypeInfo $typeMap = self::extractTypes($arg->getType(), $typeMap); } } + return $typeMap; } @@ -309,7 +310,7 @@ class TypeInfo return $arg->name === $node->name->value; } ); - if ($argDef) { + if ($argDef !== null) { $argType = $argDef->getType(); } } diff --git a/src/Validator/Rules/KnownArgumentNamesOnDirectives.php b/src/Validator/Rules/KnownArgumentNamesOnDirectives.php index 7ffd986..a013820 100644 --- a/src/Validator/Rules/KnownArgumentNamesOnDirectives.php +++ b/src/Validator/Rules/KnownArgumentNamesOnDirectives.php @@ -78,7 +78,7 @@ class KnownArgumentNamesOnDirectives extends ValidationRule foreach ($directiveNode->arguments as $argNode) { $argName = $argNode->name->value; - if (in_array($argName, $knownArgs)) { + if (in_array($argName, $knownArgs, true)) { continue; } diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index da233a4..927ce26 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -37,12 +37,25 @@ class KnownDirectives extends ValidationRule continue; } - $locationsMap[$def->name->value] = array_map(static function ($name) { - return $name->value; - }, $def->locations); + $locationsMap[$def->name->value] = array_map( + static function ($name) { + return $name->value; + }, + $def->locations + ); } + return [ - NodeKind::DIRECTIVE => function (DirectiveNode $node, $key, $parent, $path, $ancestors) use ($context, $locationsMap) { + NodeKind::DIRECTIVE => function ( + DirectiveNode $node, + $key, + $parent, + $path, + $ancestors + ) use ( + $context, + $locationsMap + ) { $name = $node->name->value; $locations = $locationsMap[$name] ?? null; @@ -51,12 +64,13 @@ class KnownDirectives extends ValidationRule self::unknownDirectiveMessage($name), [$node] )); + return; } $candidateLocation = $this->getDirectiveLocationForASTPath($ancestors); - if (! $candidateLocation || in_array($candidateLocation, $locations)) { + if (! $candidateLocation || in_array($candidateLocation, $locations, true)) { return; } $context->reportError( diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index 990fcbb..8a90652 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -29,7 +29,7 @@ class QueryComplexity extends QuerySecurityRule /** @var int */ private $maxQueryComplexity; - /** @var mixed[]|null */ + /** @var mixed[]|null */ private $rawVariableValues = []; /** @var ArrayObject */ @@ -86,7 +86,7 @@ class QueryComplexity extends QuerySecurityRule } $context->reportError( - new Error($this->maxQueryComplexityErrorMessage( + new Error(self::maxQueryComplexityErrorMessage( $this->getMaxQueryComplexity(), $complexity )) @@ -193,7 +193,7 @@ class QueryComplexity extends QuerySecurityRule $this->getRawVariableValues() ); - if ($variableValuesResult['errors']) { + if (! empty($variableValuesResult['errors'])) { throw new Error(implode( "\n\n", array_map( @@ -207,16 +207,17 @@ class QueryComplexity extends QuerySecurityRule $variableValues = $variableValuesResult['coerced']; if ($directiveNode->name->value === 'include') { - $directive = Directive::includeDirective(); - $directiveArgs = Values::getArgumentValues($directive, $directiveNode, $variableValues); + $directive = Directive::includeDirective(); + /** @var bool $directiveArgsIf */ + $directiveArgsIf = Values::getArgumentValues($directive, $directiveNode, $variableValues)['if']; - return ! $directiveArgs['if']; + return ! $directiveArgsIf; } - $directive = Directive::skipDirective(); - $directiveArgs = Values::getArgumentValues($directive, $directiveNode, $variableValues); + $directive = Directive::skipDirective(); + $directiveArgsIf = Values::getArgumentValues($directive, $directiveNode, $variableValues); - return $directiveArgs['if']; + return $directiveArgsIf['if']; } } @@ -248,7 +249,7 @@ class QueryComplexity extends QuerySecurityRule $rawVariableValues ); - if ($variableValuesResult['errors']) { + if (! empty($variableValuesResult['errors'])) { throw new Error(implode( "\n\n", array_map( diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index 1408eee..5a35f0c 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -36,7 +36,7 @@ class QueryDepth extends QuerySecurityRule } $context->reportError( - new Error($this->maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth)) + new Error(self::maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth)) ); }, ], diff --git a/src/Validator/Rules/VariablesInAllowedPosition.php b/src/Validator/Rules/VariablesInAllowedPosition.php index 4fa3d46..2deb0e9 100644 --- a/src/Validator/Rules/VariablesInAllowedPosition.php +++ b/src/Validator/Rules/VariablesInAllowedPosition.php @@ -36,7 +36,7 @@ class VariablesInAllowedPosition extends ValidationRule $varName = $node->name->value; $varDef = $this->varDefMap[$varName] ?? null; - if (! $varDef || ! $type) { + if ($varDef === null || $type === null) { continue; } diff --git a/src/Validator/ValidationContext.php b/src/Validator/ValidationContext.php index 8ef75e3..7163371 100644 --- a/src/Validator/ValidationContext.php +++ b/src/Validator/ValidationContext.php @@ -98,12 +98,12 @@ class ValidationContext { $usages = $this->recursiveVariableUsages[$operation] ?? null; - if (! $usages) { + if ($usages === null) { $usages = $this->getVariableUsages($operation); $fragments = $this->getRecursivelyReferencedFragments($operation); $tmp = [$usages]; - for ($i = 0; $i < count($fragments); $i++) { + foreach ($fragments as $i => $fragment) { $tmp[] = $this->getVariableUsages($fragments[$i]); } $usages = call_user_func_array('array_merge', $tmp); @@ -120,7 +120,7 @@ class ValidationContext { $usages = $this->variableUsages[$node] ?? null; - if (! $usages) { + if ($usages === null) { $newUsages = []; $typeInfo = new TypeInfo($this->schema); Visitor::visit( @@ -154,15 +154,15 @@ class ValidationContext { $fragments = $this->recursivelyReferencedFragments[$operation] ?? null; - if (! $fragments) { + if ($fragments === null) { $fragments = []; $collectedNames = []; $nodesToVisit = [$operation]; while (! empty($nodesToVisit)) { $node = array_pop($nodesToVisit); $spreads = $this->getFragmentSpreads($node); - for ($i = 0; $i < count($spreads); $i++) { - $fragName = $spreads[$i]->name->value; + foreach ($spreads as $spread) { + $fragName = $spread->name->value; if (! empty($collectedNames[$fragName])) { continue; @@ -190,14 +190,14 @@ class ValidationContext public function getFragmentSpreads(HasSelectionSet $node) { $spreads = $this->fragmentSpreads[$node] ?? null; - if (! $spreads) { + if ($spreads === null) { $spreads = []; /** @var SelectionSetNode[] $setsToVisit */ $setsToVisit = [$node->selectionSet]; while (! empty($setsToVisit)) { $set = array_pop($setsToVisit); - for ($i = 0; $i < count($set->selections); $i++) { + for ($i = 0, $selectionCount = count($set->selections); $i < $selectionCount; $i++) { $selection = $set->selections[$i]; if ($selection->kind === NodeKind::FRAGMENT_SPREAD) { $spreads[] = $selection; diff --git a/tests/Executor/DeferredFieldsTest.php b/tests/Executor/DeferredFieldsTest.php index 82df651..6847fda 100644 --- a/tests/Executor/DeferredFieldsTest.php +++ b/tests/Executor/DeferredFieldsTest.php @@ -153,7 +153,7 @@ class DeferredFieldsTest extends TestCase return Utils::filter( $this->storyDataSource, static function ($story) use ($category) { - return in_array($category['id'], $story['categoryIds']); + return in_array($category['id'], $story['categoryIds'], true); } ); }, diff --git a/tests/Executor/Promise/ReactPromiseAdapterTest.php b/tests/Executor/Promise/ReactPromiseAdapterTest.php index a616613..7e8af70 100644 --- a/tests/Executor/Promise/ReactPromiseAdapterTest.php +++ b/tests/Executor/Promise/ReactPromiseAdapterTest.php @@ -26,7 +26,7 @@ class ReactPromiseAdapterTest extends TestCase return; } - $this->markTestSkipped('react/promise package must be installed to run GraphQL\Tests\Executor\Promise\ReactPromiseAdapterTest'); + self::markTestSkipped('react/promise package must be installed to run GraphQL\Tests\Executor\Promise\ReactPromiseAdapterTest'); } public function testIsThenableReturnsTrueWhenAReactPromiseIsGiven() : void diff --git a/tests/Executor/Promise/SyncPromiseTest.php b/tests/Executor/Promise/SyncPromiseTest.php index 302715c..732f95e 100644 --- a/tests/Executor/Promise/SyncPromiseTest.php +++ b/tests/Executor/Promise/SyncPromiseTest.php @@ -260,14 +260,14 @@ class SyncPromiseTest extends TestCase try { $promise->reject(new Exception('other-reason')); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (Throwable $e) { self::assertEquals('Cannot change rejection reason', $e->getMessage()); } try { $promise->resolve('anything'); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (Throwable $e) { self::assertEquals('Cannot resolve rejected promise', $e->getMessage()); } @@ -316,7 +316,7 @@ class SyncPromiseTest extends TestCase try { $promise->resolve($promise); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (Throwable $e) { self::assertEquals('Cannot resolve promise with self', $e->getMessage()); self::assertEquals(SyncPromise::PENDING, $promise->state); @@ -345,7 +345,7 @@ class SyncPromiseTest extends TestCase try { $promise->reject('a'); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (Error $e) { throw $e; } catch (Throwable $e) { diff --git a/tests/Language/LexerTest.php b/tests/Language/LexerTest.php index ccb360d..5e7ed1e 100644 --- a/tests/Language/LexerTest.php +++ b/tests/Language/LexerTest.php @@ -145,7 +145,7 @@ class LexerTest extends TestCase try { $this->lexOne($str); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $error) { self::assertEquals( 'Syntax Error: Cannot parse the unexpected character "?".' . "\n" . @@ -175,7 +175,7 @@ class LexerTest extends TestCase try { $lexer = new Lexer($source); $lexer->advance(); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $error) { self::assertEquals( 'Syntax Error: Cannot parse the unexpected character "?".' . "\n" . @@ -197,7 +197,7 @@ class LexerTest extends TestCase try { $lexer = new Lexer($source); $lexer->advance(); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $error) { self::assertEquals( 'Syntax Error: Cannot parse the unexpected character "?".' . "\n" . @@ -671,7 +671,7 @@ class LexerTest extends TestCase $this->expectExceptionMessage('Syntax Error: Invalid number, expected digit but got: "b"'); try { $lexer->advance(); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $error) { self::assertEquals([$this->loc(1, 3)], $error->getLocations()); throw $error; diff --git a/tests/Language/ParserTest.php b/tests/Language/ParserTest.php index 1427739..d0769e9 100644 --- a/tests/Language/ParserTest.php +++ b/tests/Language/ParserTest.php @@ -86,7 +86,7 @@ fragment MissingOn Type ) : void { try { Parser::parse($str); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $e) { self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($stringRepresentation, (string) $e); @@ -108,7 +108,7 @@ fragment MissingOn Type { try { Parser::parse(new Source('query', 'MyQuery.graphql')); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (SyntaxError $error) { self::assertEquals( "Syntax Error: Expected {, found \n\nMyQuery.graphql (1:6)\n1: query\n ^\n", @@ -503,7 +503,7 @@ GRAPHQL ], ]; - self::assertEquals($expected, $this->nodeToArray($result)); + self::assertEquals($expected, self::nodeToArray($result)); } /** @@ -575,7 +575,7 @@ GRAPHQL 'kind' => NodeKind::NULL, 'loc' => ['start' => 0, 'end' => 4], ], - $this->nodeToArray(Parser::parseValue('null')) + self::nodeToArray(Parser::parseValue('null')) ); } @@ -602,7 +602,7 @@ GRAPHQL ], ], ], - $this->nodeToArray(Parser::parseValue('[123 "abc"]')) + self::nodeToArray(Parser::parseValue('[123 "abc"]')) ); } @@ -621,7 +621,7 @@ GRAPHQL 'value' => 'String', ], ], - $this->nodeToArray(Parser::parseType('String')) + self::nodeToArray(Parser::parseType('String')) ); } @@ -640,7 +640,7 @@ GRAPHQL 'value' => 'MyType', ], ], - $this->nodeToArray(Parser::parseType('MyType')) + self::nodeToArray(Parser::parseType('MyType')) ); } @@ -663,7 +663,7 @@ GRAPHQL ], ], ], - $this->nodeToArray(Parser::parseType('[MyType]')) + self::nodeToArray(Parser::parseType('[MyType]')) ); } @@ -686,7 +686,7 @@ GRAPHQL ], ], ], - $this->nodeToArray(Parser::parseType('MyType!')) + self::nodeToArray(Parser::parseType('MyType!')) ); } @@ -713,7 +713,7 @@ GRAPHQL ], ], ], - $this->nodeToArray(Parser::parseType('[MyType!]')) + self::nodeToArray(Parser::parseType('[MyType!]')) ); } } diff --git a/tests/Server/RequestParsingTest.php b/tests/Server/RequestParsingTest.php index d09e6c4..b6c73d6 100644 --- a/tests/Server/RequestParsingTest.php +++ b/tests/Server/RequestParsingTest.php @@ -225,8 +225,8 @@ class RequestParsingTest extends TestCase 'operationName' => $operation, ]; $parsed = [ - 'raw' => $this->parseRawMultipartFormdataRequest($post), - 'psr' => $this->parsePsrMultipartFormdataRequest($post), + 'raw' => $this->parseRawMultipartFormDataRequest($post), + 'psr' => $this->parsePsrMultipartFormDataRequest($post), ]; foreach ($parsed as $method => $parsedBody) { @@ -395,7 +395,7 @@ class RequestParsingTest extends TestCase { try { $this->parsePsrRequest('application/json', json_encode(null)); - $this->fail('Expected exception not thrown'); + self::fail('Expected exception not thrown'); } catch (InvariantViolation $e) { // Expecting parsing exception to be thrown somewhere else: self::assertEquals( diff --git a/tests/Server/RequestValidationTest.php b/tests/Server/RequestValidationTest.php index 80c44f6..c418407 100644 --- a/tests/Server/RequestValidationTest.php +++ b/tests/Server/RequestValidationTest.php @@ -67,7 +67,7 @@ class RequestValidationTest extends TestCase if (! empty($errors[0])) { self::assertEquals($expectedMessage, $errors[0]->getMessage()); } else { - $this->fail('Expected error not returned'); + self::fail('Expected error not returned'); } } diff --git a/tests/StarWarsSchema.php b/tests/StarWarsSchema.php index cded7da..b9240af 100644 --- a/tests/StarWarsSchema.php +++ b/tests/StarWarsSchema.php @@ -134,7 +134,7 @@ class StarWarsSchema ]; }, 'resolveType' => static function ($obj) use (&$humanType, &$droidType) { - return StarWarsData::getHuman($obj['id']) ? $humanType : $droidType; + return StarWarsData::getHuman($obj['id']) === null ? $droidType : $humanType; }, ]); diff --git a/tests/Type/DefinitionTest.php b/tests/Type/DefinitionTest.php index 820159a..5bfcb82 100644 --- a/tests/Type/DefinitionTest.php +++ b/tests/Type/DefinitionTest.php @@ -1599,14 +1599,14 @@ class DefinitionTest extends TestCase try { Type::listOf($type); } catch (Throwable $e) { - $this->fail('List is expected to accept type: ' . get_class($type) . ', but got error: ' . $e->getMessage()); + self::fail('List is expected to accept type: ' . get_class($type) . ', but got error: ' . $e->getMessage()); } } foreach ($badTypes as $badType) { $typeStr = Utils::printSafe($badType); try { Type::listOf($badType); - $this->fail(sprintf('List should not accept %s', $typeStr)); + self::fail(sprintf('List should not accept %s', $typeStr)); } catch (InvariantViolation $e) { self::assertEquals(sprintf('Expected %s to be a GraphQL type.', $typeStr), $e->getMessage()); } @@ -1640,14 +1640,14 @@ class DefinitionTest extends TestCase try { Type::nonNull($type); } catch (Throwable $e) { - $this->fail('NonNull is expected to accept type: ' . get_class($type) . ', but got error: ' . $e->getMessage()); + self::fail('NonNull is expected to accept type: ' . get_class($type) . ', but got error: ' . $e->getMessage()); } } foreach ($notNullableTypes as $badType) { $typeStr = Utils::printSafe($badType); try { Type::nonNull($badType); - $this->fail(sprintf('Nulls should not accept %s', $typeStr)); + self::fail(sprintf('Nulls should not accept %s', $typeStr)); } catch (InvariantViolation $e) { self::assertEquals(sprintf('Expected %s to be a GraphQL nullable type.', $typeStr), $e->getMessage()); } diff --git a/tests/Type/SchemaTest.php b/tests/Type/SchemaTest.php index c906bf8..ddb832b 100644 --- a/tests/Type/SchemaTest.php +++ b/tests/Type/SchemaTest.php @@ -108,7 +108,7 @@ class SchemaTest extends TestCase */ public function testThrowsHumanReableErrorIfSchemaTypesIsNotDefined() : void { - $this->markTestSkipped("Can't check interface implementations without full schema scan"); + self::markTestSkipped("Can't check interface implementations without full schema scan"); $this->expectException(InvariantViolation::class); $this->expectExceptionMessage( diff --git a/tests/Type/ValidationTest.php b/tests/Type/ValidationTest.php index 04be994..27ba792 100644 --- a/tests/Type/ValidationTest.php +++ b/tests/Type/ValidationTest.php @@ -201,7 +201,7 @@ class ValidationTest extends TestCase foreach ($closures as $index => $factory) { try { $factory(); - $this->fail('Expected exception not thrown for entry ' . $index); + self::fail('Expected exception not thrown for entry ' . $index); } catch (InvariantViolation $e) { self::assertEquals($expectedError, $e->getMessage(), 'Error in callable #' . $index); } @@ -356,7 +356,7 @@ class ValidationTest extends TestCase ); foreach ($array as $index => $error) { if (! isset($messages[$index]) || ! $error instanceof Error) { - $this->fail('Received unexpected error: ' . $error->getMessage()); + self::fail('Received unexpected error: ' . $error->getMessage()); } self::assertEquals($messages[$index]['message'], $error->getMessage()); $errorLocations = []; @@ -1189,7 +1189,7 @@ class ValidationTest extends TestCase public function testRejectsAnObjectImplementingTheSameInterfaceTwiceDueToExtension() : void { $this->expectNotToPerformAssertions(); - $this->markTestIncomplete('extend does not work this way (yet).'); + self::markTestIncomplete('extend does not work this way (yet).'); $schema = BuildSchema::build(' type Query { field: AnotherObject diff --git a/tests/Utils/SchemaExtenderTest.php b/tests/Utils/SchemaExtenderTest.php index 552cc29..80f19d6 100644 --- a/tests/Utils/SchemaExtenderTest.php +++ b/tests/Utils/SchemaExtenderTest.php @@ -165,7 +165,7 @@ class SchemaExtenderTest extends TestCase 'someInterface' => [ 'args' => [ 'id' => [ - 'type' => Type::nonNull(Type::ID()), + 'type' => Type::nonNull(Type::id()), ], ], 'type' => $SomeInterfaceType, @@ -219,7 +219,7 @@ class SchemaExtenderTest extends TestCase $ast->definitions = array_values(array_filter( $ast->definitions instanceof NodeList ? iterator_to_array($ast->definitions->getIterator()) : $ast->definitions, function (Node $node) : bool { - return ! in_array(Printer::doPrint($node), $this->testSchemaDefinitions); + return ! in_array(Printer::doPrint($node), $this->testSchemaDefinitions, true); } )); @@ -1579,14 +1579,14 @@ class SchemaExtenderTest extends TestCase */ public function testMaintainsConfigurationOfTheOriginalSchemaObject() { - $this->markTestSkipped('allowedLegacyNames currently not supported'); + self::markTestSkipped('allowedLegacyNames currently not supported'); $testSchemaWithLegacyNames = new Schema( [ 'query' => new ObjectType([ 'name' => 'Query', 'fields' => static function () { - return ['id' => ['type' => Type::ID()]]; + return ['id' => ['type' => Type::id()]]; }, ]), ]/*, @@ -1608,7 +1608,7 @@ class SchemaExtenderTest extends TestCase */ public function testAddsToTheConfigurationOfTheOriginalSchemaObject() { - $this->markTestSkipped('allowedLegacyNames currently not supported'); + self::markTestSkipped('allowedLegacyNames currently not supported'); $testSchemaWithLegacyNames = new Schema( [ diff --git a/tests/Validator/ValidationTest.php b/tests/Validator/ValidationTest.php index 4b5534f..05dbbf2 100644 --- a/tests/Validator/ValidationTest.php +++ b/tests/Validator/ValidationTest.php @@ -43,7 +43,7 @@ class ValidationTest extends ValidatorTestCase ]; $this->expectInvalid( - $this->getTestSchema(), + self::getTestSchema(), null, $doc, [$expectedError] @@ -59,6 +59,6 @@ class ValidationTest extends ValidatorTestCase 'locations' => [['line' => 1, 'column' => 2]], ]; $this->expectFailsCompleteValidation($query, [$expectedError]); - $this->expectValid($this->getTestSchema(), [], $query); + $this->expectValid(self::getTestSchema(), [], $query); } }