Completed
Push — master ( e8a46f...e8310f )
by Lars
02:03
created

Create.php ➔ array_key_first()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
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
             * @noinspection PhpComposerExtensionStubsInspection
15
             */
16
            function is_countable($var)
17
            {
18
                return \is_array($var)
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
                       $var instanceof SimpleXMLElement;
25
            }
26
        }
27
28
        if (!\function_exists('array_key_first')) {
29
            /**
30
             * @param array<mixed> $array
31
             *
32
             * @return int|string|null
33
             */
34
            function array_key_first(array $array)
35
            {
36 35
                foreach ($array as $key => $value) {
37 30
                    return $key;
38
                }
39
40 6
                return null;
41
            }
42
        }
43
44
        if (!\function_exists('array_key_last')) {
45
            /**
46
             * @param array<mixed> $array
47
             *
48
             * @return int|string|null
49
             */
50
            function array_key_last(array $array)
51
            {
52 21
                if (\count($array) === 0) {
53 3
                    return null;
54
                }
55
56 18
                return \array_keys(
57 18
                    \array_slice($array, -1, 1, true)
58 18
                )[0];
59
            }
60
        }
61
    }
62
63
}
64
65
namespace Arrayy {
66
67
    use Arrayy\Collection\Collection;
68
    use Arrayy\TypeCheck\TypeCheckArray;
69
    use Arrayy\TypeCheck\TypeCheckInterface;
70
71
    if (!\function_exists('Arrayy\create')) {
72
        /**
73
         * Creates a Arrayy object.
74
         *
75
         * @param mixed $data
76
         *
77
         * @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...
78
         */
79
        function create($data): Arrayy
80
        {
81 2
            return new Arrayy($data);
82
        }
83
    }
84
85
    if (!\function_exists('Arrayy\collection')) {
86
        /**
87
         * Creates a Collection object.
88
         *
89
         * @param string|TypeCheckArray|TypeCheckInterface[] $type
90
         * @param array<mixed>                               $data
91
         *
92
         * @return Collection
93
         *
94
         * @template T
95
         * @psalm-param T $type
96
         * @psalm-return Collection<int|string,T>
97
         */
98
        function collection($type, $data = []): Collection
99
        {
100 2
            return Collection::construct($type, $data);
101
        }
102
    }
103
104
    /**
105
     * @param array<mixed> $array
106
     * @param mixed        $fallback <p>This fallback will be used, if the array is empty.</p>
107
     *
108
     * @return mixed|null
109
     *
110
     * @template TLast
111
     * @template TLastFallback
112
     * @psalm-param TLast[] $array
113
     * @psalm-param TLastFallback $fallback
114
     * @psalm-return TLast|TLastFallback
115
     */
116
    function array_last(array $array, $fallback = null)
117
    {
118 4
        $key_last = \array_key_last($array);
119 4
        if ($key_last === null) {
120 1
            return $fallback;
121
        }
122
123 3
        return $array[$key_last];
124
    }
125
126
    /**
127
     * @param array<mixed> $array
128
     * @param mixed        $fallback <p>This fallback will be used, if the array is empty.</p>
129
     *
130
     * @return mixed|null
131
     *
132
     * @template TFirst
133
     * @template TFirstFallback
134
     * @psalm-param TFirst[] $array
135
     * @psalm-param TFirstFallback $fallback
136
     * @psalm-return TFirst|TFirstFallback
137
     */
138
    function array_first(array $array, $fallback = null)
139
    {
140 10
        $key_first = array_key_first($array);
141 10
        if ($key_first === null) {
142 2
            return $fallback;
143
        }
144
145 9
        return $array[$key_first];
146
    }
147
148
}
149