1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-request. |
4
|
|
|
* |
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\SimpleRequest\Validator; |
19
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface; |
21
|
|
|
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Validates that the given value is a number multiple of the specified number. |
25
|
|
|
* |
26
|
|
|
* @author mcustiel |
27
|
|
|
*/ |
28
|
|
|
class MultipleOf implements ValidatorInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var float|int |
33
|
|
|
*/ |
34
|
|
|
private $number; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* |
39
|
|
|
* @see \Mcustiel\SimpleRequest\Interfaces\Specificable::setSpecification() |
40
|
|
|
*/ |
41
|
87 |
|
public function setSpecification($specification = null) |
42
|
|
|
{ |
43
|
87 |
|
if (empty($specification) || !is_numeric($specification) || $specification <= 0) { |
44
|
4 |
|
throw new UnspecifiedValidatorException( |
45
|
|
|
'The validator MultipleOf is being initialized without a valid number' |
46
|
4 |
|
); |
47
|
|
|
} |
48
|
83 |
|
$this->number = $specification; |
|
|
|
|
49
|
83 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @see \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface::validate() |
55
|
|
|
*/ |
56
|
84 |
|
public function validate($value) |
57
|
|
|
{ |
58
|
84 |
|
if (!is_numeric($value)) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
84 |
|
return $value / $this->number == round($value / $this->number); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.