Assert::keysAre()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 7
nop 3
dl 0
loc 38
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2016 SURFnet B.V.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle;
22
23
use Assert\Assertion;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\AssertionFailedException;
25
26
final class Assert extends Assertion
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Assert
Loading history...
27
{
28
    protected static $exceptionClass = AssertionFailedException::class;
29
30
    public static function keysAre(array $array, array $expectedKeys, $propertyPath = null): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function keysAre()
Loading history...
31
    {
32
        $givenKeys = array_keys($array);
33
        sort($givenKeys);
34
        sort($expectedKeys);
35
        if ($givenKeys === $expectedKeys) {
36
            return;
37
        }
38
        $givenCount = count($givenKeys);
39
        $expectedCount = count($expectedKeys);
40
        if ($givenCount < $expectedCount) {
41
            $message = sprintf(
42
                'Required keys "%s" are missing',
43
                implode('", "', array_diff($expectedKeys, $givenKeys))
44
            );
45
        } elseif ($givenCount > $expectedCount) {
46
            $message = sprintf(
47
                'Additional keys "%s" found',
48
                implode('", "', array_diff($givenKeys, $expectedKeys))
49
            );
50
        } else {
51
            $additional = array_diff($givenKeys, $expectedKeys);
52
            $required = array_diff($expectedKeys, $givenKeys);
53
            $message = 'Keys do not match requirements';
54
            if ($additional !== []) {
55
                $message .= sprintf(
56
                    ', additional keys "%s" found',
57
                    implode('", "', array_diff($givenKeys, $expectedKeys))
58
                );
59
            }
60
            if ($required !== []) {
61
                $message .= sprintf(
62
                    ', required keys "%s" are missing',
63
                    implode('", "', array_diff($expectedKeys, $givenKeys))
64
                );
65
            }
66
        }
67
        throw new AssertionFailedException($message, 0, $propertyPath, $array);
68
    }
69
}
70