EnumTypeMiddleware::getCaseDeprecationReason()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\TypeResolver\Middleware;
6
7
use Andi\GraphQL\Attribute;
8
use Andi\GraphQL\Common\DefinitionAwareTrait;
9
use Andi\GraphQL\TypeResolver\TypeResolverInterface;
10
use GraphQL\Type\Definition as Webonyx;
11
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
12
use phpDocumentor\Reflection\DocBlockFactory;
13
use Spiral\Attributes\ReaderInterface;
14
15
final class EnumTypeMiddleware implements MiddlewareInterface
16
{
17
    use DefinitionAwareTrait;
0 ignored issues
show
introduced by
The trait Andi\GraphQL\Common\DefinitionAwareTrait requires some properties which are not provided by Andi\GraphQL\TypeResolve...ware\EnumTypeMiddleware: $name, $description
Loading history...
18
19
    public const PRIORITY = AttributedGraphQLTypeMiddleware::PRIORITY + 256;
20
21 6
    public function __construct(
22
        private readonly ReaderInterface $reader,
23
    ) {
24 6
    }
25
26 5
    public function process(mixed $type, TypeResolverInterface $typeResolver): Webonyx\Type
27
    {
28 5
        $enum = \is_string($type) && \enum_exists($type)
0 ignored issues
show
Bug introduced by
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

28
        $enum = \is_string($type) && /** @scrutinizer ignore-call */ \enum_exists($type)
Loading history...
29 1
            ? new \ReflectionEnum($type)
0 ignored issues
show
Bug introduced by
The type ReflectionEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30 4
            : $type;
31
32 5
        if ($enum instanceof \ReflectionEnum) {
33 4
            return $this->buildEnumType($enum, $this->reader->firstClassMetadata($enum, Attribute\EnumType::class));
34
        }
35
36 1
        return $typeResolver->resolve($type);
37
    }
38
39 4
    private function buildEnumType(\ReflectionEnum $class, ?Attribute\EnumType $attribute): Webonyx\EnumType
40
    {
41 4
        $config = [
42 4
            'name' => $this->getTypeName($class, $attribute),
43 4
            'description' => $this->getTypeDescription($class, $attribute),
44 4
            'values' => [],
45 4
        ];
46
47 4
        foreach ($class->getCases() as $case) {
48 4
            $caseAttribute = $this->reader->firstConstantMetadata($case, Attribute\EnumValue::class);
49
50 4
            $config['values'][$this->getCaseName($case, $caseAttribute)] = [
51 4
                'value' => $case->getValue(),
52 4
                'description' => $this->getCaseDescription($case, $caseAttribute),
53 4
                'deprecationReason' => $this->getCaseDeprecationReason($case, $caseAttribute),
54 4
            ];
55
        }
56
57 4
        return new Webonyx\EnumType($config);
58
    }
59
60 4
    private function getCaseName(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): string
0 ignored issues
show
Bug introduced by
The type ReflectionEnumUnitCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
    {
62 4
        if ($attribute?->name) {
63 1
            \assert($attribute->name !== null);
64 1
            return $attribute->name;
65
        }
66
67 3
        return $case->getName();
68
    }
69
70 4
    private function getCaseDescription(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): ?string
71
    {
72 4
        if ($attribute?->description) {
73 1
            return $attribute->description;
74
        }
75
76 3
        if ($docComment = $case->getDocComment()) {
77 1
            return DocBlockFactory::createInstance()->create($docComment)->getSummary() ?: null;
78
        }
79
80 2
        return null;
81
    }
82
83 4
    private function getCaseDeprecationReason(\ReflectionEnumUnitCase $case, ?Attribute\EnumValue $attribute): ?string
84
    {
85 4
        if ($attribute?->deprecationReason) {
86 1
            return $attribute->deprecationReason;
87
        }
88
89 3
        if ($docComment = $case->getDocComment()) {
90 1
            $docBlock = DocBlockFactory::createInstance()->create($docComment);
91 1
            foreach ($docBlock->getTags() as $tag) {
92 1
                if ($tag instanceof Deprecated) {
93 1
                    return (string) $tag->getDescription() ?: null;
94
                }
95
            }
96
        }
97
98 2
        return null;
99
    }
100
}
101