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; |
||
6 | use League\JsonGuard\ErrorCode; |
||
7 | use League\JsonGuard\ValidationError; |
||
8 | |||
9 | View Code Duplication | class Max implements ParentSchemaAwarePropertyConstraint |
|
0 ignored issues
–
show
|
|||
10 | { |
||
11 | /** |
||
12 | * {@inheritdoc} |
||
13 | */ |
||
14 | 14 | public static function validate($value, $schema, $parameter, $pointer = null) |
|
15 | { |
||
16 | 14 | if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) { |
|
17 | 6 | return self::validateExclusiveMax($value, $parameter, $pointer); |
|
18 | } |
||
19 | |||
20 | 14 | return self::validateMax($value, $parameter, $pointer); |
|
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param mixed $value |
||
25 | * @param mixed $parameter |
||
26 | * @param string|null $pointer |
||
27 | * |
||
28 | * @return \League\JsonGuard\ValidationError|null |
||
29 | */ |
||
30 | 14 | public static function validateMax($value, $parameter, $pointer = null) |
|
31 | { |
||
32 | 14 | if (!is_numeric($value) || |
|
33 | 14 | JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) { |
|
34 | 14 | return null; |
|
35 | } |
||
36 | |||
37 | 12 | $message = sprintf( |
|
38 | 12 | 'Number "%s" is not at most "%d"', |
|
39 | 12 | JsonGuard\as_string($value), |
|
40 | 12 | JsonGuard\as_string($parameter) |
|
41 | 12 | ); |
|
42 | 12 | return new ValidationError($message, ErrorCode::INVALID_MAX, $value, $pointer, ['max' => $parameter]); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param mixed $value |
||
47 | * @param mixed $parameter |
||
48 | * @param string|null $pointer |
||
49 | * |
||
50 | * @return \League\JsonGuard\ValidationError|null |
||
51 | */ |
||
52 | 6 | public static function validateExclusiveMax($value, $parameter, $pointer = null) |
|
53 | { |
||
54 | 6 | if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) { |
|
55 | 4 | return null; |
|
56 | } |
||
57 | |||
58 | 6 | $message = sprintf( |
|
59 | 6 | 'Number "%s" is not less than "%d"', |
|
60 | 6 | JsonGuard\as_string($value), |
|
61 | 6 | JsonGuard\as_string($parameter) |
|
62 | 6 | ); |
|
63 | 6 | return new ValidationError( |
|
64 | 6 | $message, |
|
65 | 6 | ErrorCode::INVALID_EXCLUSIVE_MAX, |
|
66 | 6 | $value, |
|
67 | 6 | $pointer, |
|
68 | 6 | ['exclusive_max' => $parameter] |
|
69 | 6 | ); |
|
70 | } |
||
71 | } |
||
72 |
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.