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\Constraint\DraftFour; |
||
4 | |||
5 | use League\JsonGuard\Assert; |
||
6 | use League\JsonGuard\ConstraintInterface; |
||
7 | use League\JsonGuard\Validator; |
||
8 | use function League\JsonGuard\error; |
||
9 | |||
10 | View Code Duplication | final class Maximum implements ConstraintInterface |
|
0 ignored issues
–
show
|
|||
11 | { |
||
12 | const KEYWORD = 'maximum'; |
||
13 | |||
14 | /** |
||
15 | * @var int|null |
||
16 | */ |
||
17 | private $precision; |
||
18 | |||
19 | /** |
||
20 | * @param int|null $precision |
||
21 | */ |
||
22 | 10 | public function __construct($precision = 10) |
|
23 | { |
||
24 | 10 | $this->precision = $precision; |
|
25 | 10 | } |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 10 | public function validate($value, $parameter, Validator $validator) |
|
31 | { |
||
32 | 10 | Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath()); |
|
33 | |||
34 | 8 | if (isset($validator->getSchema()->exclusiveMaximum) && $validator->getSchema()->exclusiveMaximum === true) { |
|
35 | 4 | return; |
|
36 | } |
||
37 | |||
38 | 8 | if (!is_numeric($value) || |
|
39 | 8 | bccomp($value, $parameter, $this->precision) === -1 || bccomp($value, $parameter, $this->precision) === 0) { |
|
40 | 8 | return null; |
|
41 | } |
||
42 | |||
43 | 6 | return error('The number must be less than {parameter}.', $validator); |
|
44 | } |
||
45 | } |
||
46 |
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.