| Conditions | 100 |
| Paths | > 20000 |
| Total Lines | 379 |
| Lines | 33 |
| Ratio | 8.71 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 107 | public static function init ($configFile = null) |
||
| 108 | { |
||
| 109 | if (!version_compare(PHP_VERSION, '5.6.0', '>=')) { |
||
| 110 | Daemon::log('PHP >= 5.6.0 required.'); |
||
| 111 | |||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | //run without composer |
||
| 116 | if (!function_exists('setTimeout')) { |
||
| 117 | require 'PHPDaemon/Utils/func.php'; |
||
| 118 | } |
||
| 119 | |||
| 120 | Daemon::initSettings(); |
||
| 121 | FileSystem::init(); |
||
| 122 | Daemon::$runName = basename($_SERVER['argv'][0]); |
||
| 123 | |||
| 124 | $error = false; |
||
| 125 | $argv = $_SERVER['argv']; |
||
| 126 | $runmode = isset($argv[1]) ? str_replace('-', '', $argv[1]) : ''; |
||
| 127 | $args = Bootstrap::getArgs($argv); |
||
| 128 | |||
| 129 | if ($runmode === 'pidfile') { |
||
| 130 | } |
||
| 131 | else if (!isset(self::$params[$runmode]) && !in_array($runmode, self::$commands)) { |
||
| 132 | if ('' !== $runmode) { |
||
| 133 | echo 'Unrecognized command: ' . $runmode . "\n"; |
||
| 134 | } |
||
| 135 | |||
| 136 | self::printUsage(); |
||
| 137 | exit; |
||
| 138 | } |
||
| 139 | else if ('help' === $runmode) { |
||
| 140 | self::printHelp(); |
||
| 141 | exit; |
||
| 142 | } |
||
| 143 | |||
| 144 | $n = null; |
||
| 145 | if ('log' === $runmode) { |
||
| 146 | if (isset($args['n'])) { |
||
| 147 | $n = $args['n']; |
||
| 148 | unset($args['n']); |
||
| 149 | } |
||
| 150 | else { |
||
| 151 | $n = 20; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if (isset($configFile)) { |
||
| 156 | Daemon::$config->configfile->setHumanValue($configFile); |
||
| 157 | } |
||
| 158 | if (isset($args['configfile'])) { |
||
| 159 | Daemon::$config->configfile->setHumanValue($args['configfile']); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!Daemon::$config->loadCmdLineArgs($args)) { |
||
| 163 | $error = true; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (!Daemon::loadConfig(Daemon::$config->configfile->value)) { |
||
| 167 | $error = true; |
||
| 168 | } |
||
| 169 | if ($runmode === 'pidfile') { |
||
| 170 | echo Daemon::$config->pidfile->value . PHP_EOL; |
||
| 171 | |||
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ('log' === $runmode) { |
||
| 176 | passthru('tail -n ' . $n . ' -f ' . escapeshellarg(Daemon::$config->logstorage->value)); |
||
| 177 | exit; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (extension_loaded('apc') && ini_get('apc.enabled')) { |
||
| 181 | Daemon::log( |
||
| 182 | '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.' |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') { |
||
| 187 | setlocale(LC_ALL, array_map('trim', explode(',', Daemon::$config->locale->value))); |
||
| 188 | } |
||
| 189 | |||
| 190 | if (Daemon::$config->autoreimport->value && !is_callable('runkit_import')) { |
||
| 191 | Daemon::log( |
||
| 192 | '[WARN] runkit extension not found. You should install it or disable --auto-reimport. Non-critical error.' |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (!is_callable('posix_kill')) { |
||
| 197 | Daemon::log('[EMERG] Posix not found. You should compile PHP without \'--disable-posix\'.'); |
||
| 198 | $error = true; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (!is_callable('pcntl_signal')) { |
||
| 202 | Daemon::log('[EMERG] PCNTL not found. You should compile PHP with \'--enable-pcntl\'.'); |
||
| 203 | $error = true; |
||
| 204 | } |
||
| 205 | |||
| 206 | if (extension_loaded('libevent')) { |
||
| 207 | Daemon::log('[EMERG] libevent extension found. You have to remove libevent.so extension.'); |
||
| 208 | $error = true; |
||
| 209 | } |
||
| 210 | |||
| 211 | $eventVer = '1.6.1'; |
||
| 212 | $eventVerType = 'stable'; |
||
| 213 | if (!Daemon::loadModuleIfAbsent('event', $eventVer . '-' . $eventVerType)) { |
||
| 214 | Daemon::log( |
||
| 215 | '[EMERG] event extension >= ' . $eventVer . ' not found (or OUTDATED). You have to install it. `pecl install http://pecl.php.net/get/event`' |
||
| 216 | ); |
||
| 217 | $error = true; |
||
| 218 | } |
||
| 219 | |||
| 220 | if (!is_callable('socket_create')) { |
||
| 221 | Daemon::log('[EMERG] Sockets extension not found. You should compile PHP with \'--enable-sockets\'.'); |
||
| 222 | $error = true; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (!is_callable('shmop_open')) { |
||
| 226 | Daemon::log('[EMERG] Shmop extension not found. You should compile PHP with \'--enable-shmop\'.'); |
||
| 227 | $error = true; |
||
| 228 | } |
||
| 229 | |||
| 230 | if (!isset(Daemon::$config->user)) { |
||
| 231 | Daemon::log('[EMERG] You must set \'user\' parameter.'); |
||
| 232 | $error = true; |
||
| 233 | } |
||
| 234 | |||
| 235 | if (!isset(Daemon::$config->path)) { |
||
| 236 | Daemon::log('[EMERG] You must set \'path\' parameter (path to your application resolver).'); |
||
| 237 | $error = true; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (!file_exists(Daemon::$config->pidfile->value)) { |
||
| 241 | $dir = dirname(Daemon::$config->pidfile->value); |
||
| 242 | is_dir($dir) || mkdir($dir, 0755, true); |
||
| 243 | if (!touch(Daemon::$config->pidfile->value)) { |
||
| 244 | Daemon::log('[EMERG] Couldn\'t create pid-file \'' . Daemon::$config->pidfile->value . '\'.'); |
||
| 245 | $error = true; |
||
| 246 | } |
||
| 247 | |||
| 248 | Bootstrap::$pid = 0; |
||
| 249 | } |
||
| 250 | else if (!is_file(Daemon::$config->pidfile->value)) { |
||
| 251 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be a regular file.'); |
||
| 252 | Bootstrap::$pid = false; |
||
|
|
|||
| 253 | $error = true; |
||
| 254 | } |
||
| 255 | else if (!is_writable(Daemon::$config->pidfile->value)) { |
||
| 256 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be writable.'); |
||
| 257 | $error = true; |
||
| 258 | } |
||
| 259 | else if (!is_readable(Daemon::$config->pidfile->value)) { |
||
| 260 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be readable.'); |
||
| 261 | Bootstrap::$pid = false; |
||
| 262 | $error = true; |
||
| 263 | } |
||
| 264 | else { |
||
| 265 | Bootstrap::$pid = (int)file_get_contents(Daemon::$config->pidfile->value); |
||
| 266 | } |
||
| 267 | |||
| 268 | if (Daemon::$config->chroot->value !== '/') { |
||
| 269 | if (posix_getuid() != 0) { |
||
| 270 | Daemon::log('You must have the root privileges to change root.'); |
||
| 271 | $error = true; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $pathList = preg_split('~\s*;\s*~', Daemon::$config->path->value); |
||
| 276 | $found = false; |
||
| 277 | foreach ($pathList as $path) { |
||
| 278 | if (@is_file($path)) { |
||
| 279 | Daemon::$appResolverPath = $path; |
||
| 280 | $found = true; |
||
| 281 | break; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | if (!$found) { |
||
| 285 | Daemon::log( |
||
| 286 | 'Your application resolver \'' . Daemon::$config->path->value . '\' is not available (config directive \'path\').' |
||
| 287 | ); |
||
| 288 | $error = true; |
||
| 289 | } |
||
| 290 | |||
| 291 | Daemon::$appResolver = require Daemon::$appResolverPath; |
||
| 292 | |||
| 293 | View Code Duplication | if (isset(Daemon::$config->group->value) && is_callable('posix_getgid')) { |
|
| 294 | if (($sg = posix_getgrnam(Daemon::$config->group->value)) === false) { |
||
| 295 | Daemon::log( |
||
| 296 | 'Unexisting group \'' . Daemon::$config->group->value . '\'. You have to replace config-variable \'group\' with existing group-name.' |
||
| 297 | ); |
||
| 298 | $error = true; |
||
| 299 | } |
||
| 300 | else if (($sg['gid'] != posix_getgid()) && (posix_getuid() != 0)) { |
||
| 301 | Daemon::log('You must have the root privileges to change group.'); |
||
| 302 | $error = true; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | View Code Duplication | if (isset(Daemon::$config->user->value) && is_callable('posix_getuid')) { |
|
| 307 | if (($su = posix_getpwnam(Daemon::$config->user->value)) === false) { |
||
| 308 | Daemon::log( |
||
| 309 | 'Unexisting user \'' . Daemon::$config->user->value . '\', user not found. You have to replace config-variable \'user\' with existing username.' |
||
| 310 | ); |
||
| 311 | $error = true; |
||
| 312 | } |
||
| 313 | else if (($su['uid'] != posix_getuid()) && (posix_getuid() != 0)) { |
||
| 314 | Daemon::log('You must have the root privileges to change user.'); |
||
| 315 | $error = true; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | if (isset(Daemon::$config->minspareworkers->value) |
||
| 320 | && Daemon::$config->minspareworkers->value > 0 |
||
| 321 | && isset(Daemon::$config->maxspareworkers->value) |
||
| 322 | && Daemon::$config->maxspareworkers->value > 0 |
||
| 323 | ) { |
||
| 324 | if (Daemon::$config->minspareworkers->value > Daemon::$config->maxspareworkers->value) { |
||
| 325 | Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.'); |
||
| 326 | $error = true; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | if (isset(Daemon::$config->addincludepath->value)) { |
||
| 331 | ini_set( |
||
| 332 | 'include_path', |
||
| 333 | ini_get('include_path') . ':' . implode(':', Daemon::$config->addincludepath->value) |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | |||
| 337 | if (isset(Daemon::$config->minworkers->value) && isset(Daemon::$config->maxworkers->value)) { |
||
| 338 | if (Daemon::$config->minworkers->value > Daemon::$config->maxworkers->value) { |
||
| 339 | Daemon::$config->minworkers->value = Daemon::$config->maxworkers->value; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | if ($runmode === 'start') { |
||
| 344 | if ($error === false) { |
||
| 345 | Bootstrap::start(); |
||
| 346 | } |
||
| 347 | else { |
||
| 348 | exit(6); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | else if ($runmode === 'runworker') { |
||
| 352 | if ($error === false) { |
||
| 353 | Bootstrap::runworker(); |
||
| 354 | } |
||
| 355 | else { |
||
| 356 | exit(6); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | else if ($runmode === 'status' || $runmode === 'fullstatus') { |
||
| 360 | $status = Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid); |
||
| 361 | echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon::$config->pidfile->value . ").\n"; |
||
| 362 | |||
| 363 | if ($status && ($runmode === 'fullstatus')) { |
||
| 364 | echo 'Uptime: ' . DateTime::diffAsText(filemtime(Daemon::$config->pidfile->value), time()) . "\n"; |
||
| 365 | |||
| 366 | Daemon::$shm_wstate = new ShmEntity(Daemon::$config->pidfile->value, Daemon::SHM_WSTATE_SIZE, 'wstate'); |
||
| 367 | |||
| 368 | $stat = Daemon::getStateOfWorkers(); |
||
| 369 | |||
| 370 | echo "State of workers:\n"; |
||
| 371 | echo "\tTotal: " . $stat['alive'] . "\n"; |
||
| 372 | echo "\tIdle: " . $stat['idle'] . "\n"; |
||
| 373 | echo "\tBusy: " . $stat['busy'] . "\n"; |
||
| 374 | echo "\tShutdown: " . $stat['shutdown'] . "\n"; |
||
| 375 | echo "\tPre-init: " . $stat['preinit'] . "\n"; |
||
| 376 | echo "\tInit: " . $stat['init'] . "\n"; |
||
| 377 | } |
||
| 378 | |||
| 379 | echo "\n"; |
||
| 380 | } |
||
| 381 | else if ($runmode === 'update') { |
||
| 382 | View Code Duplication | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGHUP))) { |
|
| 383 | echo '[UPDATE] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | else if ($runmode === 'reopenlog') { |
||
| 387 | View Code Duplication | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR1))) { |
|
| 388 | echo '[REOPEN-LOG] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | else if ($runmode === 'reload') { |
||
| 392 | View Code Duplication | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR2))) { |
|
| 393 | echo '[RELOAD] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | else if ($runmode === 'restart') { |
||
| 397 | if ($error === false) { |
||
| 398 | Bootstrap::stop(2); |
||
| 399 | Bootstrap::start(); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | else if ($runmode === 'hardrestart') { |
||
| 403 | Bootstrap::stop(3); |
||
| 404 | Bootstrap::start(); |
||
| 405 | } |
||
| 406 | else if ($runmode === 'ipcpath') { |
||
| 407 | $i = Daemon::$appResolver->getInstanceByAppName('\PHPDaemon\IPCManager\IPCManager'); |
||
| 408 | echo $i->getSocketUrl() . PHP_EOL; |
||
| 409 | } |
||
| 410 | else if ($runmode === 'configtest') { |
||
| 411 | $term = new Terminal; |
||
| 412 | $term->enableColor(); |
||
| 413 | |||
| 414 | echo "\n"; |
||
| 415 | |||
| 416 | $rows = []; |
||
| 417 | |||
| 418 | $rows[] = [ |
||
| 419 | 'parameter' => 'PARAMETER', |
||
| 420 | 'value' => 'VALUE', |
||
| 421 | '_color' => '37', |
||
| 422 | '_bold' => true, |
||
| 423 | ]; |
||
| 424 | |||
| 425 | foreach (Daemon::$config as $name => $entry) { |
||
| 426 | if (!$entry instanceof Generic) { |
||
| 427 | continue; |
||
| 428 | } |
||
| 429 | |||
| 430 | $row = [ |
||
| 431 | 'parameter' => $name, |
||
| 432 | 'value' => var_export($entry->humanValue, true), |
||
| 433 | ]; |
||
| 434 | |||
| 435 | if ($entry->defaultValue != $entry->humanValue) { |
||
| 436 | $row['value'] .= ' (' . var_export($entry->defaultValue, true) . ')'; |
||
| 437 | } |
||
| 438 | |||
| 439 | $rows[] = $row; |
||
| 440 | } |
||
| 441 | |||
| 442 | $term->drawtable($rows); |
||
| 443 | |||
| 444 | echo "\n"; |
||
| 445 | } |
||
| 446 | else if ($runmode === 'stop') { |
||
| 447 | Bootstrap::stop(); |
||
| 448 | } |
||
| 449 | else if ($runmode === 'gracefulstop') { |
||
| 450 | Bootstrap::stop(4); |
||
| 451 | } |
||
| 452 | else if ($runmode === 'hardstop') { |
||
| 453 | echo '[HARDSTOP] Sending SIGINT to ' . Bootstrap::$pid . '... '; |
||
| 454 | |||
| 455 | $ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, SIGINT); |
||
| 456 | |||
| 457 | echo $ok ? 'OK.' : 'ERROR. It seems that phpDaemon is not running.'; |
||
| 458 | |||
| 459 | if ($ok) { |
||
| 460 | $i = 0; |
||
| 461 | |||
| 462 | while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
||
| 463 | usleep(500000); |
||
| 464 | |||
| 465 | if ($i === 9) { |
||
| 466 | echo "\nphpDaemon master-process hasn't finished. Sending SIGKILL... "; |
||
| 467 | posix_kill(Bootstrap::$pid, SIGKILL); |
||
| 468 | sleep(0.2); |
||
| 469 | if (!Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
||
| 470 | echo " Oh, his blood is on my hands :'("; |
||
| 471 | } |
||
| 472 | else { |
||
| 473 | echo "ERROR: Process alive. Permissions?"; |
||
| 474 | } |
||
| 475 | |||
| 476 | break; |
||
| 477 | } |
||
| 478 | |||
| 479 | ++$i; |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | echo "\n"; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 648 |
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.