Completed
Push — develop ( 8de61d...0af939 )
by Nate
02:17
created

TransformerHelper::normalizeIncludesInternal()   C

Complexity

Conditions 8
Paths 25

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 16
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 25
nop 1
crap 72
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/transform/releases/latest
7
 * @license   https://github.com/flipbox/transform/blob/master/LICENSE
8
 */
9
10
namespace Flipbox\Transform\Helpers;
11
12
use Closure;
13
use Flipbox\Transform\Transformers\TransformerInterface;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class TransformerHelper
20
{
21
    /**
22
     * @param $transformer
23
     * @return bool
24
     */
25 3
    public static function isTransformer($transformer): bool
26
    {
27 3
        return (is_object($transformer) && ($transformer instanceof Closure)) ||
28 3
            $transformer instanceof TransformerInterface;
29
    }
30
31
    /**
32
     * @param $transformer
33
     * @return bool
34
     */
35
    public static function isTransformerClass($transformer): bool
36
    {
37
        return is_string($transformer) && is_subclass_of($transformer, TransformerInterface::class);
38
    }
39
40
    /**
41
     * @param $transformer
42
     * @return null|callable|TransformerInterface
43
     */
44
    public static function resolve($transformer)
45
    {
46
        if (static::isTransformer($transformer)) {
47
            return $transformer;
48
        }
49
50
        if (static::isTransformerClass($transformer)) {
51
            return new $transformer();
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param TransformerInterface $transformer
59
     * @param string $key
60
     * @return bool
61
     */
62
    public static function inInclude(TransformerInterface $transformer, string $key): bool
63
    {
64
        return self::findInArray($transformer->getIncludes(), $key) !== null;
65
    }
66
67
    /**
68
     * @param array $includes
69
     * @return array
70
     */
71
    public static function normalizeIncludes(array $includes): array
72
    {
73
        foreach ($includes as $k => $v) {
74
75
            if (is_string($v) && ($pos = strrpos($v, '.')) !== false) {
76
                $v = [substr($v, 0, $pos) => [substr($v, $pos + 1)]];
77
            }
78
79
            // normalize sub-includes
80
            $v = is_array($v) ? static::normalizeIncludes($v) : $v;
81
82
            if (is_numeric($k)) {
83
                unset($includes[$k]);
84
85
                if (is_array($v)) {
86
                    $k = key($v);
87
                    $v = reset($v);
88
                } else {
89
                    $k = $v;
90
                }
91
            }
92
93
            $includes[$k] = $v;
94
95
            if (($pos = strrpos($k, '.')) !== false) {
96
                $includes[substr($k, 0, $pos)] = [substr($k, $pos + 1) => $includes[$k]];
97
                unset($includes[$k]);
98
            }
99
        }
100
101
        return $includes;
102
    }
103
104
    /**
105
     * Retrieves the value of an array element or object property with the given key or property name.
106
     *
107
     * @param $array
108
     * @param $key
109
     * @param null $default
110
     * @return mixed|null
111
     */
112
    private static function findInArray(array $array, string $key, $default = null)
113
    {
114
        if (($pos = strrpos($key, '.')) !== false) {
115
            $array = static::findInArray($array, substr($key, 0, $pos), $default);
0 ignored issues
show
Bug introduced by
Since findInArray() 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 findInArray() 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...
116
            $key = substr($key, $pos + 1);
117
        }
118
119
        return array_key_exists($key, $array) ? $array[$key] : $default;
120
    }
121
}
122