fucongcong/framework

View on GitHub
core/Group/Async/Server/Server.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
 
namespace Group\Async\Server;
 
use swoole_server;
use Group\Common\ArrayToolkit;
use swoole_table;
 
The property $task_res is not named in camelCase.
Whitespace found at end of line
class Server
Expected 0 spaces before opening brace; 1 found
{
Spaces must be used to indent lines; tabs are not allowed
protected $serv;
 
protected $servName;
 
protected $config;
 
protected $table;
 
protected $task_res;
 
Incorrect spacing between default value and equals sign for argument "$config"; expected 1 but found 0
Spaces must be used to indent lines; tabs are not allowed
Arguments with default values must be at the end of the argument list
public function __construct($config =[], $servName)
Spaces must be used to indent lines; tabs are not allowed
Whitespace found at end of line
{
$this->serv = new swoole_server($config['serv'], $config['port']);
$this->serv->set($config['config']);
 
$this->serv->on('Start', [$this, 'onStart']);
$this->serv->on('WorkerStart', [$this, 'onWorkerStart']);
$this->serv->on('WorkerStop', [$this, 'onWorkerStop']);
$this->serv->on('WorkerError', [$this, 'onWorkerError']);
$this->serv->on('Receive', [$this, 'onReceive']);
$this->serv->on('Task', [$this, 'onTask']);
$this->serv->on('Finish', [$this, 'onFinish']);
 
$this->initConfig($config);
 
$this->servName = $servName;
$this->serv->start();
Spaces must be used to indent lines; tabs are not allowed
}
 
Avoid unused parameters such as '$serv'.
public function onStart(swoole_server $serv)
{
if (PHP_OS !== 'Darwin') {
swoole_set_process_name("php {$this->servName}: master");
}
echo $this->servName." Start...", PHP_EOL;
}
 
Function `onWorkerStart` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
public function onWorkerStart(swoole_server $serv, $workerId)
{
opcache_reset();
$loader = require __ROOT__.'/vendor/autoload.php';
$loader->setUseIncludePath(true);
Missing class import via use statement (line '54', column '20').
$app = new \Group\App\App();
$app->initSelf();
$app->registerServices();
$app->singleton('container')->setAppPath(__ROOT__);
 
//设置不同进程名字,方便grep管理
if (PHP_OS !== 'Darwin') {
if ($workerId >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$this->servName}: task");
The method onWorkerStart uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
swoole_set_process_name("php {$this->servName}: worker");
}
}
// 判定是否为Task Worker进程
if ($workerId >= $serv->setting['worker_num']) {
The method onWorkerStart uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
$this->createTaskTable();
}
}
 
public function onWorkerStop(swoole_server $serv, $workerId)
{
if ($workerId >= $serv->setting['worker_num']) {
echo 'Task #'. ($workerId - $serv->setting['worker_num']). ' Ended.'. PHP_EOL;
The method onWorkerStop uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
echo 'Worker #'. $workerId, ' Ended.'. PHP_EOL;
}
}
 
Avoid unused parameters such as '$serv'.
Avoid unused parameters such as '$workerPid'.
public function onWorkerError(swoole_server $serv, $workerId, $workerPid, $exitCode)
{
echo "[", date('Y-m-d H:i:s'), "] Process Crash : Wid : $workerId error_code : $exitCode", PHP_EOL;
}
 
Avoid variables with short names like $fd. Configured minimum length is 3.
public function onReceive(swoole_server $serv, $fd, $fromId, $data)
Whitespace found at end of line
{
$data = trim($data);
$data = explode($serv->setting['package_eof'], $data);
Avoid unused local variables such as '$return'.
$return = '';
try {
$config = $this->config;
Expected 1 space after closing parenthesis; found 0
Expected 1 space after FOREACH keyword; 0 found
foreach($data as $one){
Avoid unused local variables such as '$info'.
list($cmd, $one, $info) = \Group\Async\DataPack::unpack($one);
if (isset($config['onWork'][$cmd])) {
$this->task_res[$fd] = [];
$handler = new $config['onWork'][$cmd]['handler']($serv, $fd, $fromId, $one, $cmd, $this->table);
$handler->handle();
}
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
 
Avoid variables with short names like $fd. Configured minimum length is 3.
public function onTask(swoole_server $serv, $fd, $fromId, $data)
{
try {
list($cmd, $one, $info) = \Group\Async\DataPack::unpack($data);
$config = $this->config;
if (isset($config['onTask'][$cmd])) {
Line exceeds 120 characters; contains 137 characters
$handler = new $config['onTask'][$cmd]['handler']($serv, $fd, $fromId, ['data' => $one, 'info' => $info, 'cmd' => $cmd]);
return $handler->handle();
}
} catch (\Exception $e) {
echo $e->getMessage();
}
return null;
}
 
Function `onFinish` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Avoid unused parameters such as '$fd'.
Avoid variables with short names like $fd. Configured minimum length is 3.
The variable $task_count is not named in camelCase.
public function onFinish(swoole_server $serv, $fd, $data)
{
try {
list($cmd, $one, $info) = \Group\Async\DataPack::unpack($data);
$config = $this->config;
if (isset($config['onTask'][$cmd])) {
$this->updateTaskCount($info['fd'], -1);
if (!isset($config['onTask'][$cmd]['onFinish'])) {
$return = $one;
The method onFinish uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
$handler = new $config['onTask'][$cmd]['onFinish']($serv, $info['fd'], $one, $this->table);
$return = $handler->handle();
}
Inline control structures are not allowed
if ($return) $this->task_res[$info['fd']][] = $return;
 
//返回数据
$task_count = $this->getTaskCount($info['fd']);
Expected 0 spaces before closing bracket; 1 found
Expected 0 spaces after opening bracket; 1 found
if ( $task_count <= 0 ) {
$this->sendData($serv, $info['fd'], $this->task_res[$info['fd']]);
$this->task_res[$info['fd']] = [];
$this->table->del($info['fd']);
}
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
 
Avoid variables with short names like $fd. Configured minimum length is 3.
Opening brace should be on a new line
private function sendData(swoole_server $serv, $fd, $data){
$fdinfo = $serv->connection_info($fd);
Expected 1 space after IF keyword; 0 found
Expected 1 space after closing parenthesis; found 0
if($fdinfo){
//如果这个时候客户端还连接者的话说明需要返回返回的信息,
//如果客户端已经关闭了的话说明不需要server返回数据
//判断下data的类型
Expected 1 space after closing parenthesis; found 0
if (is_array($data)){
$data = json_encode($data);
}
$serv->send($fd, $data . $serv->setting['package_eof']);
}
}
 
Whitespace found at end of line
public function initConfig($config)
{
$config['onWork'] = ArrayToolkit::index($config['onWork'], 'cmd');
$config['onTask'] = ArrayToolkit::index($config['onTask'], 'cmd');
$this->config = $config;
}
 
private function createTaskTable()
{
$this->table = new swoole_table(10240);
$this->table->column("count", swoole_table::TYPE_INT);
$this->table->create();
}
 
Avoid variables with short names like $fd. Configured minimum length is 3.
Opening brace should be on a new line
private function updateTaskCount($fd, $incr = 1){
$count = $this->table->get($fd);
$count['count'] = $count['count'] + $incr;
$this->table->set($fd, $count);
}
 
Avoid variables with short names like $fd. Configured minimum length is 3.
Opening brace should be on a new line
The variable $task_count is not named in camelCase.
private function getTaskCount($fd){
$task_count = $this->table->get($fd);
return $task_count['count'];
}
}