AbstractDecimalType   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 24
c 1
b 0
f 0
dl 0
loc 89
rs 10
ccs 25
cts 29
cp 0.8621

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseValue() 0 8 2
A parseLiteral() 0 15 5
A getMinimum() 0 3 1
A getMaximum() 0 3 1
A serialize() 0 4 1
A isValid() 0 19 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Scalar;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Language\AST\FloatValueNode;
9
use GraphQL\Language\AST\IntValueNode;
10
use GraphQL\Language\AST\Node;
11
use GraphQL\Language\AST\StringValueNode;
12
use GraphQL\Type\Definition\ScalarType;
13
use GraphQL\Utils\Utils;
14
15
abstract class AbstractDecimalType extends ScalarType
16
{
17
    /**
18
     * Return the number of digits after the decimal.
19
     */
20
    abstract protected function getScale(): int;
21
22
    /**
23
     * Return the minimum accepted value, if any.
24
     */
25
    protected function getMinimum(): ?string
26
    {
27
        return null;
28
    }
29
30
    /**
31
     * Return the maximum accepted value, if any.
32
     */
33
    protected function getMaximum(): ?string
34
    {
35
        return null;
36
    }
37
38
    /**
39
     * Validate value.
40
     */
41 40
    private function isValid(string $value): bool
42
    {
43 40
        $decimal = $this->getScale();
44
45 40
        if (!preg_match('~^-?\d+(\.\d{0,' . $decimal . '})?$~', $value)) {
46 12
            return false;
47
        }
48
49 28
        $minimum = $this->getMinimum();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $minimum is correct as $this->getMinimum() targeting Ecodev\Felix\Api\Scalar\...cimalType::getMinimum() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50 28
        if ($minimum !== null && bccomp($value, $minimum, $decimal) === -1) {
0 ignored issues
show
introduced by
The condition $minimum !== null is always false.
Loading history...
51 2
            return false;
52
        }
53
54 26
        $maximum = $this->getMaximum();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $maximum is correct as $this->getMaximum() targeting Ecodev\Felix\Api\Scalar\...cimalType::getMaximum() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55 26
        if ($maximum !== null && bccomp($value, $maximum, $decimal) === 1) {
0 ignored issues
show
introduced by
The condition $maximum !== null is always false.
Loading history...
56 2
            return false;
57
        }
58
59 24
        return true;
60
    }
61
62
    /**
63
     * Serializes an internal value to include in a response.
64
     */
65 20
    public function serialize(mixed $value): mixed
66
    {
67
        // Assuming internal representation is always correct:
68 20
        return $value;
69
    }
70
71
    /**
72
     * Parses an externally provided value (query variable) to use as an input.
73
     *
74
     * @param null|float|int|string $value
75
     */
76 20
    public function parseValue(mixed $value): string
77
    {
78 20
        $parsedValue = (string) $value;
79 20
        if (!$this->isValid($parsedValue)) {
80 8
            throw new Error('Query error: Not a valid ' . $this->name . ': ' . Utils::printSafe($value));
81
        }
82
83 12
        return $parsedValue;
84
    }
85
86
    /**
87
     * Parses an externally provided literal value to use as an input (e.g. in Query AST).
88
     */
89 21
    public function parseLiteral(Node $valueNode, ?array $variables = null): string
90
    {
91
        // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
92
        // error location in query:
93
94 21
        if (!($valueNode instanceof StringValueNode || $valueNode instanceof IntValueNode || $valueNode instanceof FloatValueNode)) {
95 1
            throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
96
        }
97
98 20
        $parsedValue = (string) $valueNode->value;
99 20
        if (!$this->isValid($parsedValue)) {
100 8
            throw new Error('Query error: Not a valid ' . $this->name, $valueNode);
101
        }
102
103 12
        return $parsedValue;
104
    }
105
}
106