Completed
Pull Request — master (#17)
by Matt
02:30
created

Enum::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 17
loc 17
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\ErrorCode;
6
use League\JsonGuard\ValidationError;
7
use function League\JsonGuard\as_string;
8
9 View Code Duplication
class Enum implements PropertyConstraint
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 3
    public static function validate($value, $parameter, $pointer = null)
15
    {
16 3
        if (!is_array($parameter)) {
17 1
            return null;
18
        }
19
20 2
        if (in_array($value, $parameter, true)) {
21 2
            return null;
22
        }
23
24 2
        $message = sprintf(
25 2
            'Value "%s" is not one of: %s',
26 2
            as_string($value),
27 2
            implode(', ', array_map('League\JsonGuard\as_string', $parameter))
28 2
        );
29 2
        return new ValidationError($message, ErrorCode::INVALID_ENUM, $value, $pointer, ['choices' => $parameter]);
30
    }
31
}
32