MultipleOf::setSpecification()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 5
nc 2
nop 1
crap 4
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $specification can also be of type string. However, the property $number is declared as type double|integer. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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