GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

validate_array()   B
last analyzed

Complexity

Conditions 11
Paths 7

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 15
nc 7
nop 3
dl 0
loc 39
ccs 1
cts 1
cp 1
crap 11
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus;
4
5
use function array_filter;
6
use function array_key_exists;
7 3
use function array_keys;
8
use function assert;
9 3
use function count;
10
use function gettype;
11
use function in_array;
12 3
use function is_array;
13
use function is_string;
14 2
15 1
/**
16
 * @param array<string, mixed>                                   $data
17
 * @param array<string, string|array<string>>|array<int, string> $fields
18 2
 */
19
function validate_array(array $data, array $fields, ?string $exception = null): bool
20
{
21
    $isInt = count(array_filter(array_keys($fields), 'is_int')) === count($fields);
22 1
23
    foreach ($fields as $field => $type) {
24
        if ($isInt) {
25
            $field = $type;
26
            assert(is_string($field));
27
        }
28
29
        /** @psalm-suppress PossiblyInvalidArrayOffset */
30
        if ((! isset($data[$field]) && // This is faster,
31
                // but it will also return false on fields which
32
                // value is null, so calling array_key_exists when that happens.
33
                ! array_key_exists($field, $data)) ||
34
            (
35
                ! $isInt && (
36
                    (
37
                        is_string($type) && gettype($data[$field]) !== $type
38
                    ) ||
39
                    (
40
                        is_array($type) && ! in_array(gettype($data[$field]), $type, true)
41
                    )
42
                )
43
            )
44
        ) {
45
            if ($exception === null) {
46
                return false;
47
            }
48
49
            /**
50
             * @psalm-suppress InvalidThrow
51
             * @psalm-suppress InvalidStringClass
52
             */
53
            throw new $exception($data, $field);
54
        }
55
    }
56
57
    return true;
58
}
59