Passed
Push — master ( 322902...e6d843 )
by Adrien
02:21
created

AbstractDecimalType::parseValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
cc 2
rs 10
ccs 5
cts 5
cp 1
crap 2
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
use UnexpectedValueException;
15
16
abstract class AbstractDecimalType extends ScalarType
17
{
18
    /**
19
     * Return the number of digits after the decimal
20
     */
21
    abstract protected function getScale(): int;
22
23
    /**
24
     * Return the minimum accepted value, if any
25
     */
26
    protected function getMinimum(): ?string
27
    {
28
        return null;
29
    }
30
31
    /**
32
     * Return the maximum accepted value, if any
33
     */
34
    protected function getMaximum(): ?string
35
    {
36
        return null;
37
    }
38
39
    /**
40
     * Validate value
41
     */
42 68
    private function isValid(string $value): bool
43
    {
44 68
        $decimal = $this->getScale();
45
46 68
        if (!preg_match('~^-?\d+(\.\d{0,' . $decimal . '})?$~', $value)) {
47 20
            return false;
48
        }
49
50 48
        $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...
51 48
        if ($minimum !== null && bccomp($value, $minimum, $decimal) === -1) {
0 ignored issues
show
introduced by
The condition $minimum !== null is always false.
Loading history...
52 2
            return false;
53
        }
54
55 46
        $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...
56 46
        if ($maximum !== null && bccomp($value, $maximum, $decimal) === 1) {
0 ignored issues
show
introduced by
The condition $maximum !== null is always false.
Loading history...
57 2
            return false;
58
        }
59
60 44
        return true;
61
    }
62
63
    /**
64
     * Serializes an internal value to include in a response.
65
     *
66
     * @param mixed $value
67
     *
68
     * @return mixed
69
     */
70 34
    public function serialize($value)
71
    {
72
        // Assuming internal representation is always correct:
73 34
        return $value;
74
    }
75
76
    /**
77
     * Parses an externally provided value (query variable) to use as an input
78
     *
79
     * @param mixed $value
80
     *
81
     * @return mixed
82
     */
83 34
    public function parseValue($value)
84
    {
85 34
        $parsedValue = (string) $value;
86 34
        if (!$this->isValid($parsedValue)) {
87 12
            throw new UnexpectedValueException('Query error: Not a valid ' . $this->name . ': ' . Utils::printSafe($value));
88
        }
89
90 22
        return $parsedValue;
91
    }
92
93
    /**
94
     * Parses an externally provided literal value to use as an input (e.g. in Query AST)
95
     *
96
     * @return null|string
97
     */
98 34
    public function parseLiteral(Node $ast, ?array $variables = null)
99
    {
100
        // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
101
        // error location in query:
102
103 34
        if (!($ast instanceof StringValueNode || $ast instanceof IntValueNode || $ast instanceof FloatValueNode)) {
104
            throw new Error('Query error: Can only parse strings got: ' . $ast->kind, $ast);
105
        }
106
107 34
        $parsedValue = (string) $ast->value;
108 34
        if (!$this->isValid($parsedValue)) {
109 12
            throw new Error('Query error: Not a valid ' . $this->name, $ast);
110
        }
111
112 22
        return $parsedValue;
113
    }
114
}
115