1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\ConnectionListenerException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PlainTcpConnection |
10
|
|
|
* @package Genkgo\Mail\Protocol |
11
|
|
|
* @codeCoverageIgnore |
12
|
|
|
*/ |
13
|
|
|
final class PlainTcpConnectionListener implements ConnectionListenerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $host; |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $port; |
23
|
|
|
/** |
24
|
|
|
* @var resource |
25
|
|
|
*/ |
26
|
|
|
private $resource; |
27
|
|
|
/** |
28
|
|
|
* @var float |
29
|
|
|
*/ |
30
|
|
|
private $timeout; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* PlainTcpConnection constructor. |
34
|
|
|
* @param string $host |
35
|
|
|
* @param int $port |
36
|
|
|
* @param float $timeout |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $host, int $port, float $timeout = -1) |
39
|
|
|
{ |
40
|
|
|
$this->host = $host; |
41
|
|
|
$this->port = $port; |
42
|
|
|
$this->timeout = $timeout; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ConnectionInterface |
47
|
|
|
* @throws ConnectionListenerException |
48
|
|
|
*/ |
49
|
|
|
public function listen(): ConnectionInterface |
50
|
|
|
{ |
51
|
|
|
$this->validateResource(); |
52
|
|
|
|
53
|
|
|
$resource = @stream_socket_accept($this->resource, $this->timeout); |
54
|
|
|
if (is_resource($resource)) { |
55
|
|
|
return new class($resource) extends AbstractConnection { |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $resource |
59
|
|
|
*/ |
60
|
|
|
public function __construct($resource) |
61
|
|
|
{ |
62
|
|
|
$this->resource = $resource; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function connect(): void |
69
|
|
|
{ |
70
|
|
|
$this->fireEvent('connect'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $type |
75
|
|
|
*/ |
76
|
|
|
public function upgrade(int $type): void |
77
|
|
|
{ |
78
|
|
|
if (stream_socket_enable_crypto($this->resource, true, $type) === false) { |
79
|
|
|
throw new \InvalidArgumentException('Cannot upgrade connection to requested encryption type'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
}; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new ConnectionListenerException('Could not accept connection'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
private function validateResource(): void |
92
|
|
|
{ |
93
|
|
|
if ($this->resource === null) { |
94
|
|
|
$this->resource = @stream_socket_server( |
95
|
|
|
'tcp://' . $this->host . ':' . $this->port, |
96
|
|
|
$errorCode, |
97
|
|
|
$errorMessage |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
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 theid
property of an instance of theAccount
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.