Completed
Push — develop ( 6fbeb0...8de61d )
by Nate
01:53
created

TransformerHelper::normalizeIncludes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
     * @return array
60
     */
61
    public static function normalizeIncludes(TransformerInterface $transformer): array
62
    {
63
        return self::normalizeIncludesInternal($transformer->getIncludes());
64
    }
65
66
    /**
67
     * @param TransformerInterface $transformer
68
     * @param string $key
69
     * @return bool
70
     */
71
    public static function inInclude(TransformerInterface $transformer, string $key): bool
72
    {
73
        return self::findInArray(static::normalizeIncludes($transformer), $key) !== null;
74
    }
75
76
    /**
77
     * @param array $includes
78
     * @return array
79
     */
80
    private static function normalizeIncludesInternal(array $includes)
81
    {
82
        foreach ($includes as $k => $v) {
83
84
            if (is_string($v) && ($pos = strrpos($v, '.')) !== false) {
85
                $v = [substr($v, 0, $pos) => [substr($v, $pos + 1)]];
86
            }
87
88
            // normalize sub-includes
89
            $v = is_array($v) ? static::normalizeIncludesInternal($v) : $v;
0 ignored issues
show
Bug introduced by
Since normalizeIncludesInternal() 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 normalizeIncludesInternal() 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...
90
91
            if (is_numeric($k)) {
92
                unset($includes[$k]);
93
94
                if (is_array($v)) {
95
                    $k = key($v);
96
                    $v = reset($v);
97
                } else {
98
                    $k = $v;
99
                }
100
            }
101
102
            $includes[$k] = $v;
103
104
            if (($pos = strrpos($k, '.')) !== false) {
105
                $includes[substr($k, 0, $pos)] = [substr($k, $pos + 1) => $includes[$k]];
106
                unset($includes[$k]);
107
            }
108
        }
109
110
        return $includes;
111
    }
112
113
    /**
114
     * Retrieves the value of an array element or object property with the given key or property name.
115
     *
116
     * @param $array
117
     * @param $key
118
     * @param null $default
119
     * @return mixed|null
120
     */
121
    private static function findInArray(array $array, string $key, $default = null)
122
    {
123
        if (($pos = strrpos($key, '.')) !== false) {
124
            $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...
125
            $key = substr($key, $pos + 1);
126
        }
127
128
        return array_key_exists($key, $array) ? $array[$key] : $default;
129
    }
130
}
131