Completed
Push — master ( 9bf1e4...223d51 )
by Sergii
04:53
created

WAMPCommandTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 163
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionFromInput() 0 7 2
A changeWampLogger() 0 15 2
A getConfig() 0 9 3
A parseBaseOptions() 0 17 3
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 Monolog\Formatter\LineFormatter;
0 ignored issues
show
Bug introduced by
The type Monolog\Formatter\LineFormatter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Monolog\Handler\StreamHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\StreamHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Monolog\Logger as MonologLogger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Thruway\Logging\ConsoleLogger;
15
use Thruway\Logging\Logger;
16
17
/**
18
 * @trait  WAMPCommandTrait
19
 *
20
 * @author Donii Sergii <[email protected]>
21
 */
22
trait WAMPCommandTrait
23
{
24
    /**
25
     * WAMP router host.
26
     *
27
     * @var string
28
     */
29
    protected $host = '127.0.0.1';
30
31
    /**
32
     * Wamp realm to used.
33
     *
34
     * @var string
35
     */
36
    protected $realm;
37
38
    /**
39
     * WAMP router port.
40
     *
41
     * @var int
42
     */
43
    protected $port = '9090';
44
45
    /**
46
     * Run in debug mode. Echo output to console.
47
     *
48
     * @var bool
49
     */
50
    protected $noDebug = false;
51
52
    /**
53
     * Run in loop or once.
54
     *
55
     * @var bool
56
     */
57
    protected $runOnce = false;
58
59
    /**
60
     * Specify the router protocol as wss.
61
     *
62
     * @var bool
63
     */
64
    protected $tls = false;
65
66
    /**
67
     * WAMP routes path.
68
     *
69
     * @var string
70
     *
71
     * @author Donii Sergii <[email protected]>
72
     */
73
    protected $routePath = null;
74
75
    /**
76
     * Transport provider class.
77
     *
78
     * @var string|\Thruway\Transport\RatchetTransportProvider|null
79
     *
80
     * @author Donii Sergii <[email protected]>
81
     */
82
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
83
84
    /**
85
     * Get option value from input.
86
     *
87
     * @param string $optionName
88
     *
89
     * @return array|null|string
90
     */
91
    protected function getOptionFromInput(string $optionName, $default = null)
92
    {
93
        if (null === $this->input->getOption($optionName)) {
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist on sonrac\WAMP\Commands\WAMPCommandTrait. Did you maybe forget to declare it?
Loading history...
94
            return $default;
95
        }
96
97
        return $this->option($optionName);
0 ignored issues
show
Bug introduced by
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

97
        return $this->/** @scrutinizer ignore-call */ option($optionName);
Loading history...
98
    }
99
100
    /**
101
     * Get minion config.
102
     *
103
     * @param string|null $optName Option name
104
     * @param mixed       $default Default value
105
     *
106
     * @return array|string|int|null
107
     */
108
    protected function getConfig($optName = null, $default = null)
109
    {
110
        $options = config('minion') ?? [];
0 ignored issues
show
Bug introduced by
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

110
        $options = /** @scrutinizer ignore-call */ config('minion') ?? [];
Loading history...
111
112
        if (null === $optName) {
113
            return $options ?? [];
114
        }
115
116
        return isset($options[$optName]) ? $options[$optName] : $default;
117
    }
118
119
    /**
120
     * Change WAMP logger
121
     *
122
     * @param string $fileName
123
     *
124
     * @author Donii Sergii <[email protected]>
125
     */
126
    protected function changeWampLogger($fileName = 'wamp-server.log')
127
    {
128
129
        if (!$this->noDebug) {
130
            return;
131
        }
132
133
        $path = $this->getConfig('pathLogFile') ?? storage_path('logs/' . $fileName);
0 ignored issues
show
Bug introduced by
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

133
        $path = $this->getConfig('pathLogFile') ?? /** @scrutinizer ignore-call */ storage_path('logs/' . $fileName);
Loading history...
134
135
        $handler = (new StreamHandler($path, MonologLogger::DEBUG))
136
            ->setFormatter(new LineFormatter(null, null, true, true));
137
138
        $logger = new MonologLogger($fileName, [$handler]);
139
140
        Logger::set($logger);
141
    }
142
143
    /**
144
     * Parse base options
145
     *
146
     * @author Donii Sergii <[email protected]>
147
     */
148
    protected function parseBaseOptions()
149
    {
150
        $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...
151
        $this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port', $this->port);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ig('port', $this->port) can also be of type string or array. 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...
152
        $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...
153
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
154
        $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 null|string|Thruway\Tran...atchetTransportProvider. 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...
155
                $this->transportProvider);
156
157
        $this->noDebug = $this->getOptionFromInput('no-debug') ?? $this->noDebug;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...bug') ?? $this->noDebug can also be of type string or array. However, the property $noDebug is declared as type boolean. 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...
158
        $this->runOnce = $this->getOptionFromInput('no-loop') ?? $this->runOnce;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...oop') ?? $this->runOnce can also be of type string or array. However, the property $runOnce is declared as type boolean. 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
        $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...
161
        $this->routePath = is_string($this->routePath) ? realpath($this->routePath) : null;
162
163
        if (!$this->noDebug) {
164
            Logger::set(new ConsoleLogger());
165
        }
166
    }
167
168
    /**
169
     * Parse input options
170
     *
171
     * @return mixed
172
     *
173
     * @author Donii Sergii <[email protected]>
174
     */
175
    abstract protected function parseOptions();
176
177
    /**
178
     * Get transport provider
179
     *
180
     * @return mixed
181
     *
182
     * @author Donii Sergii <[email protected]>
183
     */
184
    abstract protected function getTransportProvider();
185
}