|
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\SDL\Standard\Directives\Deprecation; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Base\Dependent\BaseArgument; |
|
13
|
|
|
use Railt\SDL\Contracts\Definitions\DirectiveDefinition; |
|
14
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
15
|
|
|
use Railt\SDL\Contracts\Document; |
|
16
|
|
|
use Railt\SDL\Standard\Directives\Deprecation; |
|
17
|
|
|
use Railt\SDL\Standard\GraphQLDocument; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Reason |
|
21
|
|
|
*/ |
|
22
|
|
|
class Reason extends BaseArgument |
|
23
|
|
|
{ |
|
24
|
|
|
private const ARGUMENT_TYPE = 'String'; |
|
25
|
|
|
|
|
26
|
|
|
private const ARGUMENT_DEFAULT_VALUE = 'No longer supported'; |
|
27
|
|
|
|
|
28
|
|
|
private const ARGUMENT_DESCRIPTION = 'You can either supply a reason argument ' . |
|
29
|
|
|
'with a string value or not supply one and receive a "No longer supported" ' . |
|
30
|
|
|
'message when introspected'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Reason constructor. |
|
34
|
|
|
* @param Document|GraphQLDocument $document |
|
35
|
|
|
* @param DirectiveDefinition $type |
|
36
|
|
|
*/ |
|
37
|
283 |
|
public function __construct(Document $document, DirectiveDefinition $type) |
|
38
|
|
|
{ |
|
39
|
283 |
|
$this->parent = $type; |
|
40
|
283 |
|
$this->document = $document; |
|
41
|
283 |
|
$this->name = Deprecation::REASON_ARGUMENT; |
|
42
|
283 |
|
$this->description = self::ARGUMENT_DESCRIPTION; |
|
43
|
|
|
|
|
44
|
283 |
|
$this->defaultValue = self::ARGUMENT_DEFAULT_VALUE; |
|
45
|
283 |
|
$this->hasDefaultValue = true; |
|
46
|
283 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return TypeDefinition |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function getTypeDefinition(): TypeDefinition |
|
52
|
|
|
{ |
|
53
|
12 |
|
return $this->document->getDictionary()->get(self::ARGUMENT_TYPE, $this); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
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: