Completed
Push — master ( 787715...6d90b1 )
by Lars
02:34 queued 46s
created

Create.php ➔ array_key_last()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.9332
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 34
                foreach ($array as $key => $value) {
37 29
                    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 1
            return new Arrayy($data);
82
        }
83
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
         * @psalm-param T $type
94
         * @psalm-return Collection<int|string,T>
95
         */
96
        function collection($type, $data = []): Collection
97
        {
98 2
            return Collection::construct($type, $data);
99
        }
100
    }
101
102
    /**
103
     * @param array<mixed> $array
104
     *
105
     * @return mixed|null
106
     */
107
    function array_last(array $array)
108
    {
109 4
        $key_last = \array_key_last($array);
110 4
        if ($key_last === null) {
111 1
            return null;
112
        }
113
114 3
        return $array[$key_last];
115
    }
116
117
    /**
118
     * @param array<mixed> $array
119
     *
120
     * @return mixed|null
121
     */
122
    function array_first(array $array)
123
    {
124 10
        $key_first = array_key_first($array);
125 10
        if ($key_first === null) {
126 2
            return null;
127
        }
128
129 9
        return $array[$key_first];
130
    }
131
132
}
133