These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace League\JsonGuard\Constraints; |
||
4 | |||
5 | use League\JsonGuard\Assert; |
||
6 | use League\JsonGuard\SubSchemaValidatorFactory; |
||
7 | use League\JsonGuard\ValidationError; |
||
8 | use League\JsonGuard\Validator; |
||
9 | |||
10 | class OneOf implements Constraint |
||
11 | { |
||
12 | const KEYWORD = 'oneOf'; |
||
13 | |||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | */ |
||
17 | 8 | public function validate($value, $parameter, Validator $validator) |
|
18 | { |
||
19 | 8 | Assert::type($parameter, 'array', self::KEYWORD, $validator->getPointer()); |
|
20 | 6 | Assert::notEmpty($parameter, self::KEYWORD, $validator->getPointer()); |
|
21 | |||
22 | 4 | $passed = 0; |
|
23 | 4 | View Code Duplication | foreach ($parameter as $schema) { |
0 ignored issues
–
show
|
|||
24 | 4 | $validator = $validator->makeSubSchemaValidator($value, $schema, $validator->getPointer()); |
|
25 | 4 | if ($validator->passes()) { |
|
26 | 4 | $passed++; |
|
27 | 4 | } |
|
28 | 4 | } |
|
29 | 4 | if ($passed !== 1) { |
|
30 | 4 | return new ValidationError( |
|
31 | 4 | 'Failed matching exactly one of the provided schemas.', |
|
32 | 4 | self::KEYWORD, |
|
33 | 4 | $value, |
|
34 | 4 | $validator->getPointer(), |
|
35 | 4 | ['one_of' => $parameter] |
|
36 | 4 | ); |
|
37 | } |
||
38 | |||
39 | 4 | return null; |
|
40 | } |
||
41 | } |
||
42 |
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.