Completed
Push — master ( cde795...159245 )
by Lars
05:44
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
             * @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
                foreach ($array as $key => $value) {
37
                    return $key;
38
                }
39
40
                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
                if (\count($array) === 0) {
53
                    return null;
54
                }
55
56
                return \array_keys(
57
                    \array_slice($array, -1, 1, true)
58
                )[0];
59
            }
60
        }
61
    }
62
63
}
64
65
namespace Arrayy {
66
67
    use Arrayy\Collection\Collection;
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 1
            return new Arrayy($data);
80
        }
81
82
        /**
83
         * Creates a Collection object.
84
         *
85
         * @param string $type
86
         * @param mixed  $data
87
         *
88
         * @return Collection
89
         *
90
         * @template T
91
         * @psalm-param T $type
92
         * @psalm-return Collection<int|string,T>
93
         */
94
        function collection($type, $data = []): Collection
95
        {
96 2
            return Collection::construct($type, $data);
97
        }
98
    }
99
100
    /**
101
     * @param array<mixed> $array
102
     *
103
     * @return mixed|null
104
     */
105
    function array_last(array $array)
106
    {
107 4
        $key_last = \array_key_last($array);
108 4
        if ($key_last === null) {
109 1
            return null;
110
        }
111
112 3
        return $array[$key_last];
113
    }
114
115
    /**
116
     * @param array<mixed> $array
117
     *
118
     * @return mixed|null
119
     */
120
    function array_first(array $array)
121
    {
122 10
        $key_first = array_key_first($array);
123 10
        if ($key_first === null) {
124 2
            return null;
125
        }
126
127 9
        return $array[$key_first];
128
    }
129
130
}
131