Skip to content

Commit 5b504b0

Browse files
committed
Level up PHPStan to the max
1 parent 735955b commit 5b504b0

File tree

7 files changed

+18
-102
lines changed

7 files changed

+18
-102
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 4
2+
level: max
33
paths:
44
- src
55
- tests

src/Email.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99

1010
class Email extends StringScalar
1111
{
12-
/**
13-
* The description that is used for schema introspection.
14-
*
15-
* @var string
16-
*/
1712
public $description = 'A [RFC 5321](https://tools.ietf.org/html/rfc5321) compliant email.';
1813

19-
/**
20-
* Check if the given string is a valid email.
21-
*/
2214
protected function isValid(string $stringValue): bool
2315
{
2416
return (new EmailValidator())->isValid(

src/JSON.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,25 @@
44

55
namespace MLL\GraphQLScalars;
66

7-
use Exception;
87
use GraphQL\Error\Error;
9-
use GraphQL\Language\AST\Node;
108
use GraphQL\Type\Definition\ScalarType;
119
use GraphQL\Utils\Utils as GraphQLUtils;
1210
use Safe\Exceptions\JsonException;
1311

1412
class JSON extends ScalarType
1513
{
16-
/**
17-
* The description that is used for schema introspection.
18-
*
19-
* @var string
20-
*/
2114
public $description = 'Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org/.';
2215

23-
/**
24-
* Serializes an internal value to include in a response.
25-
*/
2616
public function serialize($value): string
2717
{
2818
return \Safe\json_encode($value);
2919
}
3020

31-
/**
32-
* Parses an externally provided value (query variable) to use as an input.
33-
*
34-
* In the case of an invalid value this method must throw an Exception
35-
*
36-
* @throws Error
37-
*/
3821
public function parseValue($value)
3922
{
4023
return $this->decodeJSON($value);
4124
}
4225

43-
/**
44-
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
45-
*
46-
* In the case of an invalid node or value this method must throw an Exception
47-
*
48-
* @param Node $valueNode
49-
* @param mixed[]|null $variables
50-
*
51-
* @throws Exception
52-
*/
5326
public function parseLiteral($valueNode, ?array $variables = null)
5427
{
5528
if (!property_exists($valueNode, 'value')) {
@@ -62,20 +35,23 @@ public function parseLiteral($valueNode, ?array $variables = null)
6235
}
6336

6437
/**
65-
* Try to decode a user-given value into JSON.
38+
* Try to decode a user-given JSON value.
39+
*
40+
* @param mixed $value A user given JSON
41+
* @return mixed The decoded value
6642
*
6743
* @throws Error
6844
*/
6945
protected function decodeJSON($value)
7046
{
7147
try {
72-
$parsed = \Safe\json_decode($value);
48+
$decoded = \Safe\json_decode($value);
7349
} catch (JsonException $jsonException) {
7450
throw new Error(
7551
$jsonException->getMessage()
7652
);
7753
}
7854

79-
return $parsed;
55+
return $decoded;
8056
}
8157
}

src/Mixed.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace MLL\GraphQLScalars;
66

7-
use GraphQL\Language\AST\Node;
87
use GraphQL\Type\Definition\ScalarType;
98
use GraphQL\Utils\AST;
109

@@ -21,34 +20,16 @@ class Mixed extends ScalarType
2120
dealing with really large numbers to be on the safe side.
2221
DESCRIPTION;
2322

24-
/**
25-
* Serializes an internal value to include in a response.
26-
*/
2723
public function serialize($value)
2824
{
2925
return $value;
3026
}
3127

32-
/**
33-
* Parses an externally provided value (query variable) to use as an input.
34-
*
35-
* In the case of an invalid value this method must throw an Exception
36-
*/
3728
public function parseValue($value)
3829
{
3930
return $value;
4031
}
4132

42-
/**
43-
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
44-
*
45-
* In the case of an invalid node or value, this method must throw an Exception.
46-
*
47-
* @param Node $valueNode
48-
* @param mixed[]|null $variables
49-
*
50-
* @throws \Exception
51-
*/
5233
public function parseLiteral($valueNode, ?array $variables = null)
5334
{
5435
return AST::valueFromASTUntyped($valueNode);

src/Regex.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use GraphQL\Error\Error;
88
use GraphQL\Error\InvariantViolation;
9-
use GraphQL\Language\AST\Node;
109
use GraphQL\Type\Definition\ScalarType;
1110
use GraphQL\Utils\Utils as GraphQLUtils;
1211
use Spatie\Regex\Regex as RegexValidator;
@@ -52,9 +51,6 @@ public static function regex(): string
5251
*/
5352
abstract public static function regex(): string;
5453

55-
/**
56-
* Serializes an internal value to include in a response.
57-
*/
5854
public function serialize($value): string
5955
{
6056
$stringValue = Utils::coerceToString($value, InvariantViolation::class);
@@ -81,11 +77,6 @@ protected static function matchesRegex(string $value): bool
8177
->hasMatch();
8278
}
8379

84-
/**
85-
* Parses an externally provided value (query variable) to use as an input.
86-
*
87-
* @throws Error
88-
*/
8980
public function parseValue($value): string
9081
{
9182
$stringValue = Utils::coerceToString($value, Error::class);
@@ -99,22 +90,14 @@ public function parseValue($value): string
9990
return $value;
10091
}
10192

102-
/**
103-
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
104-
*
105-
* @param Node $valueNode
106-
* @param mixed[]|null $variables
107-
*
108-
* @throws Error
109-
*/
11093
public function parseLiteral($valueNode, ?array $variables = null): string
11194
{
11295
$value = Utils::extractStringFromLiteral($valueNode);
11396

11497
if (!static::matchesRegex($value)) {
11598
throw new Error(
11699
static::unmatchedRegexMessage($value),
117-
[$valueNode]
100+
$valueNode
118101
);
119102
}
120103

src/StringScalar.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ protected function isValid(string $stringValue): bool
5050
*/
5151
abstract protected function isValid(string $stringValue): bool;
5252

53-
/**
54-
* Serializes an internal value to include in a response.
55-
*
56-
* @throws InvariantViolation
57-
*/
5853
public function serialize($value): string
5954
{
6055
$stringValue = Utils::coerceToString($value, InvariantViolation::class);
@@ -78,11 +73,6 @@ public function invalidStringMessage(string $stringValue): string
7873
return "The given string {$safeValue} is not a valid {$this->tryInferName()}.";
7974
}
8075

81-
/**
82-
* Parses an externally provided value (query variable) to use as an input.
83-
*
84-
* @throws Error
85-
*/
8676
public function parseValue($value): string
8777
{
8878
$stringValue = Utils::coerceToString($value, Error::class);
@@ -96,19 +86,6 @@ public function parseValue($value): string
9686
return $stringValue;
9787
}
9888

99-
/**
100-
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
101-
*
102-
* E.g.
103-
* {
104-
* user(email: "[email protected]")
105-
* }
106-
*
107-
* @param Node $valueNode
108-
* @param mixed[]|null $variables
109-
*
110-
* @throws Error
111-
*/
11289
public function parseLiteral($valueNode, ?array $variables = null): string
11390
{
11491
$stringValue = Utils::extractStringFromLiteral($valueNode);

src/Utils.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
namespace MLL\GraphQLScalars;
66

77
use GraphQL\Error\Error;
8+
use GraphQL\Language\AST\Node;
89
use GraphQL\Language\AST\StringValueNode;
10+
use GraphQL\Language\AST\ValueNode;
911
use GraphQL\Utils\Utils as GraphQLUtils;
1012

1113
class Utils
1214
{
1315
/**
1416
* Check if a value can be serialized to a string.
17+
*
18+
* @param mixed $value Any value
1519
*/
1620
public static function canBeString($value): bool
1721
{
@@ -23,14 +27,15 @@ public static function canBeString($value): bool
2327
/**
2428
* Get the underlying string from a GraphQL literal and throw if Literal is not a string.
2529
*
30+
* @param Node&ValueNode $valueNode
2631
* @throws Error
2732
*/
28-
public static function extractStringFromLiteral($valueNode): string
33+
public static function extractStringFromLiteral(Node $valueNode): string
2934
{
3035
if (!$valueNode instanceof StringValueNode) {
3136
throw new Error(
3237
"Query error: Can only parse strings got: {$valueNode->kind}",
33-
[$valueNode]
38+
$valueNode
3439
);
3540
}
3641

@@ -40,6 +45,8 @@ public static function extractStringFromLiteral($valueNode): string
4045
/**
4146
* Convert the value to a string and throw an exception if it is not possible.
4247
*
48+
* @param mixed $value Any value
49+
*
4350
* @throws \Exception of type $exceptionClass
4451
*/
4552
public static function coerceToString($value, string $exceptionClass): string
@@ -52,6 +59,6 @@ public static function coerceToString($value, string $exceptionClass): string
5259
);
5360
}
5461

55-
return strval($value);
62+
return (string) $value;
5663
}
5764
}

0 commit comments

Comments
 (0)