Json   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseValue() 0 7 2
A serialize() 0 3 1
A parseLiteral() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Type;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Language\AST\Node as ASTNode;
9
use GraphQL\Type\Definition\ScalarType;
10
11
use function is_string;
12
use function json_decode;
13
use function json_encode;
14
15
class Json extends ScalarType
16
{
17
    // phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
18
    public string|null $description = 'The `JSON` scalar type represents json data.';
19
20
    public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string
21
    {
22
        throw new Error('JSON fields are not searchable', $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

22
        throw new Error('JSON fields are not searchable', /** @scrutinizer ignore-type */ $valueNode);
Loading history...
23
    }
24
25
    /**
26
     * @return mixed[]|null
27
     *
28
     * @throws Error
29
     */
30
    public function parseValue(mixed $value): array|null
31
    {
32
        if (! is_string($value)) {
33
            throw new Error('Json is not a string: ' . $value);
34
        }
35
36
        return json_decode($value, true);
37
    }
38
39
    public function serialize(mixed $value): string|null
40
    {
41
        return json_encode($value);
42
    }
43
}
44