Completed
Push — master ( 85ee72...e5dec6 )
by Restu
11:15
created

ArrayH::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 9
rs 9.6666
1
<?php
2
namespace JayaCode\Framework\Core\Helper\HelperArray;
3
4
class ArrayH
5
{
6
    public static $pathSeparator = ".";
7
8
    public static function get($arr, $path, $default = null)
9
    {
10
        $val = $default;
11
        static::exploreAtPath($arr, $path, function (&$prosArr) use (&$val) {
0 ignored issues
show
Bug introduced by
Since exploreAtPath() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of exploreAtPath() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
12
            $val = $prosArr;
13
        });
14
15
        return $val;
16
    }
17
18
    private static function exploreAtPath(array &$arr, $path, callable $callback, array &$prosArr = null)
19
    {
20
        if ($prosArr === null) {
21
            $prosArr = &$arr;
22
            if (is_string($path) && $path == '') {
23
                $callback($prosArr);
24
                return;
25
            }
26
        }
27
28
        $explodedPath = explode(self::$pathSeparator, $path);
29
        $nextPath = array_shift($explodedPath);
30
31
        if (count($explodedPath) > 0) {
32
            static::exploreAtPath(
0 ignored issues
show
Bug introduced by
Since exploreAtPath() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of exploreAtPath() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
33
                $arr,
34
                implode(self::$pathSeparator, $explodedPath),
35
                $callback,
36
                $prosArr[$nextPath]
37
            );
38
        } else {
39
            $callback($prosArr[$nextPath]);
40
        }
41
    }
42
}
43