1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection\Stdlib\Directive; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\Definition\Dependent\ArgumentDefinition; |
13
|
|
|
use Railt\Reflection\Definition\Dependent\DirectiveLocation; |
14
|
|
|
use Railt\Reflection\Definition\DirectiveDefinition; |
15
|
|
|
use Railt\Reflection\Document; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class DeprecatedDirective |
19
|
|
|
*/ |
20
|
|
|
final class DeprecatedDirective extends DirectiveDefinition |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private const LOCATIONS = [ |
26
|
|
|
DirectiveLocation::OBJECT, |
27
|
|
|
DirectiveLocation::INTERFACE, |
28
|
|
|
DirectiveLocation::FIELD_DEFINITION, |
29
|
|
|
DirectiveLocation::SCALAR, |
30
|
|
|
DirectiveLocation::UNION, |
31
|
|
|
DirectiveLocation::ENUM, |
32
|
|
|
DirectiveLocation::ENUM_VALUE, |
33
|
|
|
DirectiveLocation::INPUT_OBJECT, |
34
|
|
|
DirectiveLocation::INPUT_UNION, |
35
|
|
|
DirectiveLocation::INPUT_FIELD_DEFINITION, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public const TYPE_NAME = 'deprecated'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public const TYPE_DESCRIPTION = <<<Description |
47
|
|
|
The @deprecated directive is used within the type system definition language to |
48
|
|
|
indicate deprecated portions of a GraphQL service’s schema, such as deprecated |
49
|
|
|
fields on a type or deprecated enum values. |
50
|
|
|
Description; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* BooleanScalar constructor. |
54
|
|
|
* @param Document $document |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Document $document) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($document, self::TYPE_NAME); |
59
|
|
|
|
60
|
|
|
$this->withDescription(self::TYPE_DESCRIPTION) |
|
|
|
|
61
|
|
|
->withArgument((new ArgumentDefinition($this, 'reason', 'String')) |
|
|
|
|
62
|
|
|
->withLine(39) |
63
|
|
|
->withDefaultValue('No longer supported')) |
64
|
|
|
->withLocation(...\array_map(function (string $location) use ($document): DirectiveLocation { |
65
|
|
|
return new DirectiveLocation($this, $location); |
66
|
|
|
}, self::LOCATIONS)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getLine(): int |
73
|
|
|
{ |
74
|
|
|
return 39; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isBuiltin(): bool |
81
|
|
|
{ |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: