-
-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathNamedTypeImplementation.php
More file actions
56 lines (43 loc) · 1.36 KB
/
NamedTypeImplementation.php
File metadata and controls
56 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
/** @see NamedType */
trait NamedTypeImplementation
{
public string $name;
public ?string $description;
public function toString(): string
{
return $this->name;
}
/** @throws InvariantViolation */
protected function inferName(): string
{
if (isset($this->name)) { // @phpstan-ignore-line property might be uninitialized
return $this->name;
}
// If class is extended - infer name from className
// QueryType -> Type
// SomeOtherType -> SomeOther
$reflection = new \ReflectionClass($this);
$name = $reflection->getShortName();
if ($reflection->getNamespaceName() !== __NAMESPACE__) {
$withoutPrefixType = preg_replace('~Type$~', '', $name);
assert(is_string($withoutPrefixType), 'regex is statically known to be correct');
return $withoutPrefixType;
}
throw new InvariantViolation('Must provide name for Type.');
}
public function isBuiltInType(): bool
{
return in_array($this->name, Type::BUILT_IN_TYPE_NAMES, true);
}
public function name(): string
{
return $this->name;
}
public function description(): ?string
{
return $this->description;
}
}