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

IteratableToNodeConverter::isArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
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\AbstractParentNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
11
12
final class IteratableToNodeConverter implements IteratableToNodeConverterInterface
13
{
14
    /**
15
     * @var ScalarToNodeConverterInterface
16
     */
17
    private $scalarToNodeConverter;
18
19
    /**
20
     * @param ScalarToNodeConverterInterface $scalarToNodeConverter
21
     */
22 3
    public function __construct(ScalarToNodeConverterInterface $scalarToNodeConverter)
23
    {
24 3
        $this->scalarToNodeConverter = $scalarToNodeConverter;
25 3
    }
26
27
    /**
28
     * @param array|\Traversable $data
29
     * @param string             $path
30
     * @return AbstractParentNode
31
     * @throws \InvalidArgumentException
32
     */
33 3
    public function convert($data, string $path = ''): AbstractParentNode
34
    {
35 3
        if (!is_array($data) && !$data instanceof \Traversable) {
36 1
            throw new \InvalidArgumentException(sprintf('Params need to be array or %s', \Traversable::class));
37
        }
38
        
39 2
        $isArray = $this->isArray($data);
40
        
41 2
        $dataNode = $isArray ? ArrayNode::create() : ObjectNode::create();
42 2
        foreach ($data as $key => $value) {
43 2
            $node = $this->getNode($value, $this->getSubPath($path, $key, $isArray));
44
45 1
            if ($isArray) {
46 1
                $dataNode->add($node);
0 ignored issues
show
Bug introduced by
The call to add() misses a required argument $node.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$node is of type object<Saxulum\ElasticSe...lder\Node\AbstractNode>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
            } else {
48 1
                $dataNode->add((string) $key, $node);
0 ignored issues
show
Documentation introduced by
(string) $key is of type string, but the function expects a object<Saxulum\ElasticSe...lder\Node\AbstractNode>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to ArrayNode::add() has too many arguments starting with $node.

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...
49
            }
50
        }
51
52 1
        return $dataNode;
53
    }
54
55
    /**
56
     * @param array|\Traversable $data
57
     * @return bool
58
     */
59 2
    private function isArray($data): bool
60
    {
61 2
        $counter = 0;
62 2
        foreach ($data as $key => $value) {
63 2
            if ($key !== $counter) {
64 2
                return false;
65
            }
66
67 2
            $counter++;
68
        }
69
70 2
        return true;
71
    }
72
73
    /**
74
     * @param string     $path
75
     * @param string|int $key
76
     * @param bool       $isArray
77
     * @return string
78
     */
79 2
    private function getSubPath(string $path, $key, bool $isArray): string
80
    {
81 2
        $key = (string) $key;
82
83 2
        if ($isArray) {
84 2
            return $path . '[' . $key . ']';
85
        }
86
87 2
        return $path !== '' ? $path . '.' . $key : $key;
88
    }
89
90
    /**
91
     * @param mixed  $value
92
     * @param string $path
93
     * @return AbstractNode
94
     */
95 2
    private function getNode($value, string $path): AbstractNode
96
    {
97 2
        if (is_array($value) || $value instanceof \Traversable) {
98 2
            return $this->convert($value, $path);
99
        }
100
101 2
        return $this->scalarToNodeConverter->convert($value, $path);
102
    }
103
}
104