TimeImmutable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLiteral() 0 10 2
A serialize() 0 7 2
A parseValue() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Type;
6
7
use DateTimeImmutable as PHPDateTime;
8
use GraphQL\Error\Error;
9
use GraphQL\Language\AST\Node as ASTNode;
10
use GraphQL\Language\AST\StringValueNode;
11
use GraphQL\Type\Definition\ScalarType;
12
13
use function is_string;
14
15
class TimeImmutable extends ScalarType
16
{
17
    // phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
18
    public string|null $description = 'The `Time` scalar type represents time data.'
19
    . 'The format is e.g. 24 hour:minutes:seconds';
20
21
    public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string
22
    {
23
        // @codeCoverageIgnoreStart
24
        if (! $valueNode instanceof StringValueNode) {
25
            throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
0 ignored issues
show
Bug introduced by
$valueNode of type GraphQL\Language\AST\Node is incompatible with the type iterable expected by parameter $nodes of GraphQL\Error\Error::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
            throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, /** @scrutinizer ignore-type */ $valueNode);
Loading history...
26
        }
27
28
        // @codeCoverageIgnoreEnd
29
30
        return $valueNode->value;
31
    }
32
33
    public function parseValue(mixed $value): PHPDateTime|false
34
    {
35
        if (! is_string($value)) {
36
            throw new Error('Time is not a string: ' . $value);
37
        }
38
39
        return PHPDateTime::createFromFormat('H:i:s.u', $value);
40
    }
41
42
    public function serialize(mixed $value): string|null
43
    {
44
        if ($value instanceof PHPDateTime) {
45
            $value = $value->format('H:i:s.u');
46
        }
47
48
        return $value;
49
    }
50
}
51