Issues (61)

src/Commands/WAMPCommandTrait.php (8 issues)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/3/17
6
 * Time: 6:08 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Thruway\Logging\ConsoleLogger;
12
use Thruway\Logging\Logger;
13
14
/**
15
 * @trait WAMPCommandTrait
16
 *
17
 * @author Donii Sergii <[email protected]>
18
 */
19
trait WAMPCommandTrait
20
{
21
    /**
22
     * WAMP router host.
23
     *
24
     * @var string
25
     */
26
    protected $host = '127.0.0.1';
27
28
    /**
29
     * Wamp realm to used.
30
     *
31
     * @var string
32
     */
33
    protected $realm = 'realm';
34
35
    /**
36
     * WAMP router port.
37
     *
38
     * @var int
39
     */
40
    protected $port = '9090';
41
42
    /**
43
     * Run in debug mode. Echo output to console.
44
     *
45
     * @var bool
46
     */
47
    protected $noDebug = false;
48
49
    /**
50
     * Run in loop or once.
51
     *
52
     * @var bool
53
     */
54
    protected $runOnce = false;
55
56
    /**
57
     * Specify the router protocol as wss.
58
     *
59
     * @var bool
60
     */
61
    protected $tls = false;
62
63
    /**
64
     * WAMP routes path.
65
     *
66
     * @var string
67
     *
68
     * @author Donii Sergii <[email protected]>
69
     */
70
    protected $routePath = null;
71
72
    /**
73
     * Transport provider class.
74
     *
75
     * @var string|\Thruway\Transport\RatchetTransportProvider|null
76
     *
77
     * @author Donii Sergii <[email protected]>
78
     */
79
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
80
81
    /**
82
     * Run in background.
83
     *
84
     * @var bool
85
     *
86
     * @author Donii Sergii <[email protected]>
87
     */
88
    protected $runInBackground = false;
89
90
    /**
91
     * No loop runner
92
     *
93
     * @var bool
94
     *
95
     * @author Donii Sergii <[email protected]>
96
     */
97
    protected $noLoop = false;
98
99
    /**
100
     * Get option value from input.
101
     *
102
     * @param string $optionName
103
     *
104
     * @return array|null|string
105
     */
106
    protected function getOptionFromInput(string $optionName, $default = null)
107
    {
108
        if (null === $this->input->getOption($optionName)) {
109
            return $default;
110
        }
111
112
        return $this->option($optionName);
0 ignored issues
show
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return $this->/** @scrutinizer ignore-call */ option($optionName);
Loading history...
113
    }
114
115
    /**
116
     * Get minion config.
117
     *
118
     * @param string|null $optName Option name
119
     * @param mixed       $default Default value
120
     *
121
     * @return array|string|int|null
122
     */
123
    protected function getConfig($optName = null, $default = null)
124
    {
125
        $options = config('wamp') ?? [];
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
        $options = /** @scrutinizer ignore-call */ config('wamp') ?? [];
Loading history...
126
127
        if (null === $optName) {
128
            return $options ?? [];
129
        }
130
131
        return isset($options[$optName]) ? $options[$optName] : $default;
132
    }
133
134
    /**
135
     * Get logger class for wamp
136
     *
137
     * @return \Psr\Log\LoggerInterface
138
     *
139
     * @author Donii Sergii <[email protected]>
140
     */
141
    abstract protected function getLogger();
142
143
    /**
144
     * Change WAMP logger
145
     *
146
     * @param string $fileName
147
     *
148
     * @return void
149
     *
150
     * @author Donii Sergii <[email protected]>
151
     */
152
    protected function changeWampLogger($fileName = 'wamp-server.log')
0 ignored issues
show
The parameter $fileName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

152
    protected function changeWampLogger(/** @scrutinizer ignore-unused */ $fileName = 'wamp-server.log')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
    {
154
        if (!$this->noDebug) {
155
            return;
156
        }
157
158
        Logger::set($this->getLogger());
159
    }
160
161
    /**
162
     * Parse base options
163
     *
164
     * @author Donii Sergii <[email protected]>
165
     */
166
    protected function parseBaseOptions()
167
    {
168
        $this->host = $this->getOptionFromInput('host') ?? $this->getConfig('host', $this->host);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ig('host', $this->host) can also be of type array. However, the property $host 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...
169
        $this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port', $this->port);
170
        $this->realm = $this->getOptionFromInput('realm') ?? $this->getConfig('realm', $this->realm);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...('realm', $this->realm) can also be of type array. However, the property $realm 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...
171
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
172
        $this->transportProvider = $this->getOptionFromInput('transport-provider') ?? $this->getConfig('transportProvider',
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...his->transportProvider) can also be of type array. However, the property $transportProvider is declared as type Thruway\Transport\Ratche...ortProvider|null|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...
173
                $this->transportProvider);
174
        $this->runInBackground = $this->getOptionFromInput('in-background') ?? false;
175
176
        $this->noDebug = $this->getOptionFromInput('no-debug') ?? $this->noDebug;
177
        $this->runOnce = $this->getOptionFromInput('no-loop') ?? $this->runOnce;
178
179
        $this->routePath = $this->getOptionFromInput('route-path') ?? $this->getConfig('routePath', $this->routePath);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ath', $this->routePath) can also be of type array. However, the property $routePath 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...
180
        $this->routePath = is_string($this->routePath) ? realpath($this->routePath) : null;
181
182
        if (!$this->noDebug) {
183
            Logger::set(new ConsoleLogger());
184
        }
185
    }
186
187
    /**
188
     * Parse input options
189
     *
190
     * @return mixed
191
     *
192
     * @author Donii Sergii <[email protected]>
193
     */
194
    abstract protected function parseOptions();
195
196
    /**
197
     * Get transport provider
198
     *
199
     * @return mixed
200
     *
201
     * @author Donii Sergii <[email protected]>
202
     */
203
    abstract protected function getTransportProvider();
204
205
    /**
206
     * Add pid process
207
     *
208
     * @param int    $pid      Process pid
209
     * @param string $fileName Filename
210
     *
211
     * @author Donii Sergii <[email protected]>
212
     */
213
    protected function addPidToLog($pid, $fileName = 'server.pids')
214
    {
215
        $fileName = storage_path($fileName);
0 ignored issues
show
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        $fileName = /** @scrutinizer ignore-call */ storage_path($fileName);
Loading history...
216
        if (!file_exists(storage_path($fileName))) {
217
            file_put_contents($fileName, $pid);
218
219
            return;
220
        }
221
222
        $content = file_get_contents($fileName);
223
224
        file_put_contents($fileName, $content.PHP_EOL.$pid);
225
    }
226
}
227