Subset::dotNotationToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace EnergieProduction\Chart\Renderable;
4
5
use EnergieProduction\Chart\Contracts\Renderable;
6
7
class Subset implements Renderable {
8
9
    /**
10
     * [__construct description]
11
     * @param \EnergieProduction\Chart\Contracts\Renderable $render
12
     */
13
    public function __construct(Renderable $render)
14
    {
15
        $this->render = $render;
0 ignored issues
show
Bug introduced by
The property render does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
    }
17
18
    /**
19
     * [handle description]
20
     * @param  string $key
21
     * @param  mixed $content
22
     * @return array
23
     */
24
    public function handle($key, $content)
25
    {
26
        if (str_contains($key, '.')) {
27
28
            $path = explode('.', $key);
29
30
            $result = [];
31
32
            $this->dotNotationToArray($result, $path, $content);
0 ignored issues
show
Documentation introduced by
$path is of type array, 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...
33
34
            return $this->render->handle($path[0], $result);
35
        }
36
37
        return $this->render->handle($key, $content);
38
    }         
39
40
    /**
41
     * [dotNotationToArray description]
42
     * @param  array  &$arr
43
     * @param  string $path
44
     * @param  string $val
45
     * @return array
46
     */
47
    protected function dotNotationToArray(array &$arr, $path, $val)
48
    {
49
       $loc = &$arr;
50
51
       array_shift($path);
52
53
        foreach($path as $step)
54
        {
55
            $loc = &$loc[$step];
56
        }
57
       
58
       return $loc = $val;
59
    }       
60
}
61