Issues (4)

src/Address.php (1 issue)

1
<?php
2
/**
3
 * Network address (host and port)
4
 * User: moyo
5
 * Date: 21/09/2017
6
 * Time: 5:52 PM
7
 */
8
9
namespace Carno\Net;
10
11
class Address
12
{
13
    /**
14
     * @var string
15
     */
16
    private $host = null;
17
18
    /**
19
     * @var int
20
     */
21
    private $port = null;
22
23
    /**
24
     * @var bool
25
     */
26
    private $valid = null;
27
28
    /**
29
     * Address constructor.
30
     * @param string $host
31
     * @param int $port
32
     */
33
    public function __construct(string $host, int $port = null)
34
    {
35
        if (is_null($port)) {
36
            if (is_numeric($sep = strpos($host, ':')) && !strstr($host, '://')) {
37
                // ("host:port")
38
                $port = substr($host, $sep + 1);
39
                $host = substr($host, 0, $sep) ?: '0.0.0.0';
40
            } elseif (is_numeric($host)) {
41
                // ("port")
42
                $port = $host;
43
                $host = '127.0.0.1';
44
            } else {
45
                // ("host")
46
                $port = -1;
47
            }
48
        }
49
50
        $this->host = $host;
51
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
It seems like $port can also be of type string. However, the property $port is declared as type 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...
52
53
        $this->valid = $this->host && is_numeric($this->port);
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function host() : string
60
    {
61
        return $this->host;
62
    }
63
64
    /**
65
     * @return int
66
     */
67
    public function port() : int
68
    {
69
        return $this->port;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function valid() : bool
76
    {
77
        return $this->valid;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function __toString() : string
84
    {
85
        return sprintf('%s:%d', $this->host, $this->port);
86
    }
87
}
88