Relation::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
/**
4
 * @author ventaquil <[email protected]>
5
 * @licence MIT
6
 */
7
8
namespace PHPAlgorithms\Dijkstra;
9
10
use PHPAlgorithms\Dijkstra\Exceptions\RelationException;
11
use PHPAlgorithms\GraphTools\AbstractLine;
12
use PHPAlgorithms\GraphTools\Traits\MagicGet;
13
14
/**
15
 * Class Relation
16
 * @package PHPAlgorithms\Dijkstra
17
 * @property integer $distance Relation's distance.
18
 */
19
class Relation extends AbstractLine {
20
    use MagicGet;
21
22
    /**
23
     * @var integer|null $distance Distance of relation. Defaults null.
24
     */
25
    private $distance;
26
27
    /**
28
     * Relation constructor.
29
     * 
30
     * @param Point $from Start object of relation.
31
     * @param Point $to End object of relation.
32
     * @param integer|null $distance Distance of relation. Must be greater than 0.
33
     */
34
    public function __construct($from, $to, $distance = null)
35
    {
36
        parent::__construct($from, $to);
37
38
        if (!is_null($distance)) {
39
            $this->setDistance($distance);
40
        }
41
    }
42
43
    /**
44
     * Method checks distance. If greater than 0 set sent argument as object parameter or throws exception otherwise.
45
     * 
46
     * @param integer $distance Distance of relation. Must be greater than 0.
47
     * @throws RelationException Method throws exception when $distance is less or equal to 0.
48
     */
49
    public function setDistance($distance)
50
    {
51
        if (!is_numeric($distance) && ($distance <= 0)) {
52
            throw new RelationException('Distance must be numeric value greater than 0');
53
        }
54
55
        $this->distance = $distance;
0 ignored issues
show
Documentation Bug introduced by
It seems like $distance can also be of type double or string. However, the property $distance is declared as type integer|null. 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...
56
    }
57
}
58