Completed
Push — develop ( 95d441...014b83 )
by Michiel
02:13
created

StepupSelfService/SelfServiceBundle/Assert.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle;
20
21
use Assert\Assertion;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\AssertionFailedException;
23
24
final class Assert extends Assertion
25
{
26
    protected static $exceptionClass = '\Surfnet\StepupSelfService\SelfServiceBundle\Exception\AssertionFailedException';
27
28
    public static function keysAre(array $array, array $expectedKeys, $propertyPath = null)
29
    {
30
        $givenKeys = array_keys($array);
31
        sort($givenKeys);
32
        sort($expectedKeys);
33
        if ($givenKeys === $expectedKeys) {
34
            return;
35
        }
36
        $givenCount = count($givenKeys);
37
        $expectedCount = count($expectedKeys);
38
        if ($givenCount < $expectedCount) {
39
            $message = sprintf(
40
                'Required keys "%s" are missing',
41
                implode('", "', array_diff($expectedKeys, $givenKeys))
42
            );
43
        } elseif ($givenCount > $expectedCount) {
44
            $message = sprintf(
45
                'Additional keys "%s" found',
46
                implode('", "', array_diff($givenKeys, $expectedKeys))
47
            );
48
        } else {
49
            $additional = array_diff($givenKeys, $expectedKeys);
50
            $required = array_diff($expectedKeys, $givenKeys);
51
            $message = 'Keys do not match requirements';
52 View Code Duplication
            if (!empty($additional)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                $message .= sprintf(
54
                    ', additional keys "%s" found',
55
                    implode('", "', array_diff($givenKeys, $expectedKeys))
56
                );
57
            }
58 View Code Duplication
            if (!empty($required)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $message .= sprintf(
60
                    ', required keys "%s" are missing',
61
                    implode('", "', array_diff($expectedKeys, $givenKeys))
62
                );
63
            }
64
        }
65
        throw new AssertionFailedException($message, 0, $propertyPath, $array);
66
    }
67
}
68