MultipleOf   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 6
c 4
b 2
f 2
lcom 1
cbo 1
dl 0
loc 37
ccs 9
cts 10
cp 0.9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSpecification() 0 9 4
A validate() 0 8 2
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