RedisDriver::getPort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SfCod\SocketIoBundle\Service;
4
5
use Predis\Client;
6
7
/**
8
 * Class RedisDriver.
9
 *
10
 * @package SfCod\SocketIoBundle\drivers
11
 */
12
class RedisDriver
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $dsn;
18
19
    /**
20
     * @var string
21
     */
22
    protected $host;
23
24
    /**
25
     * @var int
26
     */
27
    protected $port;
28
29
    /**
30
     * @var string
31
     */
32
    protected $user;
33
34
    /**
35
     * @var string
36
     */
37
    protected $password;
38
39
    /**
40
     * @var string
41
     */
42
    protected $database;
43
44
    /**
45
     * @var string
46
     */
47
    protected $socket;
48
49
    /**
50
     * @var
51
     */
52
    protected $client;
53
54
    /**
55
     * RedisDriver constructor.
56
     */
57
    public function __construct(string $dsn = 'redis://localhost:6379')
58
    {
59
        $this->dsn = $dsn;
60
        $this->parseDsn($dsn);
61
    }
62
63
    /**
64
     * Get redis client.
65
     */
66
    public function getClient(bool $reset = false): Client
67
    {
68
        if (is_null($this->client) || $reset) {
69
            $this->client = new Client([
70
                'scheme' => 'tcp',
71
                'read_write_timeout' => 0,
72
                'host' => $this->host,
73
                'port' => $this->port,
74
                'database' => $this->database,
75
                'password' => $this->password,
76
            ]);
77
        }
78
79
        return $this->client;
80
    }
81
82
    public function getHost(): string
83
    {
84
        return $this->host;
85
    }
86
87
    public function getPort(): int
88
    {
89
        return $this->port;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getUser(): ?string
96
    {
97
        return $this->user;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getPassword(): ?string
104
    {
105
        return $this->password;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getDatabase(): ?string
112
    {
113
        return $this->database;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getSocket(): ?string
120
    {
121
        return $this->socket;
122
    }
123
124
    /**
125
     * Parse dsn and get all needed attributes.
126
     *
127
     * @param string $dsn
128
     */
129
    protected function parseDsn($dsn)
130
    {
131
        $dsn = str_replace('redis://', '', $dsn); // remove "redis://"
132
        if (false !== $pos = strrpos($dsn, '@')) {
133
            // parse password
134
            $password = substr($dsn, 0, $pos);
135
            if (strstr($password, ':')) {
136
                list(, $password) = explode(':', $password, 2);
137
            }
138
            $this->password = urldecode($password);
139
            $dsn = substr($dsn, $pos + 1);
140
        }
141
        $dsn = preg_replace_callback('/\?(.*)$/', [$this, 'parseParameters'], $dsn); // parse parameters
142
        if (preg_match('#^(.*)/(\d+|%[^%]+%|env_\w+_[[:xdigit:]]{32,})$#', $dsn, $matches)) {
143
            // parse database
144
            $this->database = is_numeric($matches[2]) ? (int)$matches[2] : $matches[2];
0 ignored issues
show
Documentation Bug introduced by
It seems like is_numeric($matches[2]) ...atches[2] : $matches[2] can also be of type integer. However, the property $database is declared as type string. 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...
145
            $dsn = $matches[1];
146
        }
147
        if (preg_match('#^([^:]+)(:(\d+|%[^%]+%|env_\w+_[[:xdigit:]]{32,}))?$#', $dsn, $matches)) {
148
            if (!empty($matches[1])) {
149
                // parse host/ip or socket
150
                if ('/' === $matches[1][0]) {
151
                    $this->socket = $matches[1];
152
                } else {
153
                    $this->host = $matches[1];
154
                }
155
            }
156
            if (null === $this->socket && !empty($matches[3])) {
157
                // parse port
158
                $this->port = is_numeric($matches[3]) ? (int)$matches[3] : $matches[3];
0 ignored issues
show
Documentation Bug introduced by
It seems like is_numeric($matches[3]) ...atches[3] : $matches[3] 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...
159
            }
160
        }
161
    }
162
163
    /**
164
     * @param array $matches
165
     *
166
     * @return string
167
     */
168
    protected function parseParameters($matches)
169
    {
170
        parse_str($matches[1], $params);
171
        foreach ($params as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $params of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
172
            if (!$val) {
173
                continue;
174
            }
175
//            switch ($key) {
176
//                case 'weight':
177
//                    $this->weight = (int)$val;
178
//                    break;
179
//                case 'alias':
180
//                    $this->alias = $val;
181
//                    break;
182
//            }
183
        }
184
185
        return '';
186
    }
187
}
188