IntType::coerce()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 8.8571
cc 5
eloc 4
nc 2
nop 1
crap 5
1
<?php
2
3
namespace Fubhy\GraphQL\Type\Definition\Types\Scalars;
4
5
use Fubhy\GraphQL\Language\Node;
6
use Fubhy\GraphQL\Language\Node\IntValue;
7
use Fubhy\GraphQL\Type\Definition\Types\ScalarType;
8
9
class IntType extends ScalarType
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $name = 'Int';
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 75
    public function coerce($value)
20
    {
21 75
        if ((is_numeric($value) && $value <= PHP_INT_MAX && $value >= -PHP_INT_MAX) || is_bool($value)) {
22 66
            return (int) $value;
23
        }
24
25 9
        return NULL;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 6 View Code Duplication
    public function coerceLiteral(Node $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33 6
        if ($node instanceof IntValue) {
34 6
            $num = $node->get('value');
35
36 6
            if ($num <= PHP_INT_MAX && $num >= -PHP_INT_MAX) {
37 6
                return intval($num);
38
            }
39
        }
40
41
        return NULL;
42
    }
43
}
44