Completed
Push — master ( 633045...791dc4 )
by Lars
03:19
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
            function is_countable($var)
10
            {
11
                /** @noinspection PhpComposerExtensionStubsInspection */
12
                return \is_array($var)
13
                       ||
14
                       $var instanceof Countable
15
                       ||
16
                       $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...
17
                       ||
18
                       $var instanceof SimpleXMLElement;
19
            }
20
        }
21
22
        if (!\function_exists('array_key_first')) {
23
            function array_key_first(array $array)
24
            {
25 24
                foreach ($array as $key => $value) {
26 19
                    return $key;
27
                }
28
29 5
                return null;
30
            }
31
        }
32
33
        if (!\function_exists('array_key_last')) {
34
            function array_key_last(array $array)
35
            {
36 20
                if (\count($array) === 0) {
37 3
                    return null;
38
                }
39
40 17
                return \array_keys(
41 17
                    \array_slice($array, -1, 1, true)
42 17
                )[0];
43
            }
44
        }
45
    }
46
47
}
48
49
namespace Arrayy {
50
51
    use Arrayy\Collection\Collection;
52
    use Arrayy\Collection\CollectionInterface;
53
54
    if (!\function_exists('Arrayy\create')) {
55
        /**
56
         * Creates a Arrayy object.
57
         *
58
         * @param mixed $data
59
         *
60
         * @return Arrayy
61
         */
62
        function create($data): Arrayy
63
        {
64 1
            return new Arrayy($data);
65
        }
66
67
        /**
68
         * Creates a Collection object.
69
         *
70
         * @param string $type
71
         * @param mixed  $data
72
         *
73
         * @return CollectionInterface
74
         */
75
        function collection($type, $data = []): CollectionInterface
76
        {
77 2
            return new Collection($type, $data);
78
        }
79
    }
80
81
    /**
82
     * @param array $array
83
     *
84
     * @return mixed|null
85
     */
86
    function array_last(array $array)
87
    {
88 4
        $key_last = \array_key_last($array);
89 4
        if ($key_last === null) {
90 1
            return null;
91
        }
92
93 3
        return $array[$key_last];
94
    }
95
96
    /**
97
     * @param array $array
98
     *
99
     * @return mixed|null
100
     */
101
    function array_first(array $array)
102
    {
103 4
        $key_first = array_key_first($array);
104 4
        if ($key_first === null) {
105 1
            return null;
106
        }
107
108 3
        return $array[$key_first];
109
    }
110
111
}
112