functions.php ➔ is_array_accessible()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Colada
4
{
5
    use Colada\X\LazyObjectProxy;
6
    use InvalidArgumentException;
7
8
    if (!function_exists('\\Colada\\x')) {
9
        /**
10
         * Represents future value (useful for processing collection elements with {@see \Colada\Collection::mapBy()}, for example).
11
         *
12
         * Example:
13
         * <code>
14
         * $users->acceptBy(\Colada\x()->isActive());
15
         * </code>
16
         *
17
         * And example for PHP 5.6:
18
         * <code>
19
         * use function Colada\x;
20
         *
21
         * $users->acceptBy(x()->isActive());
22
         * </code>
23
         *
24
         * vs.
25
         *
26
         * <code>
27
         * $users->acceptBy(
28
         *     function($user) { return $user->isActive(); }
29
         * );
30
         * </code>
31
         *
32
         * @api
33
         *
34
         * @param null|string $t Return class type hint (for PHPStorm, see .phpstorm.meta.php in the package's root)
35
         *
36
         * @return callable
37
         */
38
        function x($t = null)
0 ignored issues
show
Unused Code introduced by
The parameter $t is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
        {
40
            return new LazyObjectProxy();
41
        }
42
    }
43
44
    if (!function_exists('\\Colada\\lazy')) {
45
        /**
46
         * Wraps object to proxy, that collects all actions and plays them only when asked.
47
         *
48
         * Example:
49
         * <code>
50
         * use function Colada\lazy;
51
         *
52
         * $value = lazy(new \ArrayObject([1, 2, 3]))->count();
53
         *
54
         * echo $value(); // Will print "3".
55
         * </code>
56
         *
57
         * @api
58
         *
59
         * @param mixed $object
60
         *
61
         * @return callable
62
         */
63
        function lazy($object)
64
        {
65
            if (!is_object($object)) {
66
                throw new InvalidArgumentException('Only objects can be wrapped.');
67
            }
68
69
            return new LazyObjectProxy(function () use ($object) {
70
                return $object;
71
            });
72
        }
73
    }
74
}
75
76
namespace Colada\X
77
{
78
    use ArrayAccess;
79
    use Closure;
80
    use ReflectionFunction;
81
    use ReflectionMethod;
82
    use ReflectionObject;
83
84
    if (!function_exists('\\Colada\\X\\id')) {
85
        /**
86
         * @param $value
87
         *
88
         * @return mixed
89
         */
90
        function id($value)
91
        {
92
            return $value;
93
        }
94
    }
95
96
    if (!function_exists('\\Colada\\X\\is_array_accessible')) {
97
        /**
98
         * @param mixed $value
99
         *
100
         * @return bool
101
         */
102
        function is_array_accessible($value)
103
        {
104
            return is_array($value) || (is_object($value) && ($value instanceof ArrayAccess));
105
        }
106
    }
107
108
    if (!function_exists('\\Colada\\X\\as_closure')) {
109
        /**
110
         * Create \Closure view of given callable
111
         *
112
         * Closure style calling can speed up callback (see http://php.net/manual/en/reflectionfunction.getclosure.php
113
         * for details).
114
         *
115
         * @see https://wiki.php.net/rfc/closurefromcallable
116
         *
117
         * @param callable $callback
118
         *
119
         * @return Closure
120
         */
121
        function as_closure(callable $callback)
122
        {
123
            if (method_exists('Closure','fromCallable')) {
124
                // Available as of PHP 7.1
125
                $closure = Closure::fromCallable($callback);
126
            } else {
127
                if (is_object($callback) && ($callback instanceof Closure)) {
128
                    $closure = $callback;
129
                } elseif (is_object($callback)) {
130
                    // Object with __invoke().
131
                    $closure = (new ReflectionObject($callback))->getMethod('__invoke')->getClosure($callback);
132
                } elseif (is_array($callback) && is_object($callback[0])) {
133
                    // Object method.
134
                    $closure = (new ReflectionMethod($callback[0], $callback[1]))->getClosure($callback[0]);
135
                } elseif (is_array($callback)) {
136
                    // Static method.
137
                    $closure = (new ReflectionMethod($callback[0], $callback[1]))->getClosure();
138
                } else {
139
                    // Function name as a string.
140
                    $closure = (new ReflectionFunction($callback))->getClosure();
141
                }
142
            }
143
144
            return $closure;
145
        }
146
    }
147
}
148