Passed
Push — master ( a9556d...8d29f3 )
by Dominik
03:16
created

ScalarToNodeConverter::convert()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 6
nop 2
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder\Converter;
6
7
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\BoolNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\IntNode;
11
use Saxulum\ElasticSearchQueryBuilder\Node\NullNode;
12
use Saxulum\ElasticSearchQueryBuilder\Node\StringNode;
13
14
final class ScalarToNodeConverter implements ScalarToNodeConverterInterface
15
{
16
    /**
17
     * @param bool|float|integer|null|string $value
18
     * @param string                         $path
19
     * @return AbstractNode
20
     * @throws \InvalidArgumentException
21
     */
22 6
    public function convert($value, string $path = ''): AbstractNode
23
    {
24 6
        $type = gettype($value);
25
26 6
        if ($type === 'boolean') {
27 1
            return BoolNode::create($value);
28
        }
29
30 5
        if ($type === 'double') {
31 1
            return FloatNode::create($value);
32
        }
33
34 4
        if ($type === 'integer') {
35 1
            return IntNode::create($value);
36
        }
37
38 3
        if ($type === 'NULL') {
39 1
            return NullNode::create($value);
0 ignored issues
show
Unused Code introduced by
The call to NullNode::create() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
        }
41
42 2
        if ($type === 'string') {
43 1
            return StringNode::create($value);
44
        }
45
46 1
        throw new \InvalidArgumentException(
47 1
            sprintf('Type %s is not supported, at path %s', is_object($value) ? get_class($value) : $type, $path)
48
        );
49
    }
50
}
51