Completed
Push — EventLoopContainer ( c9a84d...6e77fa )
by Vasily
04:04
created

Bootstrap::init()   F

Complexity

Conditions 97
Paths > 20000

Size

Total Lines 334
Code Lines 220

Duplication

Lines 59
Ratio 17.66 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 97
eloc 220
c 5
b 2
f 0
nc 4294967295
nop 1
dl 59
loc 334
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PHPDaemon\Core;
3
4
use PHPDaemon\Config\Entry\Generic;
5
use PHPDaemon\FS\FileSystem;
6
use PHPDaemon\Thread;
7
use PHPDaemon\Utils\DateTime;
8
use PHPDaemon\Utils\ShmEntity;
9
use PHPDaemon\Utils\Terminal;
10
11
/**
12
 * Bootstrap for PHPDaemon
13
 *
14
 * @package Core
15
 *
16
 * @author  Vasily Zorin <[email protected]>
17
 */
18
class Bootstrap
19
{
20
    use \PHPDaemon\Traits\ClassWatchdog;
21
    use \PHPDaemon\Traits\StaticObjectWatchdog;
22
23
    /**
24
     * Master process ID
25
     * @var integer
26
     */
27
    protected static $pid;
28
29
    /**
30
     * List of commands
31
     * @var array
32
     */
33
    protected static $commands = [
34
        'start',
35
        'stop',
36
        'hardstop',
37
        'gracefulstop',
38
        'update',
39
        'reload',
40
        'restart',
41
        'hardrestart',
42
        'fullstatus',
43
        'status',
44
        'configtest',
45
        'log',
46
        'runworker',
47
        'ipcpath',
48
        'reopenlog'
49
    ];
50
51
    /**
52
     * Command-line params
53
     * @var array
54
     */
55
    protected static $params = [
56
        'pid-file' => [
57
            'val' => '/path/to/pid-file',
58
            'desc' => 'Pid file'
59
        ],
60
        'max-requests' => [
61
            'desc' => 'Maximum requests to worker before respawn',
62
            'val' => [
63
                'n' => 'Count'
64
            ]
65
        ],
66
        'path' => [
67
            'desc' => 'Path to your application resolver',
68
            'val' => '/path/to/resolver.php'
69
        ],
70
        'config-file' => [
71
            'desc' => 'Paths to configuration file separated by semicolon. First found will be used.',
72
            'val' => '/path/to/file'
73
        ],
74
        'logging' => [
75
            'desc' => 'Logging status',
76
            'val' => [
77
                '0' => 'Disabled',
78
                '1' => 'Enabled'
79
            ]
80
        ],
81
        'log-storage' => [
82
            'desc' => 'Log storage',
83
            'val' => '/path/to/file'
84
        ],
85
        'user' => [
86
            'desc' => 'User of master process',
87
            'val' => 'username'
88
        ],
89
        'group' => [
90
            'desc' => 'Group of master process',
91
            'val' => 'groupname'
92
        ],
93
        'help' => 'This help information'
94
    ];
95
96
    /**
97
     * Actions on early startup.
98
     * @param string Optional. Config file path.
99
     * @return void
100
     */
101
    public static function init($configFile = null)
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
102
    {
103
        if (!version_compare(PHP_VERSION, '5.6.0', '>=')) {
104
            Daemon::log('PHP >= 5.6.0 required.');
105
            return;
106
        }
107
108
        //run without composer
109
        if (!function_exists('setTimeout')) {
110
            require 'PHPDaemon/Utils/func.php';
111
        }
112
113
        Daemon::initSettings();
114
        FileSystem::init();
115
        Daemon::$runName = basename($_SERVER['argv'][0]);
116
117
        $error = false;
118
        $argv = $_SERVER['argv'];
119
        $runmode = isset($argv[1]) ? str_replace('-', '', $argv[1]) : '';
120
        $args = Bootstrap::getArgs($argv);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
121
122
        if (!isset(self::$params[$runmode]) && !in_array($runmode, self::$commands)) {
123
            if ('' !== $runmode) {
124
                echo('Unrecognized command: ' . $runmode . "\n");
125
            }
126
127
            self::printUsage();
128
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
129
        } elseif ('help' === $runmode) {
130
            self::printHelp();
131
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
132
        }
133
134
        $n = null;
135
        if ('log' === $runmode) {
136
            if (isset($args['n'])) {
137
                $n = $args['n'];
138
                unset($args['n']);
139
            } else {
140
                $n = 20;
141
            }
142
        }
143
144
        if (isset($configFile)) {
145
            Daemon::$config->configfile->setHumanValue($configFile);
146
        }
147
        if (isset($args['configfile'])) {
148
            Daemon::$config->configfile->setHumanValue($args['configfile']);
149
        }
150
151
        if (!Daemon::$config->loadCmdLineArgs($args)) {
152
            $error = true;
153
        }
154
155
        if (!Daemon::loadConfig(Daemon::$config->configfile->value)) {
156
            $error = true;
157
        }
158
159
        if ('log' === $runmode) {
160
            passthru('tail -n ' . $n . ' -f ' . escapeshellarg(Daemon::$config->logstorage->value));
161
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
162
        }
163
164
        if (extension_loaded('apc') && ini_get('apc.enabled')) {
165
            Daemon::log('Detected pecl-apc extension enabled. Usage of bytecode caching (APC/eAccelerator/xcache/...)  makes no sense at all in case of using phpDaemon \'cause phpDaemon includes files just in time itself.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 224 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
166
        }
167
168
        if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') {
169
            setlocale(LC_ALL, array_map('trim', explode(',', Daemon::$config->locale->value)));
170
        }
171
172
        if (Daemon::$config->autoreimport->value && !is_callable('runkit_import')) {
173
            Daemon::log('[WARN] runkit extension not found. You should install it or disable --auto-reimport. Non-critical error.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
174
        }
175
176
        if (!is_callable('posix_kill')) {
177
            Daemon::log('[EMERG] Posix not found. You should compile PHP without \'--disable-posix\'.');
178
            $error = true;
179
        }
180
181
        if (!is_callable('pcntl_signal')) {
182
            Daemon::log('[EMERG] PCNTL not found. You should compile PHP with \'--enable-pcntl\'.');
183
            $error = true;
184
        }
185
186
        if (extension_loaded('libevent')) {
187
            Daemon::log('[EMERG] libevent extension found. You have to remove libevent.so extension.');
188
            $error = true;
189
        }
190
191
        $eventVer = '1.6.1';
192
        $eventVerType = 'stable';
193
        if (!Daemon::loadModuleIfAbsent('event', $eventVer . '-' . $eventVerType)) {
194
            Daemon::log('[EMERG] event extension >= ' . $eventVer . ' not found (or OUTDATED). You have to install it. `pecl install http://pecl.php.net/get/event`');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 166 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
195
            $error = true;
196
        }
197
198
        if (!is_callable('socket_create')) {
199
            Daemon::log('[EMERG] Sockets extension not found. You should compile PHP with \'--enable-sockets\'.');
200
            $error = true;
201
        }
202
203
        if (!is_callable('shmop_open')) {
204
            Daemon::log('[EMERG] Shmop extension not found. You should compile PHP with \'--enable-shmop\'.');
205
            $error = true;
206
        }
207
208
        if (!isset(Daemon::$config->user)) {
209
            Daemon::log('[EMERG] You must set \'user\' parameter.');
210
            $error = true;
211
        }
212
213
        if (!isset(Daemon::$config->path)) {
214
            Daemon::log('[EMERG] You must set \'path\' parameter (path to your application resolver).');
215
            $error = true;
216
        }
217
218
        if (!file_exists(Daemon::$config->pidfile->value)) {
219 View Code Duplication
            if (!touch(Daemon::$config->pidfile->value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
                Daemon::log('[EMERG] Couldn\'t create pid-file \'' . Daemon::$config->pidfile->value . '\'.');
221
                $error = true;
222
            }
223
224
            Bootstrap::$pid = 0;
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
225 View Code Duplication
        } elseif (!is_file(Daemon::$config->pidfile->value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
            Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be a regular file.');
227
            Bootstrap::$pid = false;
0 ignored issues
show
Documentation Bug introduced by
The property $pid was declared of type integer, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
228
            $error = true;
229
        } elseif (!is_writable(Daemon::$config->pidfile->value)) {
230
            Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be writable.');
231
            $error = true;
232 View Code Duplication
        } elseif (!is_readable(Daemon::$config->pidfile->value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
            Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be readable.');
234
            Bootstrap::$pid = false;
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
235
            $error = true;
236
        } else {
237
            Bootstrap::$pid = (int)file_get_contents(Daemon::$config->pidfile->value);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
238
        }
239
240
        if (Daemon::$config->chroot->value !== '/') {
241
            if (posix_getuid() != 0) {
242
                Daemon::log('You must have the root privileges to change root.');
243
                $error = true;
244
            }
245
        }
246
247
        $pathList = preg_split('~\s*;\s*~', Daemon::$config->path->value);
248
        $found = false;
249
        foreach ($pathList as $path) {
250
            if (@is_file($path)) {
251
                Daemon::$appResolverPath = $path;
252
                $found = true;
253
                break;
254
            }
255
        }
256
        if (!$found) {
257
            Daemon::log('Your application resolver \'' . Daemon::$config->path->value . '\' is not available (config directive \'path\').');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
258
            $error = true;
259
        }
260
261
        Daemon::$appResolver = require Daemon::$appResolverPath;
262
263 View Code Duplication
        if (isset(Daemon::$config->group->value) && is_callable('posix_getgid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
264
            if (($sg = posix_getgrnam(Daemon::$config->group->value)) === false) {
265
                Daemon::log('Unexisting group \'' . Daemon::$config->group->value . '\'. You have to replace config-variable \'group\' with existing group-name.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
266
                $error = true;
267
            } elseif (($sg['gid'] != posix_getgid()) && (posix_getuid() != 0)) {
268
                Daemon::log('You must have the root privileges to change group.');
269
                $error = true;
270
            }
271
        }
272
273 View Code Duplication
        if (isset(Daemon::$config->user->value) && is_callable('posix_getuid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
            if (($su = posix_getpwnam(Daemon::$config->user->value)) === false) {
275
                Daemon::log('Unexisting user \'' . Daemon::$config->user->value . '\', user not found. You have to replace config-variable \'user\' with existing username.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 174 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
276
                $error = true;
277
            } elseif (($su['uid'] != posix_getuid()) && (posix_getuid() != 0)) {
278
                Daemon::log('You must have the root privileges to change user.');
279
                $error = true;
280
            }
281
        }
282
283
        if (isset(Daemon::$config->minspareworkers->value)
284
            && Daemon::$config->minspareworkers->value > 0
285
            && isset(Daemon::$config->maxspareworkers->value)
286
            && Daemon::$config->maxspareworkers->value > 0
287
        ) {
288
            if (Daemon::$config->minspareworkers->value > Daemon::$config->maxspareworkers->value) {
289
                Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.');
290
                $error = true;
291
            }
292
        }
293
294
        if (isset(Daemon::$config->addincludepath->value)) {
295
            ini_set(
296
                'include_path',
297
                ini_get('include_path') . ':' . implode(':', Daemon::$config->addincludepath->value)
298
            );
299
        }
300
301
        if (isset(Daemon::$config->minworkers->value) && isset(Daemon::$config->maxworkers->value)) {
302
            if (Daemon::$config->minworkers->value > Daemon::$config->maxworkers->value) {
303
                Daemon::$config->minworkers->value = Daemon::$config->maxworkers->value;
304
            }
305
        }
306
307
        if ($runmode === 'start') {
308
            if ($error === false) {
309
                Bootstrap::start();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
310
            } else {
311
                exit(6);
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
312
            }
313
        } elseif ($runmode === 'runworker') {
314
            if ($error === false) {
315
                Bootstrap::runworker();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
316
            } else {
317
                exit(6);
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
318
            }
319
        } elseif ($runmode === 'status' || $runmode === 'fullstatus') {
320
            $status = Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid);
0 ignored issues
show
Bug Best Practice introduced by
The expression \PHPDaemon\Core\Bootstrap::$pid of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
321
            echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon::$config->pidfile->value . ").\n";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 157 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
322
323
            if ($status && ($runmode === 'fullstatus')) {
324
                echo 'Uptime: ' . DateTime::diffAsText(filemtime(Daemon::$config->pidfile->value), time()) . "\n";
325
326
                Daemon::$shm_wstate = new ShmEntity(Daemon::$config->pidfile->value, Daemon::SHM_WSTATE_SIZE, 'wstate');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PHPDaemon\Utils\Shm..._WSTATE_SIZE, 'wstate') of type object<PHPDaemon\Utils\ShmEntity> is incompatible with the declared type object<PHPDaemon\Thread\Collection> of property $shm_wstate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
327
328
                $stat = Daemon::getStateOfWorkers();
329
330
                echo "State of workers:\n";
331
                echo "\tTotal: " . $stat['alive'] . "\n";
332
                echo "\tIdle: " . $stat['idle'] . "\n";
333
                echo "\tBusy: " . $stat['busy'] . "\n";
334
                echo "\tShutdown: " . $stat['shutdown'] . "\n";
335
                echo "\tPre-init: " . $stat['preinit'] . "\n";
336
                echo "\tInit: " . $stat['init'] . "\n";
337
            }
338
339
            echo "\n";
340 View Code Duplication
        } elseif ($runmode === 'update') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
            if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGHUP))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \PHPDaemon\Core\Bootstrap::$pid of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
342
                echo '[UPDATE] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n";
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
343
            }
344
        } elseif ($runmode === 'reopenlog') {
345
            if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR1))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \PHPDaemon\Core\Bootstrap::$pid of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
346
                echo '[REOPEN-LOG] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n";
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 150 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
347
            }
348 View Code Duplication
        } elseif ($runmode === 'reload') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
349
            if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR2))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \PHPDaemon\Core\Bootstrap::$pid of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
350
                echo '[RELOAD] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n";
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
351
            }
352
        } elseif ($runmode === 'restart') {
353
            if ($error === false) {
354
                Bootstrap::stop(2);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
355
                Bootstrap::start();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
356
            }
357
        } elseif ($runmode === 'hardrestart') {
358
            Bootstrap::stop(3);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
359
            Bootstrap::start();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
360
        } elseif ($runmode === 'ipcpath') {
361
            $i = Daemon::$appResolver->getInstanceByAppName('\PHPDaemon\IPCManager\IPCManager');
362
            echo $i->getSocketUrl() . PHP_EOL;
363
        } elseif ($runmode === 'configtest') {
364
            $term = new Terminal;
365
            $term->enableColor();
366
367
            echo "\n";
368
369
            $rows = [];
370
371
            $rows[] = [
372
                'parameter' => 'PARAMETER',
373
                'value' => 'VALUE',
374
                '_color' => '37',
375
                '_bold' => true,
376
            ];
377
378
            foreach (Daemon::$config as $name => $entry) {
0 ignored issues
show
Bug introduced by
The expression \PHPDaemon\Core\Daemon::$config of type object<PHPDaemon\Config\Object> is not traversable.
Loading history...
379
                if (!$entry instanceof Generic) {
380
                    continue;
381
                }
382
383
                $row = [
384
                    'parameter' => $name,
385
                    'value' => var_export($entry->humanValue, true),
386
                ];
387
388
                if ($entry->defaultValue != $entry->humanValue) {
389
                    $row['value'] .= ' (' . var_export($entry->defaultValue, true) . ')';
390
                }
391
392
                $rows[] = $row;
393
            }
394
395
            $term->drawtable($rows);
396
397
            echo "\n";
398
        } elseif ($runmode === 'stop') {
399
            Bootstrap::stop();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
400
        } elseif ($runmode === 'gracefulstop') {
401
            Bootstrap::stop(4);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
402
        } elseif ($runmode === 'hardstop') {
403
            echo '[HARDSTOP] Sending SIGINT to ' . Bootstrap::$pid . '... ';
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
404
405
            $ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, SIGINT);
0 ignored issues
show
Bug Best Practice introduced by
The expression \PHPDaemon\Core\Bootstrap::$pid of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
406
407
            echo $ok ? 'OK.' : 'ERROR. It seems that phpDaemon is not running.';
408
409
            if ($ok) {
410
                $i = 0;
411
412
                while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) {
0 ignored issues
show
Unused Code introduced by
$r is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
413
                    usleep(500000);
414
415
                    if ($i === 9) {
416
                        echo "\nphpDaemon master-process hasn't finished. Sending SIGKILL... ";
417
                        posix_kill(Bootstrap::$pid, SIGKILL);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
418
                        sleep(0.2);
419
                        if (!Thread\Generic::ifExistsByPid(Bootstrap::$pid)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
420
                            echo " Oh, his blood is on my hands :'(";
421
                        } else {
422
                            echo "ERROR: Process alive. Permissions?";
423
                        }
424
425
                        break;
426
                    }
427
428
                    ++$i;
429
                }
430
            }
431
432
            echo "\n";
433
        }
434
    }
435
436
    /**
437
     * Print ussage
438
     * @return void
439
     */
440
    protected static function printUsage()
441
    {
442
        echo 'usage: ' . Daemon::$runName . " (start|(hard|graceful)stop|update|reload|reopenlog|(hard)restart|fullstatus|status|configtest|log|runworker|help) ...\n";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 167 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
443
    }
444
445
    /**
446
     * Print help
447
     * @return void
448
     */
449
    protected static function printHelp()
450
    {
451
        $term = new Terminal();
452
453
        echo 'phpDaemon ' . Daemon::$version . ". http://phpdaemon.net\n";
454
455
        self::printUsage();
456
457
        echo "\nAlso you can use some optional parameters to override the same configuration variables:\n";
458
459
        foreach (self::$params as $name => $desc) {
460
            if (empty($desc)) {
461
                continue;
462
            } elseif (!is_array($desc)) {
463
                $term->drawParam($name, $desc);
464
            } else {
465
                $term->drawParam(
466
                    $name,
467
                    isset($desc['desc']) ? $desc['desc'] : '',
468
                    isset($desc['val']) ? $desc['val'] : ''
469
                );
470
            }
471
        }
472
473
        echo "\n";
474
    }
475
476
    /**
477
     * Start master.
478
     * @return void
479
     */
480
    public static function start()
481
    {
482
        if (Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
483
            Daemon::log('[START] phpDaemon with pid-file \'' . Daemon::$config->pidfile->value . '\' is running already (PID ' . Bootstrap::$pid . ')');
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 152 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
484
            exit(6);
0 ignored issues
show
Coding Style Compatibility introduced by
The method start() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
485
        }
486
487
        Daemon::init();
488
        $pid = Daemon::spawnMaster();
489
        file_put_contents(Daemon::$config->pidfile->value, $pid);
490
    }
491
492
    /**
493
     * Runworker.
494
     * @return void
495
     */
496
    public static function runworker()
497
    {
498
        Daemon::log('PLEASE USE runworker COMMAND ONLY FOR DEBUGGING PURPOSES.');
499
        Daemon::init();
500
        Daemon::runWorker();
501
    }
502
503
    /**
504
     * Stop script.
505
     * @param int $mode
506
     * @return void
507
     */
508
    public static function stop($mode = 1)
509
    {
510
        $ok = Bootstrap::$pid && posix_kill(
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
511
                Bootstrap::$pid,
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
512
                $mode === 3 ? SIGINT : (($mode === 4) ? SIGTSTP : SIGTERM)
513
            );
514
515
        if (!$ok) {
516
            echo '[WARN]. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n";
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
517
        }
518
519
        if ($ok && ($mode > 1)) {
520
            $i = 0;
521
522
            while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) {
0 ignored issues
show
Unused Code introduced by
$r is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
523
                usleep(10000);
524
                ++$i;
525
            }
526
        }
527
    }
528
529
    /**
530
     * Parses command-line arguments.
531
     * @param array $args $_SERVER ['argv']
532
     * @return array Arguments
533
     */
534
    public static function getArgs($args)
535
    {
536
        $out = [];
537
        $last_arg = null;
538
539
        for ($i = 1, $il = sizeof($args); $i < $il; ++$i) {
540
            if (preg_match('~^--(.+)~', $args[$i], $match)) {
541
                $parts = explode('=', $match[1]);
542
                $key = preg_replace('~[^a-z0-9]+~', '', $parts[0]);
543
544
                if (isset($parts[1])) {
545
                    $out[$key] = $parts[1];
546
                } else {
547
                    $out[$key] = true;
548
                }
549
550
                $last_arg = $key;
551
            } elseif (preg_match('~^-([a-zA-Z0-9]+)~', $args[$i], $match)) {
552
                $key = null;
553
                for ($j = 0, $jl = mb_orig_strlen($match[1]); $j < $jl; ++$j) {
554
                    $key = $match[1]{$j};
555
                    $out[$key] = true;
556
                }
557
558
                $last_arg = $key;
559
            } elseif ($last_arg !== null) {
560
                $out[$last_arg] = $args[$i];
561
            }
562
        }
563
564
        return $out;
565
    }
566
}
567