Directive::getInternalDirectives()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.7257

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 56
ccs 3
cts 25
cp 0.12
rs 9.296
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 4.7257

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Type\Definition;
6
7
use GraphQL\Language\AST\DirectiveDefinitionNode;
8
use GraphQL\Language\DirectiveLocation;
9
use GraphQL\Utils\Utils;
10
use function array_key_exists;
11
use function is_array;
12
13
class Directive
14
{
15
    public const DEFAULT_DEPRECATION_REASON = 'No longer supported';
16
17
    public const INCLUDE_NAME         = 'include';
18
    public const IF_ARGUMENT_NAME     = 'if';
19
    public const SKIP_NAME            = 'skip';
20
    public const DEPRECATED_NAME      = 'deprecated';
21
    public const REASON_ARGUMENT_NAME = 'reason';
22
23
    /** @var Directive[]|null */
24
    public static $internalDirectives;
25
26
    // Schema Definitions
27
28
    /** @var string */
29
    public $name;
30
31
    /** @var string|null */
32
    public $description;
33
34
    /** @var string[] */
35
    public $locations;
36
37
    /** @var FieldArgument[] */
38
    public $args = [];
39
40
    /** @var DirectiveDefinitionNode|null */
41
    public $astNode;
42
43
    /** @var mixed[] */
44
    public $config;
45
46
    /**
47
     * @param mixed[] $config
48
     */
49 440
    public function __construct(array $config)
50
    {
51 440
        if (isset($config['args'])) {
52 97
            $args = [];
53 97
            foreach ($config['args'] as $name => $arg) {
54 76
                if (is_array($arg)) {
55 57
                    $args[] = new FieldArgument($arg + ['name' => $name]);
56
                } else {
57 76
                    $args[] = $arg;
58
                }
59
            }
60 97
            $this->args = $args;
61 97
            unset($config['args']);
62
        }
63 440
        foreach ($config as $key => $value) {
64 440
            $this->{$key} = $value;
65
        }
66
67 440
        Utils::invariant($this->name, 'Directive must be named.');
68 440
        Utils::invariant(is_array($this->locations), 'Must provide locations for directive.');
69 440
        $this->config = $config;
70 440
    }
71
72
    /**
73
     * @return Directive
74
     */
75 704
    public static function includeDirective()
76
    {
77 704
        $internal = self::getInternalDirectives();
78
79 704
        return $internal['include'];
80
    }
81
82
    /**
83
     * @return Directive[]
84
     */
85 895
    public static function getInternalDirectives() : array
86
    {
87 895
        if (self::$internalDirectives === null) {
88
            self::$internalDirectives = [
89
                'include'    => new self([
90
                    'name'        => self::INCLUDE_NAME,
91
                    'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
92
                    'locations'   => [
93
                        DirectiveLocation::FIELD,
94
                        DirectiveLocation::FRAGMENT_SPREAD,
95
                        DirectiveLocation::INLINE_FRAGMENT,
96
                    ],
97
                    'args'        => [new FieldArgument([
98
                        'name'        => self::IF_ARGUMENT_NAME,
99
                        'type'        => Type::nonNull(Type::boolean()),
100
                        'description' => 'Included when true.',
101
                    ]),
102
                    ],
103
                ]),
104
                'skip'       => new self([
105
                    'name'        => self::SKIP_NAME,
106
                    'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
107
                    'locations'   => [
108
                        DirectiveLocation::FIELD,
109
                        DirectiveLocation::FRAGMENT_SPREAD,
110
                        DirectiveLocation::INLINE_FRAGMENT,
111
                    ],
112
                    'args'        => [new FieldArgument([
113
                        'name'        => self::IF_ARGUMENT_NAME,
114
                        'type'        => Type::nonNull(Type::boolean()),
115
                        'description' => 'Skipped when true.',
116
                    ]),
117
                    ],
118
                ]),
119
                'deprecated' => new self([
120
                    'name'        => self::DEPRECATED_NAME,
121
                    'description' => 'Marks an element of a GraphQL schema as no longer supported.',
122
                    'locations'   => [
123
                        DirectiveLocation::FIELD_DEFINITION,
124
                        DirectiveLocation::ENUM_VALUE,
125
                    ],
126
                    'args'        => [new FieldArgument([
127
                        'name'         => self::REASON_ARGUMENT_NAME,
128
                        'type'         => Type::string(),
129
                        'description'  =>
130
                            'Explains why this element was deprecated, usually also including a ' .
131
                            'suggestion for how to access supported similar data. Formatted ' .
132
                            'in [Markdown](https://daringfireball.net/projects/markdown/).',
133
                        'defaultValue' => self::DEFAULT_DEPRECATION_REASON,
134
                    ]),
135
                    ],
136
                ]),
137
            ];
138
        }
139
140 895
        return self::$internalDirectives;
141
    }
142
143
    /**
144
     * @return Directive
145
     */
146 706
    public static function skipDirective()
147
    {
148 706
        $internal = self::getInternalDirectives();
149
150 706
        return $internal['skip'];
151
    }
152
153
    /**
154
     * @return Directive
155
     */
156 177
    public static function deprecatedDirective()
157
    {
158 177
        $internal = self::getInternalDirectives();
159
160 177
        return $internal['deprecated'];
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 109
    public static function isSpecifiedDirective(Directive $directive)
167
    {
168 109
        return array_key_exists($directive->name, self::getInternalDirectives());
169
    }
170
}
171