|
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\Reflection\Validation\Definitions; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Contracts\Definitions\Definition; |
|
13
|
|
|
use Railt\SDL\Contracts\Definitions\Directive\Location; |
|
14
|
|
|
use Railt\SDL\Contracts\Definitions\DirectiveDefinition; |
|
15
|
|
|
use Railt\SDL\Exceptions\TypeConflictException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class DirectiveDefinitionValidator |
|
19
|
|
|
*/ |
|
20
|
|
|
class DirectiveDefinitionValidator extends BaseDefinitionValidator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param Definition $definition |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
6548 |
|
public function match(Definition $definition): bool |
|
27
|
|
|
{ |
|
28
|
6548 |
|
return $definition instanceof DirectiveDefinition; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Definition|DirectiveDefinition $definition |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
2436 |
|
public function validate(Definition $definition): void |
|
36
|
|
|
{ |
|
37
|
2436 |
|
$this->getCallStack()->push($definition); |
|
38
|
|
|
|
|
39
|
2436 |
|
$locations = $definition->getLocations(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
2436 |
|
foreach ($locations as $location) { |
|
42
|
2436 |
|
$isValidLocation = $this->isSDLLocation($location) || $this->isQueryLocation($location); |
|
43
|
|
|
|
|
44
|
2436 |
|
if (! $isValidLocation) { |
|
45
|
6 |
|
$error = \vsprintf('Trying to define directive %s, but %s location is invalid', [ |
|
46
|
6 |
|
$definition, |
|
47
|
6 |
|
$location, |
|
48
|
|
|
]); |
|
49
|
2436 |
|
throw new TypeConflictException($error, $this->getCallStack()); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2430 |
|
$this->getCallStack()->pop(); |
|
54
|
2430 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $location |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
2436 |
|
private function isSDLLocation(string $location): bool |
|
61
|
|
|
{ |
|
62
|
2436 |
|
return \in_array($location, Location::TARGET_GRAPHQL_SDL, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $location |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
6 |
|
private function isQueryLocation(string $location): bool |
|
70
|
|
|
{ |
|
71
|
6 |
|
return \in_array($location, Location::TARGET_GRAPHQL_QUERY, true); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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: