diff --git a/composer.json b/composer.json index 3289dc143..54d22f9a9 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,7 @@ "lint" : "phpcs", "fix" : "phpcbf", "stan": "phpstan analyse --ansi --memory-limit 2048M", - "phpstan-baseline": "phpstan analyse --ansi --generate-baseline=phpstan-baseline.neon", + "phpstan-baseline": "phpstan analyse --ansi --generate-baseline", "check": "composer lint && composer stan && composer test" } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8598378fa..f57b1c63d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5,11 +5,6 @@ parameters: count: 1 path: src/Error/Error.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 6 - path: src/Error/Error.php - - message: "#^Only booleans are allowed in &&, Throwable\\|null given on the left side\\.$#" count: 1 @@ -80,11 +75,6 @@ parameters: count: 1 path: src/Error/FormattedError.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 3 - path: src/Error/FormattedError.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 5 @@ -110,46 +100,16 @@ parameters: count: 1 path: src/Error/FormattedError.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Error/Warning.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Executor/ExecutionContext.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 2 path: src/Executor/ExecutionResult.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Executor/ExecutionResult.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Executor/Executor.php - - message: "#^Variable property access on object\\.$#" count: 2 path: src/Executor/Executor.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Executor/Promise/Adapter/SyncPromise.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 5 - path: src/Executor/ReferenceExecutor.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 4 @@ -185,11 +145,6 @@ parameters: count: 3 path: src/Experimental/Executor/CoroutineExecutor.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Experimental/Executor/CoroutineExecutor.php - - message: "#^Variable property access on object\\.$#" count: 2 @@ -200,11 +155,6 @@ parameters: count: 1 path: src/Experimental/Executor/CoroutineExecutor.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/GraphQL.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 2 @@ -220,41 +170,16 @@ parameters: count: 1 path: src/Language/AST/Node.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Language/Lexer.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 3 path: src/Language/Parser.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Language/Parser.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Language/Printer.php - - message: "#^Only booleans are allowed in a ternary operator condition, array\\\\|null given\\.$#" count: 2 path: src/Language/Printer.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Language/Source.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Language/Visitor.php - - message: "#^Only booleans are allowed in a negated boolean, array\\ given\\.$#" count: 1 @@ -295,11 +220,6 @@ parameters: count: 2 path: src/Server/Helper.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 4 - path: src/Server/Helper.php - - message: "#^Only booleans are allowed in an if condition, int given\\.$#" count: 1 @@ -340,11 +260,6 @@ parameters: count: 1 path: src/Server/Helper.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Server/OperationParams.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 2 @@ -360,11 +275,6 @@ parameters: count: 1 path: src/Type/Definition/EnumType.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Type/Definition/FieldDefinition.php - - message: "#^Variable property access on \\$this\\(GraphQL\\\\Type\\\\Definition\\\\InputObjectField\\)\\.$#" count: 1 @@ -380,11 +290,6 @@ parameters: count: 1 path: src/Type/Definition/InputObjectType.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Type/Definition/ObjectType.php - - message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Language\\\\AST\\\\SelectionSetNode\\|null given\\.$#" count: 1 @@ -416,7 +321,7 @@ parameters: path: src/Type/Introspection.php - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" + message: "#^Anonymous function should have native return typehint \"array\"\\.$#" count: 1 path: src/Type/Introspection.php @@ -430,11 +335,6 @@ parameters: count: 1 path: src/Type/Schema.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Type/Schema.php - - message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\Type\\|null given\\.$#" count: 1 @@ -515,11 +415,6 @@ parameters: count: 1 path: src/Type/SchemaValidationContext.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Type/SchemaValidationContext.php - - message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Language\\\\AST\\\\InterfaceTypeDefinitionNode\\|GraphQL\\\\Language\\\\AST\\\\InterfaceTypeExtensionNode\\|GraphQL\\\\Language\\\\AST\\\\ObjectTypeDefinitionNode\\|GraphQL\\\\Language\\\\AST\\\\ObjectTypeExtensionNode given\\.$#" count: 1 @@ -660,11 +555,6 @@ parameters: count: 1 path: src/Utils/ASTDefinitionBuilder.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Utils/BreakingChangesFinder.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 3 @@ -725,11 +615,6 @@ parameters: count: 1 path: src/Utils/TypeInfo.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Utils/TypeInfo.php - - message: "#^Only booleans are allowed in an if condition, GraphQL\\\\Type\\\\Definition\\\\Directive\\|GraphQL\\\\Type\\\\Definition\\\\FieldDefinition\\|null given\\.$#" count: 1 @@ -750,11 +635,6 @@ parameters: count: 1 path: src/Utils/TypeInfo.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Utils/Utils.php - - message: "#^Variable property access on object\\.$#" count: 1 @@ -795,11 +675,6 @@ parameters: count: 1 path: src/Utils/Value.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Validator/DocumentValidator.php - - message: "#^Only booleans are allowed in a negated boolean, \\(GraphQL\\\\Type\\\\Definition\\\\CompositeType&GraphQL\\\\Type\\\\Definition\\\\Type\\)\\|null given\\.$#" count: 1 @@ -905,11 +780,6 @@ parameters: count: 2 path: src/Validator/Rules/OverlappingFieldsCanBeMerged.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Validator/Rules/OverlappingFieldsCanBeMerged.php - - message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Language\\\\AST\\\\ArgumentNode\\|null given\\.$#" count: 1 @@ -940,11 +810,6 @@ parameters: count: 1 path: src/Validator/Rules/ProvidedRequiredArguments.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Validator/Rules/ProvidedRequiredArguments.php - - message: "#^Only booleans are allowed in \\|\\|, GraphQL\\\\Language\\\\AST\\\\ArgumentNode\\|null given on the left side\\.$#" count: 1 @@ -970,26 +835,11 @@ parameters: count: 1 path: src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 3 path: src/Validator/Rules/QueryComplexity.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Validator/Rules/QueryComplexity.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 2 - path: src/Validator/Rules/QuerySecurityRule.php - - message: "#^Only booleans are allowed in &&, GraphQL\\\\Type\\\\Definition\\\\Type\\|null given on the left side\\.$#" count: 1 @@ -1050,11 +900,6 @@ parameters: count: 1 path: src/Validator/Rules/UniqueVariableNames.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Validator/Rules/ValidationRule.php - - message: "#^Only booleans are allowed in \\|\\|, GraphQL\\\\Type\\\\Definition\\\\EnumType\\|GraphQL\\\\Type\\\\Definition\\\\InputObjectType\\|GraphQL\\\\Type\\\\Definition\\\\ListOfType\\|GraphQL\\\\Type\\\\Definition\\\\NonNull\\|GraphQL\\\\Type\\\\Definition\\\\ScalarType given on the left side\\.$#" count: 1 @@ -1115,16 +960,6 @@ parameters: count: 1 path: tests/Executor/DirectivesTest.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: tests/Executor/DirectivesTest.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 7 - path: tests/Executor/ExecutorLazySchemaTest.php - - message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\InterfaceType given\\.$#" count: 1 @@ -1165,11 +1000,6 @@ parameters: count: 4 path: tests/Language/VisitorTest.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: tests/Server/Psr7/PsrStreamStub.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 1 @@ -1200,8 +1030,3 @@ parameters: count: 2 path: tests/Utils/MixedStoreTest.php - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: tests/Utils/ValueFromAstTest.php - diff --git a/src/Error/Error.php b/src/Error/Error.php index 7e6056fc0..4509c140e 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -15,6 +15,7 @@ use function array_filter; use function array_map; use function array_values; +use function count; use function is_array; use function iterator_to_array; @@ -114,7 +115,7 @@ public function __construct( $this->source = $source; $this->positions = $positions; $this->path = $path; - $this->extensions = $extensions ?: ( + $this->extensions = count($extensions) > 0 ? $extensions : ( $previous && $previous instanceof self ? $previous->extensions : [] @@ -122,7 +123,8 @@ public function __construct( if ($previous instanceof ClientAware) { $this->isClientSafe = $previous->isClientSafe(); - $this->category = $previous->getCategory() ?: self::CATEGORY_INTERNAL; + $cat = $previous->getCategory(); + $this->category = $cat === '' || $cat === null ? self::CATEGORY_INTERNAL: $cat; } elseif ($previous) { $this->isClientSafe = false; $this->category = self::CATEGORY_INTERNAL; @@ -150,8 +152,8 @@ public static function createLocatedError($error, $nodes = null, $path = null) return $error; } - $nodes = $nodes ?: $error->nodes; - $path = $path ?: $error->path; + $nodes = $nodes ?? $error->nodes; + $path = $path ?? $error->path; } $source = $positions = $originalError = null; @@ -160,7 +162,7 @@ public static function createLocatedError($error, $nodes = null, $path = null) if ($error instanceof self) { $message = $error->getMessage(); $originalError = $error; - $nodes = $error->nodes ?: $nodes; + $nodes = $error->nodes ?? $nodes; $source = $error->source; $positions = $error->positions; $extensions = $error->extensions; @@ -172,7 +174,7 @@ public static function createLocatedError($error, $nodes = null, $path = null) } return new static( - $message ?: 'An unknown error occurred.', + $message === '' || $message === null ? 'An unknown error occurred.' : $message, $nodes, $source, $positions, diff --git a/src/Error/FormattedError.php b/src/Error/FormattedError.php index 46c781e26..64b5ea266 100644 --- a/src/Error/FormattedError.php +++ b/src/Error/FormattedError.php @@ -181,7 +181,7 @@ public static function createFromException($e, $debug = false, $internalErrorMes Utils::getVariableType($e) ); - $internalErrorMessage = $internalErrorMessage ?: self::$internalErrorMessage; + $internalErrorMessage = $internalErrorMessage === '' || $internalErrorMessage === null ? self::$internalErrorMessage : $internalErrorMessage; if ($e instanceof ClientAware) { $formattedError = [ @@ -284,7 +284,7 @@ public static function addDebugEntries(array $formattedError, $e, $debug) $isTrivial = $e instanceof Error && ! $e->getPrevious(); if (! $isTrivial) { - $debugging = $e->getPrevious() ?: $e; + $debugging = $e->getPrevious() ?? $e; $formattedError['trace'] = static::toSafeTrace($debugging); } } @@ -302,7 +302,7 @@ public static function addDebugEntries(array $formattedError, $e, $debug) */ public static function prepareFormatter(?callable $formatter = null, $debug) { - $formatter = $formatter ?: static function ($e) : array { + $formatter = $formatter ?? static function ($e) : array { return FormattedError::createFromException($e); }; if ($debug) { diff --git a/src/Error/Warning.php b/src/Error/Warning.php index f376ed9cd..3efd1cfeb 100644 --- a/src/Error/Warning.php +++ b/src/Error/Warning.php @@ -96,7 +96,7 @@ public static function enable($enable = true) : void public static function warnOnce(string $errorMessage, int $warningId, ?int $messageLevel = null) : void { - $messageLevel = $messageLevel ?: E_USER_WARNING; + $messageLevel = $messageLevel ?? E_USER_WARNING; if (self::$warningHandler !== null) { $fn = self::$warningHandler; @@ -109,7 +109,7 @@ public static function warnOnce(string $errorMessage, int $warningId, ?int $mess public static function warn(string $errorMessage, int $warningId, ?int $messageLevel = null) : void { - $messageLevel = $messageLevel ?: E_USER_WARNING; + $messageLevel = $messageLevel ?? E_USER_WARNING; if (self::$warningHandler !== null) { $fn = self::$warningHandler; diff --git a/src/Executor/ExecutionContext.php b/src/Executor/ExecutionContext.php index c510c30cf..4a57f20c4 100644 --- a/src/Executor/ExecutionContext.php +++ b/src/Executor/ExecutionContext.php @@ -64,7 +64,7 @@ public function __construct( $this->contextValue = $contextValue; $this->operation = $operation; $this->variableValues = $variableValues; - $this->errors = $errors ?: []; + $this->errors = $errors ?? []; $this->fieldResolver = $fieldResolver; $this->promiseAdapter = $promiseAdapter; } diff --git a/src/Executor/ExecutionResult.php b/src/Executor/ExecutionResult.php index 1fc47c8db..1331c64b0 100644 --- a/src/Executor/ExecutionResult.php +++ b/src/Executor/ExecutionResult.php @@ -139,7 +139,7 @@ public function toArray($debug = false) $result = []; if (! empty($this->errors)) { - $errorsHandler = $this->errorsHandler ?: static function (array $errors, callable $formatter) : array { + $errorsHandler = $this->errorsHandler ?? static function (array $errors, callable $formatter) : array { return array_map($formatter, $errors); }; diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index ee9618748..babe2f9e0 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -44,7 +44,7 @@ public static function setDefaultFieldResolver(callable $fieldResolver) public static function getPromiseAdapter() : PromiseAdapter { - return self::$defaultPromiseAdapter ?: (self::$defaultPromiseAdapter = new SyncPromiseAdapter()); + return self::$defaultPromiseAdapter ?? (self::$defaultPromiseAdapter = new SyncPromiseAdapter()); } public static function setPromiseAdapter(?PromiseAdapter $defaultPromiseAdapter = null) @@ -149,7 +149,7 @@ public static function promiseToExecute( $contextValue, $variableValues, $operationName, - $fieldResolver ?: self::$defaultFieldResolver + $fieldResolver ?? self::$defaultFieldResolver ); return $executor->doExecute(); diff --git a/src/Executor/Promise/Adapter/SyncPromise.php b/src/Executor/Promise/Adapter/SyncPromise.php index 29d41c36d..d0b94b18d 100644 --- a/src/Executor/Promise/Adapter/SyncPromise.php +++ b/src/Executor/Promise/Adapter/SyncPromise.php @@ -167,7 +167,7 @@ private function enqueueWaitingPromises() : void public static function getQueue() : SplQueue { - return self::$queue ?: self::$queue = new SplQueue(); + return self::$queue ?? self::$queue = new SplQueue(); } /** diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index afcb59434..1f6a33f42 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -167,8 +167,8 @@ private static function buildExecutionContext( if ($operation !== null) { [$coercionErrors, $coercedVariableValues] = Values::getVariableValues( $schema, - $operation->variableDefinitions ?: [], - $rawVariableValues ?: [] + $operation->variableDefinitions ?? [], + $rawVariableValues ?? [] ); if (empty($coercionErrors)) { $variableValues = $coercedVariableValues; @@ -582,9 +582,9 @@ private function resolveField(ObjectType $parentType, $rootValue, $fieldNodes, $ private function getFieldDef(Schema $schema, ObjectType $parentType, string $fieldName) : ?FieldDefinition { static $schemaMetaFieldDef, $typeMetaFieldDef, $typeNameMetaFieldDef; - $schemaMetaFieldDef = $schemaMetaFieldDef ?: Introspection::schemaMetaFieldDef(); - $typeMetaFieldDef = $typeMetaFieldDef ?: Introspection::typeMetaFieldDef(); - $typeNameMetaFieldDef = $typeNameMetaFieldDef ?: Introspection::typeNameMetaFieldDef(); + $schemaMetaFieldDef = $schemaMetaFieldDef ?? Introspection::schemaMetaFieldDef(); + $typeMetaFieldDef = $typeMetaFieldDef ?? Introspection::typeMetaFieldDef(); + $typeNameMetaFieldDef = $typeNameMetaFieldDef ?? Introspection::typeNameMetaFieldDef(); if ($fieldName === $schemaMetaFieldDef->name && $schema->getQueryType() === $parentType) { return $schemaMetaFieldDef; } diff --git a/src/Experimental/Executor/CoroutineExecutor.php b/src/Experimental/Executor/CoroutineExecutor.php index 5a073eb8e..cebef18c8 100644 --- a/src/Experimental/Executor/CoroutineExecutor.php +++ b/src/Experimental/Executor/CoroutineExecutor.php @@ -187,8 +187,8 @@ public function doExecute() : Promise [$errors, $coercedVariableValues] = Values::getVariableValues( $this->schema, - $this->collector->operation->variableDefinitions ?: [], - $this->rawVariableValues ?: [] + $this->collector->operation->variableDefinitions ?? [], + $this->rawVariableValues ?? [] ); if (! empty($errors)) { diff --git a/src/GraphQL.php b/src/GraphQL.php index 3b653b6ab..01e14d8d5 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -131,7 +131,7 @@ public static function promiseToExecute( if ($source instanceof DocumentNode) { $documentNode = $source; } else { - $documentNode = Parser::parse(new Source($source ?: '', 'GraphQL')); + $documentNode = Parser::parse(new Source($source ?? '', 'GraphQL')); } // FIXME diff --git a/src/Language/Lexer.php b/src/Language/Lexer.php index 14bdc580e..15563bb23 100644 --- a/src/Language/Lexer.php +++ b/src/Language/Lexer.php @@ -121,7 +121,7 @@ public function lookahead() $token = $this->token; if ($token->kind !== Token::EOF) { do { - $token = $token->next ?: ($token->next = $this->readToken($token)); + $token = $token->next ?? ($token->next = $this->readToken($token)); } while ($token->kind === Token::COMMENT); } @@ -793,7 +793,7 @@ private function readChars($charCount, $advance = false, $byteStreamPosition = n { $result = ''; $totalBytes = 0; - $byteOffset = $byteStreamPosition ?: $this->byteStreamPosition; + $byteOffset = $byteStreamPosition ?? $this->byteStreamPosition; for ($i = 0; $i < $charCount; $i++) { [$char, $code, $bytes] = $this->readChar(false, $byteOffset); diff --git a/src/Language/Parser.php b/src/Language/Parser.php index 0328fbdb3..1050ecd2c 100644 --- a/src/Language/Parser.php +++ b/src/Language/Parser.php @@ -351,7 +351,7 @@ private function expectKeyword(string $value) : Token private function unexpected(?Token $atToken = null) : SyntaxError { - $token = $atToken ?: $this->lexer->token; + $token = $atToken ?? $this->lexer->token; return new SyntaxError($this->lexer->source, $token->start, 'Unexpected ' . $token->getDescription()); } diff --git a/src/Language/Printer.php b/src/Language/Printer.php index 8e46ac329..7aa13b748 100644 --- a/src/Language/Printer.php +++ b/src/Language/Printer.php @@ -84,7 +84,7 @@ class Printer public static function doPrint($ast) { static $instance; - $instance = $instance ?: new static(); + $instance = $instance ?? new static(); return $instance->printAST($ast); } diff --git a/src/Language/Source.php b/src/Language/Source.php index 7e940305e..bd8794076 100644 --- a/src/Language/Source.php +++ b/src/Language/Source.php @@ -46,8 +46,8 @@ public function __construct($body, $name = null, ?SourceLocation $location = nul $this->body = $body; $this->length = mb_strlen($body, 'UTF-8'); - $this->name = $name ?: 'GraphQL request'; - $this->locationOffset = $location ?: new SourceLocation(1, 1); + $this->name = $name === '' || $name === null ? 'GraphQL request' : $name; + $this->locationOffset = $location ?? new SourceLocation(1, 1); Utils::invariant( $this->locationOffset->line > 0, diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index dc52a2347..cc3576fa8 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -186,7 +186,7 @@ class Visitor */ public static function visit($root, $visitor, $keyMap = null) { - $visitorKeys = $keyMap ?: self::$visitorKeys; + $visitorKeys = $keyMap ?? self::$visitorKeys; $stack = null; $inArray = $root instanceof NodeList || is_array($root); @@ -328,7 +328,7 @@ public static function visit($root, $visitor, $keyMap = null) ]; $inArray = $node instanceof NodeList || is_array($node); - $keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: []; + $keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?? []; $index = -1; $edits = []; if ($parent !== null) { diff --git a/src/Server/Helper.php b/src/Server/Helper.php index ce9bf1c5c..e950fc869 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -76,12 +76,12 @@ public function parseHttpRequest(?callable $readRawBodyFn = null) $rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); - $bodyParams = ['query' => $rawBody ?: '']; + $bodyParams = ['query' => $rawBody ?? '']; } elseif (stripos($contentType, 'application/json') !== false) { $rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); - $bodyParams = json_decode($rawBody ?: '', true); + $bodyParams = json_decode($rawBody ?? '', true); if (json_last_error()) { throw new RequestError('Could not parse JSON: ' . json_last_error_msg()); @@ -202,7 +202,7 @@ public function validateOperationParams(OperationParams $params) */ public function executeOperation(ServerConfig $config, OperationParams $op) { - $promiseAdapter = $config->getPromiseAdapter() ?: Executor::getPromiseAdapter(); + $promiseAdapter = $config->getPromiseAdapter() ?? Executor::getPromiseAdapter(); $result = $this->promiseToExecuteOperation($promiseAdapter, $config, $op); if ($promiseAdapter instanceof SyncPromiseAdapter) { @@ -224,7 +224,7 @@ public function executeOperation(ServerConfig $config, OperationParams $op) */ public function executeBatch(ServerConfig $config, array $operations) { - $promiseAdapter = $config->getPromiseAdapter() ?: Executor::getPromiseAdapter(); + $promiseAdapter = $config->getPromiseAdapter() ?? Executor::getPromiseAdapter(); $result = []; foreach ($operations as $operation) { diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index a976ec072..67c38d60e 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -102,7 +102,7 @@ public static function create(array $params, bool $readonly = false) : Operation } $instance->query = $params['query']; - $instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id']; + $instance->queryId = $params['queryid'] ?? $params['documentid'] ?? $params['id']; $instance->operation = $params['operationname']; $instance->variables = $params['variables']; $instance->extensions = $params['extensions']; diff --git a/src/Type/Definition/FieldDefinition.php b/src/Type/Definition/FieldDefinition.php index 7bf0800b6..39392cd53 100644 --- a/src/Type/Definition/FieldDefinition.php +++ b/src/Type/Definition/FieldDefinition.php @@ -164,7 +164,7 @@ public static function defaultComplexity($childrenComplexity) */ public function getArg($name) { - foreach ($this->args ?: [] as $arg) { + foreach ($this->args ?? [] as $arg) { /** @var FieldArgument $arg */ if ($arg->name === $name) { return $arg; diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 5e031df2f..ce6097a09 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -195,7 +195,7 @@ public function getInterfaces() ); } - $this->interfaces = $interfaces ?: []; + $this->interfaces = $interfaces ?? []; } return $this->interfaces; diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index fab7ecd0c..0178a13ec 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -661,8 +661,8 @@ public static function _directive() ], 'args' => [ 'type' => Type::nonNull(Type::listOf(Type::nonNull(self::_inputValue()))), - 'resolve' => static function (Directive $directive) : array { - return $directive->args ?: []; + 'resolve' => static function (Directive $directive) { + return $directive->args ?? []; }, ], ], diff --git a/src/Type/Schema.php b/src/Type/Schema.php index 610d5e944..e69b018db 100644 --- a/src/Type/Schema.php +++ b/src/Type/Schema.php @@ -158,7 +158,7 @@ public function __construct($config) */ private function resolveAdditionalTypes() { - $types = $this->config->types ?: []; + $types = $this->config->types ?? []; if (is_callable($types)) { $types = $types(); diff --git a/src/Type/SchemaValidationContext.php b/src/Type/SchemaValidationContext.php index b37b0990d..3ac18ede6 100644 --- a/src/Type/SchemaValidationContext.php +++ b/src/Type/SchemaValidationContext.php @@ -528,7 +528,7 @@ private function getAllNodes($obj) ? ($extensionNodes ? array_merge([$astNode], $extensionNodes) : [$astNode]) - : ($extensionNodes ?: []); + : ($extensionNodes ?? []); } /** diff --git a/src/Utils/BreakingChangesFinder.php b/src/Utils/BreakingChangesFinder.php index 8ef9253d2..3eec2b071 100644 --- a/src/Utils/BreakingChangesFinder.php +++ b/src/Utils/BreakingChangesFinder.php @@ -691,7 +691,7 @@ public static function findRemovedArgsForDirectives(Directive $oldDirective, Dir private static function getArgumentMapForDirective(Directive $directive) { return Utils::keyMap( - $directive->args ?: [], + $directive->args ?? [], static function ($arg) { return $arg->name; } diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 870180115..ef1f02301 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -317,7 +317,7 @@ public function enter(Node $node) break; case $node instanceof ArgumentNode: - $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef(); + $fieldOrDirective = $this->getDirective() ?? $this->getFieldDef(); $argDef = $argType = null; if ($fieldOrDirective) { /** @var FieldArgument $argDef */ diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 7919f262e..0e6c18641 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -58,7 +58,7 @@ public static function undefined() { static $undefined; - return $undefined ?: $undefined = new stdClass(); + return $undefined ?? $undefined = new stdClass(); } /** diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index 0b5fb0c1b..fa94dc4b1 100644 --- a/src/Validator/DocumentValidator.php +++ b/src/Validator/DocumentValidator.php @@ -110,7 +110,7 @@ public static function validate( return []; } - $typeInfo = $typeInfo ?: new TypeInfo($schema); + $typeInfo = $typeInfo ?? new TypeInfo($schema); return static::visitUsingRules($schema, $typeInfo, $ast, $rules); } diff --git a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php index 790c28cf9..1817d0144 100644 --- a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php +++ b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php @@ -380,7 +380,7 @@ private function findConflict( ]; } - if (! $this->sameArguments($ast1->arguments ?: [], $ast2->arguments ?: [])) { + if (! $this->sameArguments($ast1->arguments ?? [], $ast2->arguments ?? [])) { return [ [$responseName, 'they have differing arguments'], [$ast1], diff --git a/src/Validator/Rules/ProvidedRequiredArguments.php b/src/Validator/Rules/ProvidedRequiredArguments.php index 41730ebe2..930163023 100644 --- a/src/Validator/Rules/ProvidedRequiredArguments.php +++ b/src/Validator/Rules/ProvidedRequiredArguments.php @@ -26,7 +26,7 @@ public function getVisitor(ValidationContext $context) if (! $fieldDef) { return Visitor::skipNode(); } - $argNodes = $fieldNode->arguments ?: []; + $argNodes = $fieldNode->arguments ?? []; $argNodeMap = []; foreach ($argNodes as $argNode) { @@ -53,7 +53,7 @@ public function getVisitor(ValidationContext $context) if (! $directiveDef) { return Visitor::skipNode(); } - $argNodes = $directiveNode->arguments ?: []; + $argNodes = $directiveNode->arguments ?? []; $argNodeMap = []; foreach ($argNodes as $argNode) { $argNodeMap[$argNode->name->value] = $argNodes; diff --git a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php index 2eacdc202..3c7c84b08 100644 --- a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php +++ b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php @@ -92,7 +92,7 @@ static function (NamedTypeNode $argument) : string { return null; } - $argNodes = $directiveNode->arguments ?: []; + $argNodes = $directiveNode->arguments ?? []; $argNodeMap = Utils::keyMap( $argNodes, static function (ArgumentNode $arg) : string { diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index b084799b3..78d3e11d0 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -232,7 +232,7 @@ public function getRawVariableValues() */ public function setRawVariableValues(?array $rawVariableValues = null) { - $this->rawVariableValues = $rawVariableValues ?: []; + $this->rawVariableValues = $rawVariableValues ?? []; } private function buildFieldArguments(FieldNode $node) diff --git a/src/Validator/Rules/QuerySecurityRule.php b/src/Validator/Rules/QuerySecurityRule.php index c6e5b460d..1ff2f508a 100644 --- a/src/Validator/Rules/QuerySecurityRule.php +++ b/src/Validator/Rules/QuerySecurityRule.php @@ -109,8 +109,8 @@ protected function collectFieldASTsAndDefs( ?ArrayObject $visitedFragmentNames = null, ?ArrayObject $astAndDefs = null ) { - $_visitedFragmentNames = $visitedFragmentNames ?: new ArrayObject(); - $_astAndDefs = $astAndDefs ?: new ArrayObject(); + $_visitedFragmentNames = $visitedFragmentNames ?? new ArrayObject(); + $_astAndDefs = $astAndDefs ?? new ArrayObject(); foreach ($selectionSet->selections as $selection) { switch (true) { diff --git a/src/Validator/Rules/ValidationRule.php b/src/Validator/Rules/ValidationRule.php index cf38cd138..461aafeeb 100644 --- a/src/Validator/Rules/ValidationRule.php +++ b/src/Validator/Rules/ValidationRule.php @@ -15,7 +15,7 @@ abstract class ValidationRule public function getName() { - return $this->name ?: static::class; + return $this->name === '' || $this->name === null ? static::class : $this->name; } public function __invoke(ValidationContext $context) diff --git a/tests/Executor/DirectivesTest.php b/tests/Executor/DirectivesTest.php index 84aa491c8..fda71bb43 100644 --- a/tests/Executor/DirectivesTest.php +++ b/tests/Executor/DirectivesTest.php @@ -20,8 +20,11 @@ class DirectivesTest extends TestCase /** @var Schema */ private static $schema; - /** @var string[] */ - private static $data; + /** @var array */ + private static $data = [ + 'a' => 'a', + 'b' => 'b', + ]; /** * @see it('basic query works') @@ -38,7 +41,7 @@ public function testWorksWithoutDirectives() : void */ private function executeTestQuery($doc) : array { - return Executor::execute(self::getSchema(), Parser::parse($doc), self::getData())->toArray(); + return Executor::execute(self::getSchema(), Parser::parse($doc), self::$data)->toArray(); } private static function getSchema() : Schema @@ -58,17 +61,6 @@ private static function getSchema() : Schema return self::$schema; } - /** - * @return string[] - */ - private static function getData() : array - { - return self::$data ?: (self::$data = [ - 'a' => 'a', - 'b' => 'b', - ]); - } - public function testWorksOnScalars() : void { // if true includes scalar diff --git a/tests/Executor/ExecutorLazySchemaTest.php b/tests/Executor/ExecutorLazySchemaTest.php index 9d9878531..43f94bee9 100644 --- a/tests/Executor/ExecutorLazySchemaTest.php +++ b/tests/Executor/ExecutorLazySchemaTest.php @@ -257,7 +257,7 @@ public function loadType($name, $isExecutorCall = false) switch ($name) { case 'Query': - return $this->queryType ?: $this->queryType = new ObjectType([ + return $this->queryType ?? $this->queryType = new ObjectType([ 'name' => 'Query', 'fields' => function () : array { $this->calls[] = 'Query.fields'; @@ -269,7 +269,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'SomeObject': - return $this->someObjectType ?: $this->someObjectType = new ObjectType([ + return $this->someObjectType ?? $this->someObjectType = new ObjectType([ 'name' => 'SomeObject', 'fields' => function () : array { $this->calls[] = 'SomeObject.fields'; @@ -288,7 +288,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'OtherObject': - return $this->otherObjectType ?: $this->otherObjectType = new ObjectType([ + return $this->otherObjectType ?? $this->otherObjectType = new ObjectType([ 'name' => 'OtherObject', 'fields' => function () : array { $this->calls[] = 'OtherObject.fields'; @@ -300,7 +300,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'DeeperObject': - return $this->deeperObjectType ?: $this->deeperObjectType = new ObjectType([ + return $this->deeperObjectType ?? $this->deeperObjectType = new ObjectType([ 'name' => 'DeeperObject', 'fields' => function () : array { return [ @@ -309,7 +309,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'SomeScalar': - return $this->someScalarType ?: $this->someScalarType = new CustomScalarType([ + return $this->someScalarType ?? $this->someScalarType = new CustomScalarType([ 'name' => 'SomeScalar', 'serialize' => static function ($value) { return $value; @@ -321,7 +321,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'SomeUnion': - return $this->someUnionType ?: $this->someUnionType = new UnionType([ + return $this->someUnionType ?? $this->someUnionType = new UnionType([ 'name' => 'SomeUnion', 'resolveType' => function () { $this->calls[] = 'SomeUnion.resolveType'; @@ -335,7 +335,7 @@ public function loadType($name, $isExecutorCall = false) }, ]); case 'SomeInterface': - return $this->someInterfaceType ?: $this->someInterfaceType = new InterfaceType([ + return $this->someInterfaceType ?? $this->someInterfaceType = new InterfaceType([ 'name' => 'SomeInterface', 'resolveType' => function () { $this->calls[] = 'SomeInterface.resolveType'; diff --git a/tests/Server/Psr7/PsrStreamStub.php b/tests/Server/Psr7/PsrStreamStub.php index 736ada980..1865dfedd 100644 --- a/tests/Server/Psr7/PsrStreamStub.php +++ b/tests/Server/Psr7/PsrStreamStub.php @@ -13,6 +13,7 @@ */ class PsrStreamStub implements StreamInterface { + /** @var string */ public $content; /** @@ -27,9 +28,8 @@ class PsrStreamStub implements StreamInterface * string casting operations. * * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring - * @return string */ - public function __toString() + public function __toString() : string { return $this->content; } @@ -59,11 +59,10 @@ public function detach() /** * Get the size of the stream if known. * - * @return int|null Returns the size in bytes if known, or null if unknown. */ - public function getSize() + public function getSize() : ?int { - return strlen($this->content ?: ''); + return $this->content === null ? null : strlen($this->content); } /** @@ -185,7 +184,7 @@ public function read($length) * @throws \RuntimeException if unable to read or an error occurs while * reading. */ - public function getContents() + public function getContents() : string { return $this->content; } diff --git a/tests/Utils/ValueFromAstTest.php b/tests/Utils/ValueFromAstTest.php index ee3f6a843..310df55f1 100644 --- a/tests/Utils/ValueFromAstTest.php +++ b/tests/Utils/ValueFromAstTest.php @@ -183,7 +183,7 @@ public function testCoercesInputObjectsAccordingToInputCoercionRules() : void private function inputObj() { - return $this->inputObj ?: $this->inputObj = new InputObjectType([ + return $this->inputObj ?? $this->inputObj = new InputObjectType([ 'name' => 'TestInput', 'fields' => [ 'int' => ['type' => Type::int(), 'defaultValue' => 42],