Skip to content

Commit

Permalink
chore(deps): update phpstan packages (#1322)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Benedikt Franke <benedikt.franke@mll.com>
  • Loading branch information
renovate[bot] and spawnia committed Feb 24, 2023
1 parent 75d647a commit b9cfc95
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 47 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v15.2.0

### Changed

- Improve validation messages for type definitions

## v15.1.0

### Added
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"nyholm/psr7": "^1.5",
"phpbench/phpbench": "^1.2",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "1.9.18",
"phpstan/phpstan-phpunit": "1.3.4",
"phpstan/phpstan-strict-rules": "1.4.5",
"phpstan/phpstan": "1.10.2",
"phpstan/phpstan-phpunit": "1.3.7",
"phpstan/phpstan-strict-rules": "1.5.0",
"phpunit/phpunit": "^9.5",
"psr/http-message": "^1",
"react/http": "^1.6",
Expand Down
38 changes: 30 additions & 8 deletions src/Type/Definition/CustomScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GraphQL\Language\AST\ScalarTypeExtensionNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Utils\AST;
use GraphQL\Utils\Utils;

/**
* @phpstan-type InputCustomScalarConfig array{
Expand Down Expand Up @@ -77,18 +78,39 @@ public function assertValid(): void
{
parent::assertValid();

if (isset($this->config['serialize']) && ! \is_callable($this->config['serialize'])) {
throw new InvariantViolation("{$this->name} must provide \"serialize\" function. If this custom Scalar is also used as an input type, ensure \"parseValue\" and \"parseLiteral\" functions are also provided.");
}

$serialize = $this->config['serialize'] ?? null;
$parseValue = $this->config['parseValue'] ?? null;
$parseLiteral = $this->config['parseLiteral'] ?? null;
if ($parseValue === null && $parseLiteral === null) {
return;

$hasSerialize = $serialize !== null;
$hasParseValue = $parseValue !== null;
$hasParseLiteral = $parseLiteral !== null;
$hasParse = $hasParseValue && $hasParseLiteral;

if ($hasParseValue !== $hasParseLiteral) {
throw new InvariantViolation("{$this->name} must provide both \"parseValue\" and \"parseLiteral\" functions to work as an input type.");
}

if (! $hasSerialize && ! $hasParse) {
throw new InvariantViolation("{$this->name} must provide \"parseValue\" and \"parseLiteral\" functions, \"serialize\" function, or both.");
}

// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if ($hasSerialize && ! is_callable($serialize)) {
$notCallable = Utils::printSafe($serialize);
throw new InvariantViolation("{$this->name} must provide \"serialize\" as a callable if given, but got: {$notCallable}.");
}

// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if ($hasParseValue && ! is_callable($parseValue)) {
$notCallable = Utils::printSafe($parseValue);
throw new InvariantViolation("{$this->name} must provide \"parseValue\" as a callable if given, but got: {$notCallable}.");
}

if (! \is_callable($parseValue) || ! \is_callable($parseLiteral)) {
throw new InvariantViolation("{$this->name} must provide both \"parseValue\" and \"parseLiteral\" functions.");
// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if ($hasParseLiteral && ! is_callable($parseLiteral)) {
$notCallable = Utils::printSafe($parseLiteral);
throw new InvariantViolation("{$this->name} must provide \"parseLiteral\" as a callable if given, but got: {$notCallable}.");
}
}
}
3 changes: 1 addition & 2 deletions src/Type/Definition/Deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ class Deprecated
{
public function __construct(
public string $reason = Directive::DEFAULT_DEPRECATION_REASON,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Type/Definition/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ class Description
{
public function __construct(
public string $description,
) {
}
) {}
}
5 changes: 3 additions & 2 deletions src/Type/Definition/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,13 @@ public function assertValid(Type $parentType): void

if (! $type instanceof OutputType) {
$safeType = Utils::printSafe($this->type);
throw new InvariantViolation("{$parentType->name}.{$this->name} field type must be Output Type but got: {$safeType}");
throw new InvariantViolation("{$parentType->name}.{$this->name} field type must be Output Type but got: {$safeType}.");
}

// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if ($this->resolveFn !== null && ! \is_callable($this->resolveFn)) {
$safeResolveFn = Utils::printSafe($this->resolveFn);
throw new InvariantViolation("{$parentType->name}.{$this->name} field resolver must be a function if provided, but got: {$safeResolveFn}");
throw new InvariantViolation("{$parentType->name}.{$this->name} field resolver must be a function if provided, but got: {$safeResolveFn}.");
}

foreach ($this->args as $fieldArgument) {
Expand Down
8 changes: 5 additions & 3 deletions src/Type/Definition/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public function assertValid(): void
{
Utils::assertValidName($this->name);

if (isset($this->config['resolveType']) && ! \is_callable($this->config['resolveType'])) {
$notCallable = Utils::printSafe($this->config['resolveType']);
throw new InvariantViolation("{$this->name} must provide \"resolveType\" as a callable, but got: {$notCallable}");
$resolveType = $this->config['resolveType'] ?? null;
// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if ($resolveType !== null && ! \is_callable($resolveType)) {
$notCallable = Utils::printSafe($resolveType);
throw new InvariantViolation("{$this->name} must provide \"resolveType\" as null or a callable, but got: {$notCallable}.");
}

$this->assertValidInterfaces();
Expand Down
8 changes: 5 additions & 3 deletions src/Type/Definition/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ public function assertValid(): void
{
Utils::assertValidName($this->name);

if (isset($this->config['isTypeOf']) && ! \is_callable($this->config['isTypeOf'])) {
$notCallable = Utils::printSafe($this->config['isTypeOf']);
throw new InvariantViolation("{$this->name} must provide \"isTypeOf\" as a callable, but got: {$notCallable}");
$isTypeOf = $this->config['isTypeOf'] ?? null;
// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if (isset($isTypeOf) && ! \is_callable($isTypeOf)) {
$notCallable = Utils::printSafe($isTypeOf);
throw new InvariantViolation("{$this->name} must provide \"isTypeOf\" as null or a callable, but got: {$notCallable}.");
}

foreach ($this->getFields() as $field) {
Expand Down
8 changes: 5 additions & 3 deletions src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ public function assertValid(): void
{
Utils::assertValidName($this->name);

if (isset($this->config['resolveType']) && ! \is_callable($this->config['resolveType'])) {
$notCallable = Utils::printSafe($this->config['resolveType']);
throw new InvariantViolation("{$this->name} must provide \"resolveType\" as a callable, but got: {$notCallable}");
$resolveType = $this->config['resolveType'] ?? null;
// @phpstan-ignore-next-line not necessary according to types, but can happen during runtime
if (isset($resolveType) && ! \is_callable($resolveType)) {
$notCallable = Utils::printSafe($resolveType);
throw new InvariantViolation("{$this->name} must provide \"resolveType\" as null or a callable, but got: {$notCallable}.");
}
}

Expand Down
28 changes: 7 additions & 21 deletions tests/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,9 +1256,7 @@ public function testRejectsAnInterfaceTypeWithAnIncorrectTypeForResolveType(): v
]);

// Slightly deviating from the reference implementation in order to be idiomatic for PHP
$this->expectExceptionObject(new InvariantViolation(
'AnotherInterface must provide "resolveType" as a callable, but got: instance of stdClass'
));
$this->expectExceptionObject(new InvariantViolation('AnotherInterface must provide "resolveType" as null or a callable, but got: instance of stdClass.'));
$type->assertValid();
}

Expand Down Expand Up @@ -1312,9 +1310,7 @@ public function testAcceptsAUnionTypeDefiningResolveTypeOfObjectTypesDefiningIsT
public function testRejectsAnUnionTypeWithAnIncorrectTypeForResolveType(): void
{
// Slightly deviating from the reference implementation in order to be idiomatic for PHP
$this->expectExceptionObject(new InvariantViolation(
'SomeUnion must provide "resolveType" as a callable, but got: instance of stdClass'
));
$this->expectExceptionObject(new InvariantViolation('SomeUnion must provide "resolveType" as null or a callable, but got: instance of stdClass.'));

$this->schemaWithFieldType(
// @phpstan-ignore-next-line intentionally wrong
Expand Down Expand Up @@ -1347,9 +1343,7 @@ public function testAcceptsAScalarTypeDefiningSerialize(): void
*/
public function testRejectsAScalarTypeDefiningSerializeWithAnIncorrectType(): void
{
$this->expectExceptionObject(new InvariantViolation(
'SomeScalar must provide "serialize" function. If this custom Scalar is also used as an input type, ensure "parseValue" and "parseLiteral" functions are also provided.'
));
$this->expectExceptionObject(new InvariantViolation('SomeScalar must provide "serialize" as a callable if given, but got: instance of stdClass.'));

$this->schemaWithFieldType(
// @phpstan-ignore-next-line intentionally wrong
Expand Down Expand Up @@ -1384,9 +1378,7 @@ public function testAcceptsAScalarTypeDefiningParseValueAndParseLiteral(): void
*/
public function testRejectsAScalarTypeDefiningParseValueButNotParseLiteral(): void
{
$this->expectExceptionObject(new InvariantViolation(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.'
));
$this->expectExceptionObject(new InvariantViolation('SomeScalar must provide both "parseValue" and "parseLiteral" functions to work as an input type.'));

$this->schemaWithFieldType(
new CustomScalarType([
Expand All @@ -1404,9 +1396,7 @@ public function testRejectsAScalarTypeDefiningParseValueButNotParseLiteral(): vo
*/
public function testRejectsAScalarTypeDefiningParseLiteralButNotParseValue(): void
{
$this->expectExceptionObject(new InvariantViolation(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.'
));
$this->expectExceptionObject(new InvariantViolation('SomeScalar must provide both "parseValue" and "parseLiteral" functions to work as an input type.'));

$this->schemaWithFieldType(
new CustomScalarType([
Expand All @@ -1424,9 +1414,7 @@ public function testRejectsAScalarTypeDefiningParseLiteralButNotParseValue(): vo
*/
public function testRejectsAScalarTypeDefiningParseValueAndParseLiteralWithAnIncorrectType(): void
{
$this->expectExceptionObject(new InvariantViolation(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.'
));
$this->expectExceptionObject(new InvariantViolation('SomeScalar must provide "parseValue" as a callable if given, but got: instance of stdClass.'));

$this->schemaWithFieldType(
// @phpstan-ignore-next-line intentionally wrong
Expand Down Expand Up @@ -1462,9 +1450,7 @@ public function testAcceptsAnObjectTypeWithAnIsTypeOfFunction(): void
public function testRejectsAnObjectTypeWithAnIncorrectTypeForIsTypeOf(): void
{
// Slightly deviating from the reference implementation in order to be idiomatic for PHP
$this->expectExceptionObject(new InvariantViolation(
'AnotherObject must provide "isTypeOf" as a callable, but got: instance of stdClass'
));
$this->expectExceptionObject(new InvariantViolation('AnotherObject must provide "isTypeOf" as null or a callable, but got: instance of stdClass.'));

$this->schemaWithFieldType(
// @phpstan-ignore-next-line intentionally wrong
Expand Down

0 comments on commit b9cfc95

Please sign in to comment.