Completed
Push — master ( 811b94...d9b0b1 )
by Lars
01:45
created

Create.php ➔ array_key_first()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace {
6
7
    if (\PHP_VERSION_ID < 70300) {
8
        if (!\function_exists('is_countable')) {
9
            /**
10
             * @param mixed $var
11
             *
12
             * @return bool
13
             */
14
            function is_countable($var)
15
            {
16
                return \is_array($var)
17
                       ||
18
                       $var instanceof SimpleXMLElement
19
                       ||
20
                       $var instanceof Countable
21
                       ||
22
                       $var instanceof ResourceBundle;
0 ignored issues
show
Bug introduced by
The class ResourceBundle does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
23
            }
24
        }
25
26
        if (!\function_exists('array_key_first')) {
27
            /**
28
             * @param array<mixed> $array
29
             *
30
             * @return int|string|null
31
             */
32
            function array_key_first(array $array)
33
            {
34
                foreach ($array as $key => $value) {
35
                    return $key;
36
                }
37
38
                return null;
39
            }
40
        }
41
42
        if (!\function_exists('array_key_last')) {
43
            /**
44
             * @param array<mixed> $array
45
             *
46
             * @return int|string|null
47
             */
48
            function array_key_last(array $array)
49
            {
50
                if (\count($array) === 0) {
51
                    return null;
52
                }
53
54
                return \array_keys(
55
                    \array_slice($array, -1, 1, true)
56
                )[0];
57
            }
58
        }
59
    }
60
61
}
62
63
namespace Arrayy {
64
65
    use Arrayy\Collection\Collection;
66
    use Arrayy\TypeCheck\TypeCheckArray;
67
    use Arrayy\TypeCheck\TypeCheckInterface;
68
69
    if (!\function_exists('Arrayy\create')) {
70
        /**
71
         * Creates a Arrayy object.
72
         *
73
         * @param mixed $data
74
         *
75
         * @return Arrayy<int|string,mixed>
0 ignored issues
show
Documentation introduced by
The doc-type Arrayy<int|string,mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
76
         */
77
        function create($data): Arrayy
78
        {
79 2
            return new Arrayy($data);
80
        }
81
    }
82
83
    if (!\function_exists('Arrayy\collection')) {
84
        /**
85
         * Creates a Collection object.
86
         *
87
         * @param string|TypeCheckArray|TypeCheckInterface[] $type
88
         * @param array<mixed>                               $data
89
         *
90
         * @return Collection
91
         *
92
         * @template T
93
         * @phpstan-param T $type
94
         * @phpstan-return Collection<array-key,T>
95
         */
96
        function collection($type, $data = []): Collection
97
        {
98
            /** @phpstan-var Collection<array-key,T> */
99 2
            return Collection::construct($type, $data);
100
        }
101
    }
102
103
    /**
104
     * @param array<mixed> $array
105
     * @param mixed        $fallback <p>This fallback will be used, if the array is empty.</p>
106
     *
107
     * @return mixed|null
108
     *
109
     * @template TLast
110
     * @template TLastFallback
111
     * @phpstan-param TLast[] $array
112
     * @phpstan-param TLastFallback $fallback
113
     * @phpstan-return TLast|TLastFallback
114
     */
115
    function array_last(array $array, $fallback = null)
116
    {
117 4
        $key_last = \array_key_last($array);
118 4
        if ($key_last === null) {
119 1
            return $fallback;
120
        }
121
122 3
        return $array[$key_last];
123
    }
124
125
    /**
126
     * @param array<mixed> $array
127
     * @param mixed        $fallback <p>This fallback will be used, if the array is empty.</p>
128
     *
129
     * @return mixed|null
130
     *
131
     * @template TFirst
132
     * @template TFirstFallback
133
     * @phpstan-param TFirst[] $array
134
     * @phpstan-param TFirstFallback $fallback
135
     * @phpstan-return TFirst|TFirstFallback
136
     */
137
    function array_first(array $array, $fallback = null)
138
    {
139 10
        $key_first = \array_key_first($array);
140 10
        if ($key_first === null) {
141 2
            return $fallback;
142
        }
143
144 9
        return $array[$key_first];
145
    }
146
147
}
148