Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpstan Level 3 #566

Merged
merged 84 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
5c8adc3
phpstan level 3
shmax Oct 24, 2019
36e8eec
cleanup
shmax Oct 27, 2019
c323c16
linting, remove array shape stuff
shmax Oct 27, 2019
e4352c0
use protected
shmax Oct 27, 2019
f091d91
use concrete type
shmax Oct 27, 2019
87ef68c
remove null type
shmax Oct 27, 2019
9edeb73
add null
shmax Oct 27, 2019
0ad36dd
use null syntax over ?
shmax Oct 27, 2019
e381be8
address comments
shmax Oct 27, 2019
8574398
Update src/Validator/Rules/UniqueInputFieldNames.php
shmax Oct 27, 2019
ba2e5b2
Update src/Type/Definition/Type.php
shmax Oct 27, 2019
8c69f28
Update src/Validator/Rules/UniqueInputFieldNames.php
shmax Oct 27, 2019
5ae5048
Update src/Validator/Rules/FieldsOnCorrectType.php
shmax Oct 27, 2019
09a4c47
Update src/Utils/TypeInfo.php
shmax Oct 27, 2019
dd2b0ae
Update src/Type/Definition/Type.php
shmax Oct 27, 2019
35a0582
remove assignment
shmax Oct 27, 2019
2b93597
Update src/Type/Introspection.php
shmax Oct 27, 2019
cdd4cf5
Update src/Type/Schema.php
shmax Oct 27, 2019
586155e
Update src/Type/Schema.php
shmax Oct 27, 2019
8a2be47
restore return annoation
shmax Oct 27, 2019
f16085d
add return types
shmax Oct 27, 2019
2fda671
restore return annotation
shmax Oct 27, 2019
9804ac8
add null
shmax Oct 27, 2019
83fd01b
simplify
shmax Oct 27, 2019
77a2f4f
more cleanup to appease scrutinizer
shmax Oct 27, 2019
6665c39
scrutinizer workaround
shmax Oct 27, 2019
d2a3728
more scrutinizer experiments
shmax Oct 27, 2019
ef8c2c4
remove dead code
shmax Oct 27, 2019
04a2462
simplify annotation
shmax Oct 27, 2019
778a918
null check
shmax Oct 27, 2019
3333f34
broaden type
shmax Oct 27, 2019
605211a
remove null
shmax Oct 27, 2019
f2e9efe
add return type
shmax Oct 28, 2019
e322b5e
cleanup
shmax Oct 28, 2019
8130d36
add return types
shmax Oct 28, 2019
af620eb
try alternate order
shmax Oct 28, 2019
58afe78
concrete check, annotate member
shmax Oct 28, 2019
c12e6b7
null check
shmax Oct 28, 2019
2aa9d52
handle operation === false
shmax Oct 28, 2019
0ea884f
remove return types from parseLiteral (to match interface signature)
shmax Oct 28, 2019
bf53a29
downgrade type (bug in code_sniffer)
shmax Oct 28, 2019
5fb5e22
workaround for code_sniffer parser bug
shmax Oct 28, 2019
0052b70
add return hinting
shmax Oct 28, 2019
a1cbfd8
implement interface
shmax Oct 28, 2019
583946c
implement interface
shmax Oct 28, 2019
422dc57
deep array convert
shmax Oct 28, 2019
27c448f
fix coroutine tests
shmax Oct 28, 2019
f2a26dc
move extensions
shmax Oct 28, 2019
75562e6
add final
shmax Oct 28, 2019
4e73336
Update docs/type-system/scalar-types.md
shmax Oct 28, 2019
5ac9cc7
Update src/Type/Schema.php
shmax Oct 28, 2019
8c4316e
Update src/Type/Definition/NonNull.php
shmax Oct 28, 2019
18f12bd
Update examples/01-blog/Blog/Type/Scalar/UrlType.php
shmax Oct 29, 2019
a84dba0
address comments
shmax Oct 29, 2019
74bea2a
improve coverage
shmax Oct 29, 2019
7d30c26
rebase on master
shmax Oct 29, 2019
8ba9489
increase coverage
shmax Oct 29, 2019
6aaee8e
linting
shmax Oct 29, 2019
915a062
Update src/Validator/Rules/KnownArgumentNames.php
shmax Oct 30, 2019
31cc867
undo change
shmax Oct 30, 2019
2f69e2b
Merge branch 'stan-level-3' of /~https://github.com/shmax/graphql-php i…
shmax Oct 30, 2019
a2b49ef
try workaround for scrutinizer bug
shmax Oct 31, 2019
1a75776
try swapping the order
shmax Oct 31, 2019
64d45fe
restore weird syntax to appease scrutinizer
shmax Nov 1, 2019
7f39453
remove null
shmax Nov 1, 2019
889db9b
use alt null syntax
shmax Nov 1, 2019
a6de2a2
add space
shmax Nov 1, 2019
fdadd5f
restore some return types
shmax Nov 1, 2019
7142c3d
code sniffer fixes
shmax Nov 1, 2019
6f00407
use alt null syntax
shmax Nov 1, 2019
0c0af8e
return type hint
shmax Nov 1, 2019
519321a
return type hint
shmax Nov 1, 2019
f836716
more typehinting
shmax Nov 1, 2019
1185c33
refine type
shmax Nov 2, 2019
2c70456
revert
shmax Nov 2, 2019
d9f2ede
narrow to ObjectType
shmax Nov 2, 2019
bd650ab
remove instanceOf check
shmax Nov 2, 2019
0807bb5
remove unused imports
shmax Nov 2, 2019
e1e5b08
simplify type
shmax Nov 2, 2019
bebaad9
restore null init
shmax Nov 2, 2019
3b428f7
restore isInputType check
shmax Nov 2, 2019
78295d7
restore check
shmax Nov 2, 2019
801033f
remove check
shmax Nov 2, 2019
18ed4af
revert deferredfieldstest kookiness
shmax Nov 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/type-system/scalar-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class EmailType extends ScalarType
* @return string
* @throws Error
*/
public function parseLiteral($valueNode, array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
// error location in query:
Expand Down
2 changes: 1 addition & 1 deletion examples/01-blog/Blog/Type/Scalar/UrlType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function parseValue($value)
* @return null|string
shmax marked this conversation as resolved.
Show resolved Hide resolved
* @throws Error
*/
public function parseLiteral($valueNode, array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
// error location in query:
Expand Down
26 changes: 22 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
parameters:
level: 2
level: 3

reportUnmatchedIgnoredErrors: false

inferPrivatePropertyTypeFromConstructor: true

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

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon

services:
-
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsInputTypeStaticMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
-
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsOutputTypeStaticMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
-
class: GraphQL\Tests\PhpStan\Type\Definition\Type\IsCompositeTypeStaticMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
2 changes: 2 additions & 0 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ public function getExtensions()
* @deprecated Use FormattedError::createFromException() instead
*
* @return mixed[]
*
* @codeCoverageIgnore
shmax marked this conversation as resolved.
Show resolved Hide resolved
*/
public function toSerializableArray()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Error/FormattedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ static function ($loc) {
* @deprecated as of v0.10.0, use general purpose method createFromException() instead
*
* @return mixed[]
*
* @codeCoverageIgnore
*/
public static function createFromPHPError(ErrorException $e)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private function buildResponse($data)
*
* @param mixed $rootValue
*
* @return Promise|stdClass|mixed[]
* @return Promise|stdClass|mixed[]|null
*/
private function executeOperation(OperationDefinitionNode $operation, $rootValue)
{
Expand Down Expand Up @@ -993,7 +993,7 @@ private function completeAbstractValue(AbstractType $returnType, $fieldNodes, Re
* @param mixed|null $contextValue
* @param InterfaceType|UnionType $abstractType
*
* @return ObjectType|Promise|null
* @return Promise|Type|string|null
*/
private function defaultTypeResolver($value, $contextValue, ResolveInfo $info, AbstractType $abstractType)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Executor/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ public static function getArgumentValuesForMap($fieldDefinition, $argumentValueM
* @param mixed[]|null $variables
*
* @return mixed[]|stdClass|null
*
* @codeCoverageIgnore
*/
public static function valueFromAST(ValueNode $valueNode, InputType $type, ?array $variables = null)
{
Expand All @@ -316,6 +318,7 @@ public static function valueFromAST(ValueNode $valueNode, InputType $type, ?arra
*
* @return string[]
*
* @codeCoverageIgnore
* @paarm ScalarType|EnumType|InputObjectType|ListOfType|NonNull $type
*/
public static function isValidPHPValue($value, InputType $type)
Expand Down
2 changes: 1 addition & 1 deletion src/Experimental/Executor/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Collector
/** @var FieldNode[][] */
private $fields;

/** @var string[] */
/** @var array<string, bool> */
vladar marked this conversation as resolved.
Show resolved Hide resolved
private $visitedFragments;

public function __construct(Schema $schema, Runtime $runtime)
Expand Down
2 changes: 1 addition & 1 deletion src/Experimental/Executor/CoroutineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CoroutineContext
/** @var string[] */
public $path;

/** @var ResolveInfo|null */
/** @var ResolveInfo */
public $resolveInfo;

/** @var string[]|null */
Expand Down
40 changes: 26 additions & 14 deletions src/Experimental/Executor/CoroutineExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
use GraphQL\Type\Introspection;
use GraphQL\Type\Schema;
use GraphQL\Utils\AST;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
use SplQueue;
use stdClass;
use Throwable;
use function is_array;
use function is_string;
use function json_decode;
use function json_encode;
use function sprintf;

class CoroutineExecutor implements Runtime, ExecutorImplementation
Expand Down Expand Up @@ -73,10 +76,10 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation
/** @var string|null */
private $operationName;

/** @var Collector */
/** @var Collector|null */
private $collector;

/** @var Error[] */
/** @var array<Error> */
private $errors;

/** @var SplQueue */
Expand All @@ -85,10 +88,10 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation
/** @var SplQueue */
private $schedule;

/** @var stdClass */
/** @var stdClass|null */
private $rootResult;

/** @var int */
/** @var int|null */
private $pending;

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

$this->errors = [];
$this->queue = new SplQueue();
$this->schedule = new SplQueue();
$this->schema = $schema;
$this->fieldResolver = $fieldResolver;
$this->promiseAdapter = $promiseAdapter;
Expand Down Expand Up @@ -143,10 +149,11 @@ public static function create(
private static function resultToArray($value, $emptyObjectAsStdClass = true)
{
if ($value instanceof stdClass) {
$array = [];
foreach ($value as $propertyName => $propertyValue) {
$array = (array) $value;
foreach ($array as $propertyName => $propertyValue) {
vladar marked this conversation as resolved.
Show resolved Hide resolved
$array[$propertyName] = self::resultToArray($propertyValue);
}

if ($emptyObjectAsStdClass && empty($array)) {
return new stdClass();
}
Expand Down Expand Up @@ -237,9 +244,9 @@ public function doExecute() : Promise
private function finishExecute($value, array $errors) : ExecutionResult
{
$this->rootResult = null;
$this->errors = null;
$this->queue = null;
$this->schedule = null;
$this->errors = [];
$this->queue = new SplQueue();
$this->schedule = new SplQueue();
$this->pending = null;
$this->collector = null;
$this->variableValues = null;
Expand Down Expand Up @@ -825,11 +832,16 @@ private function completeValue(CoroutineContext $ctx, Type $type, $value, array
} else {
$childContexts = [];

$fields = [];
if ($this->collector !== null) {
$fields = $this->collector->collectFields(
$objectType,
$ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx)
);
}

/** @var CoroutineContextShared $childShared */
foreach ($this->collector->collectFields(
$objectType,
$ctx->shared->mergedSelectionSet ?? $this->mergeSelectionSets($ctx)
) as $childShared) {
foreach ($fields as $childShared) {
$childPath = $path;
$childPath[] = $childShared->resultName; // !!! uses array COW semantics
$childCtx = new CoroutineContext(
Expand Down Expand Up @@ -938,7 +950,7 @@ private function resolveTypeSlow(CoroutineContext $ctx, $value, AbstractType $ab
$selectedType = null;
foreach ($possibleTypes as $type) {
$typeCheck = yield $type->isTypeOf($value, $this->contextValue, $ctx->resolveInfo);
if ($selectedType !== null || $typeCheck !== true) {
if ($selectedType !== null || ! $typeCheck) {
continue;
}

Expand Down
9 changes: 8 additions & 1 deletion src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema as SchemaType;
use GraphQL\Validator\DocumentValidator;
Expand Down Expand Up @@ -182,6 +183,8 @@ public static function promiseToExecute(
* @param mixed[]|null $variableValues
*
* @return Promise|mixed[]
*
* @codeCoverageIgnore
*/
public static function execute(
SchemaType $schema,
Expand Down Expand Up @@ -227,6 +230,8 @@ public static function execute(
* @param mixed[]|null $variableValues
*
* @return ExecutionResult|Promise
*
* @codeCoverageIgnore
*/
public static function executeAndReturnResult(
SchemaType $schema,
Expand Down Expand Up @@ -287,7 +292,7 @@ public static function getStandardTypes() : array
* Replaces standard types with types from this list (matching by name)
* Standard types not listed here remain untouched.
*
* @param Type[] $types
* @param array<string, ScalarType> $types
*
* @api
*/
Expand Down Expand Up @@ -345,6 +350,8 @@ public static function useReferenceExecutor()
* @deprecated Renamed to getStandardDirectives
*
* @return Directive[]
*
* @codeCoverageIgnore
*/
public static function getInternalDirectives() : array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/DocumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class DocumentNode extends Node
/** @var string */
public $kind = NodeKind::DOCUMENT;

/** @var NodeList|DefinitionNode[] */
/** @var NodeList|array<DefinitionNode> */
vladar marked this conversation as resolved.
Show resolved Hide resolved
public $definitions;
}
7 changes: 1 addition & 6 deletions src/Language/DirectiveLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ class DirectiveLocation
self::INPUT_FIELD_DEFINITION => self::INPUT_FIELD_DEFINITION,
];

/**
* @param string $name
*
* @return bool
*/
public static function has($name)
public static function has(string $name) : bool
{
return isset(self::$locations[$name]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ private function parseImplementsInterfaces()
}

/**
* @return FieldDefinitionNode[]|NodeList
* @return array<FieldDefinitionNode>|NodeList
*
* @throws SyntaxError
*/
Expand Down
11 changes: 3 additions & 8 deletions src/Language/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,13 @@ class Token
*/
public $prev;

/** @var Token */
/** @var Token|null */
public $next;

/**
* @param string $kind
* @param int $start
* @param int $end
* @param int $line
* @param int $column
* @param mixed|null $value
* @param mixed $value
*/
public function __construct($kind, $start, $end, $line, $column, ?Token $previous = null, $value = null)
public function __construct(string $kind, int $start, int $end, int $line, int $column, ?Token $previous = null, $value = null)
simPod marked this conversation as resolved.
Show resolved Hide resolved
{
$this->kind = $kind;
$this->start = $start;
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public static function removeNode()
/**
* @param callable[][] $visitors
*
* @return callable[][]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should keep

* @return array<string, callable>
*/
public static function visitInParallel($visitors)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function parseRequestParams($method, array $bodyParams, array $queryParam
* Checks validity of OperationParams extracted from HTTP request and returns an array of errors
* if params are invalid (or empty array when params are valid)
*
* @return Error[]
* @return array<int, RequestError>
*
* @api
*/
Expand Down Expand Up @@ -285,6 +285,11 @@ static function (RequestError $err) {
}

$operationType = AST::getOperation($doc, $op->operation);

if ($operationType === false) {
throw new RequestError('Failed to determine operation type');
}

if ($operationType !== 'query' && $op->isReadOnly()) {
throw new RequestError('GET supports only query operation');
}
Expand Down Expand Up @@ -385,11 +390,9 @@ private function resolveValidationRules(
}

/**
* @param string $operationType
*
* @return mixed
*/
private function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $doc, $operationType)
private function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $doc, string $operationType)
{
$rootValue = $config->getRootValue();

Expand Down
5 changes: 1 addition & 4 deletions src/Type/Definition/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ public function parseValue($value)
}

/**
* @param Node $valueNode
* @param mixed[]|null $variables
*
* @return bool|null
*
* @throws Exception
*/
public function parseLiteral($valueNode, ?array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if (! $valueNode instanceof BooleanValueNode) {
// Intentionally without message, as all information already in wrapped Exception
Expand Down
Loading