Completed
Push — master ( 195fbe...724c19 )
by Ventaquil
02:47
created

Point::addRelationObject()   A

Complexity

Conditions 3
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 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PHPAlgorithms\Dijkstra;
4
5
use PHPAlgorithms\Dijkstra\Exceptions\PointException;
6
use PHPAlgorithms\GraphTools\AbstractPoint;
7
use PHPAlgorithms\GraphTools\Traits\MagicGet;
8
9
class Point extends AbstractPoint
10
{
11
    use MagicGet;
12
13
    private $label = '';
14
    private $relations = array();
15
16
    public function __construct($label = null)
17
    {
18
        if (!is_null($label)) {
19
            $this->setLabel($label);
20
        }
21
    }
22
23
    public function __isset($name)
24
    {
25
        return isset($this->{$name});
26
    }
27
28
    public function addDoubleRelation()
29
    {
30
        switch (func_num_args()) {
31
            case 1:
32
                $arg = func_get_arg(0);
33
34
                $this->addRelation($arg);
35
                if ($arg->from === $this) {
36
                    $arg->to
37
                        ->addRelation($arg);
38
                } else {
39
                    $arg->from
40
                        ->addRelation($arg);
41
                }
42
                break;
43
            case 2:
44
                $this->addRelation(func_get_arg(0), func_get_arg(1));
45
                func_get_arg(0)->addRelation($this, func_get_arg(1));
46
                break;
47
            default:
48
                throw new PointException('Wrong arguments sent to addRelation() method');
49
        }
50
51
        return $this;
52
    }
53
54
    public function addRelation()
55
    {
56
        switch (func_num_args()) {
57
            case 1:
58
                $this->addRelationObject(func_get_arg(0));
59
                break;
60
            case 2:
61
                $this->addRelationPointDistance(func_get_arg(0), func_get_arg(1));
62
                break;
63
            default:
64
                throw new PointException('Wrong arguments sent to addRelation() method');
65
        }
66
67
        return $this;
68
    }
69
70
    private function addRelationObject(Relation $relation)
71
    {
72
        if (($this !== $relation->from) && ($this !== $relation->to)) {
0 ignored issues
show
Documentation introduced by
The property $from is declared protected in PHPAlgorithms\GraphTools\AbstractLine. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property $to is declared protected in PHPAlgorithms\GraphTools\AbstractLine. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
            throw new PointException('Sent relation is not match to this point');
74
        }
75
76
        $this->relations[] = $relation;
77
    }
78
79
    private function addRelationPointDistance(Point $point, $distance)
80
    {
81
        $this->addRelationObject(new Relation($this, $point, $distance));
82
    }
83
84
    public function setLabel($label)
85
    {
86
        if (!is_string($label)) {
87
            throw new PointException('Label is not a string');
88
        }
89
90
        $this->label = $label;
91
    }
92
}
93