Completed
Push — master ( 535042...d7d013 )
by Ventaquil
02:21
created

Line1D   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPoint() 0 6 2
A countLength() 0 4 1
A __construct() 0 7 1
1
<?php
2
3
/**
4
 * @author ventaquil <[email protected]>
5
 * @licence MIT
6
 */
7
8
namespace PHPAlgorithms\GraphTools;
9
10
use PHPAlgorithms\GraphTools\Interfaces\LineInterface;
11
use PHPAlgorithms\GraphTools\Exceptions\LineException;
12
13
/**
14
 * Class Line1D
15
 * @package PHPAlgorithms\GraphTools
16
 * @property-read float $length
17
 * @property-read float $width
18
 */
19
class Line1D extends AbstractLine implements LineInterface {
20
    /**
21
     * @var float $length Line length property.
22
     * @var float $width Line width property.
23
     */
24
    protected $length,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
25
              $width;
26
27
    /**
28
     * Method checks sent variable and throws LineException if it is not an Point1D instance.
29
     *
30
     * @param mixed $point Variable to check.
31
     * @throws LineException If sent argument is not an Point1D instance.
32
     */
33
    protected function checkPoint($point)
34
    {
35
        if (!($point instanceof Point1D)) {
36
            throw new LineException('This is not a point');
37
        }
38
    }
39
40
    /**
41
     * Method counts length.
42
     *
43
     * return float Line length.
44
     */
45
    protected function countLength()
46
    {
47
        return abs($this->from->x - $this->to->x);
0 ignored issues
show
Bug introduced by
The property x does not seem to exist in PHPAlgorithms\GraphTools\Point.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
    }
49
50
    /**
51
     * Line1D constructor.
52
     *
53
     * @param mixed $from First end of the line.
54
     * @param mixed $to Second end of the line.
55
     */
56
    public function __construct($from, $to)
57
    {
58
        parent::__construct($from, $to);
59
60
        $this->width = $this->length
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->length = $this->countLength() can also be of type integer. However, the property $width is declared as type double. 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...
61
                     = $this->countLength();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->countLength() can also be of type integer. However, the property $length is declared as type double. 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...
62
    }
63
}
64