Skip to content

Commit 6b805c9

Browse files
author
fdream
committed
format code
1 parent af90c3a commit 6b805c9

File tree

5 files changed

+60
-63
lines changed

5 files changed

+60
-63
lines changed

src/Handler.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,16 @@ public static function start(\Swoole\Http\Request $request, \Swoole\Http\Respons
2525
null
2626
];
2727
}
28-
/**
29-
*
30-
* @var \Controller $controller
31-
*/
3228
$controller = $router->getController($response);
3329
if ($controller instanceof Controller\StaticResourceController) {
3430
$controller->begin()->endRequest();
3531
} else {
3632
if ($router->isWebhook()) {
3733
\session::startForWebhook();
3834
} else {
39-
if ($router->getMethod() == 'POST') {
35+
if ('POST' === $router->getMethod()) {
4036
$session_started = self::parseRequest($request, $response, $controller);
41-
if ($session_started === null) {
37+
if (null === $session_started) {
4238
return [
4339
200,
4440
'replay request',
@@ -54,7 +50,7 @@ public static function start(\Swoole\Http\Request $request, \Swoole\Http\Respons
5450
}
5551
\cache::select(1);
5652
$controller->checkAuthority()->validate()->begin();
57-
if ($router->getMethod() != 'GET') {
53+
if ('GET' !== $router->getMethod()) {
5854
$controller->jsonResponse();
5955
} else {
6056
$controller->endRequest();
@@ -99,7 +95,7 @@ public static function start(\Swoole\Http\Request $request, \Swoole\Http\Respons
9995
$cnmsg = '服务器出现内部错误,请稍后重试';
10096
$data = null;
10197
}
102-
if (isset($router) && $router->getMethod() == 'GET' &&
98+
if (isset($router) && 'GET' === $router->getMethod() &&
10399
\session::getAgentMap()->echoErrorMsgWhenMethodGet(\session::getAgent())) {
104100
$response->header('Content-Type', 'text/html; charset=UTF-8');
105101
$response->end($cnmsg);
@@ -221,6 +217,10 @@ private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http
221217
} else {
222218
$sid = null;
223219
}
220+
if (! isset($data->ua)) {
221+
throw new \ExceptionToResponse('Unrecognizable data', '报文无法识别');
222+
}
223+
\session::getAgentMap()->getAgentId($data->ua);
224224
if ($flag) {
225225
if (! property_exists($data, 'timestamp') || abs($data->timestamp / 1000 - \Time\now()) > 1200 * 3600) {
226226
throw new \ExceptionToResponse\TimeTooDifferentException();
@@ -246,11 +246,11 @@ private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http
246246
} else {
247247
$response_string = null;
248248
}
249+
\cache::select(1);
249250
} catch (\Swango\Cache\RedisErrorException $e) {
250251
\cache::select(1);
251252
throw $e;
252253
}
253-
\cache::select(1);
254254
if (isset($response_string) && is_string($response_string)) {
255255
$response->header('Access-Control-Allow-Headers',
256256
'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type');
@@ -273,7 +273,7 @@ private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http
273273
}
274274
\SysContext::set('request_post', $data->data);
275275
if ($controller::USE_SESSION && ! $controller::START_SESSION_LATER && property_exists($data, 'sid')) {
276-
\session::start($request, $response, $sid, $data->ua ?? 'wmp');
276+
\session::start($request, $response, $sid, $data->ua);
277277
return true;
278278
} else {
279279
return false;

src/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function exists(): bool {
3939
protected function __construct(string $uri, string $method, ?int $version) {
4040
$action = explode('/', strtolower($uri));
4141
array_shift($action);
42-
if (end($action) == '') {
42+
if ('' === end($action)) {
4343
array_pop($action);
4444
}
4545
$this->action = $action;
@@ -65,7 +65,7 @@ public function getControllerName(): string {
6565
}
6666
public function isWebhook(): bool {
6767
$action1 = reset($this->action);
68-
return $action1 == 'webhook' || $action1 == 'server' || $action1 == 'service';
68+
return 'webhook' === $action1 || 'server' === $action1 || 'service' === $action1;
6969
}
7070
public function getHost(): string {
7171
return $this->host ?? '';

src/Validator/COI.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ public function __construct() {
55
parent::__construct('身份证号', 18, 18);
66
}
77
public function getCnMsg(): string {
8-
if (isset($this->cnmsg))
8+
if (isset($this->cnmsg)) {
99
return $this->cnmsg;
10+
}
1011
return '请填写正确的身份证号';
1112
}
1213
protected function check(?string $key, &$value): void {
1314
parent::check($key, $value);
1415
$idcard_base = substr($value, 0, 17);
1516
$verify_code = substr($value, 17, 1);
16-
if ($verify_code == 'x')
17+
if ('x' === $verify_code) {
1718
$verify_code = 'X';
19+
}
1820
if (bccomp($idcard_base, '11000000000000000') < 0 || bccomp($idcard_base, '66000000000000000') > 0 ||
19-
(! is_numeric($verify_code) && $verify_code != 'X'))
21+
(! is_numeric($verify_code) && $verify_code != 'X')) {
2022
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
21-
23+
}
2224
// 校验码对应值
2325
$verify_code_list = [
2426
'1',
@@ -33,32 +35,31 @@ protected function check(?string $key, &$value): void {
3335
'3',
3436
'2'
3537
];
36-
3738
// 根据前17位计算校验码
3839
$total = 0;
3940
foreach ([
40-
7,
41-
9,
42-
10,
43-
5,
44-
8,
45-
4,
46-
2,
47-
1,
48-
6,
49-
3,
50-
7,
51-
9,
52-
10,
53-
5,
54-
8,
55-
4,
56-
2
57-
] as $i=>$v)
41+
7,
42+
9,
43+
10,
44+
5,
45+
8,
46+
4,
47+
2,
48+
1,
49+
6,
50+
3,
51+
7,
52+
9,
53+
10,
54+
5,
55+
8,
56+
4,
57+
2
58+
] as $i => $v)
5859
$total += $idcard_base{$i} * $v;
59-
60-
if ($verify_code != $verify_code_list[$total % 11])
60+
if ($verify_code != $verify_code_list[$total % 11]) {
6161
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
62+
}
6263
$value = $idcard_base . $verify_code;
6364
}
6465
}

src/Validator/Ob.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
* 对象 或 每项含义不同的数组
66
*/
77
class Ob extends \Swango\HttpServer\Validator {
8-
private $set_null_when_empty = false;
9-
protected $do_not_validate_deeper = false;
8+
private bool $set_null_when_empty = false;
9+
protected bool $do_not_validate_deeper = false;
10+
/**
11+
* @var \Swango\HttpServer\Validator[]
12+
*/
13+
protected array $map = [];
1014
public function getCnMsg(): string {
1115
if (isset($this->cnmsg)) {
1216
return $this->cnmsg;
@@ -29,7 +33,7 @@ public function __set(string $key, \Swango\HttpServer\Validator $value) {
2933
/**
3034
* 表示可能为null,默认为不能为null
3135
*/
32-
public function setNullWhenEmpty() {
36+
public function setNullWhenEmpty(): self {
3337
$this->set_null_when_empty = true;
3438
return $this;
3539
}
@@ -44,15 +48,11 @@ protected function check(?string $key, &$value): void {
4448
return;
4549
}
4650
if (is_object($value)) {
47-
if ($this->couldBeEmpty() && $value == new \stdClass()) {
51+
if ($this->couldBeEmpty() && false === current($value)) {
4852
$value = null;
4953
return;
5054
}
5155
foreach ($this->map as $k => $validator) {
52-
/**
53-
*
54-
* @var $validator \Swango\HttpServer\Validator
55-
*/
5656
if (! property_exists($value, $k)) {
5757
if ($validator->isOptional()) {
5858
continue;
@@ -73,10 +73,6 @@ protected function check(?string $key, &$value): void {
7373
return;
7474
}
7575
foreach ($this->map as $k => $validator) {
76-
/**
77-
*
78-
* @var $validator \Swango\HttpServer\Validator
79-
*/
8076
if (! array_key_exists($k, $value)) {
8177
if ($validator->isOptional()) {
8278
continue;
@@ -93,7 +89,6 @@ protected function check(?string $key, &$value): void {
9389
unset($v);
9490
}
9591
}
96-
protected $map = [];
9792
public function setMap(array $map): self {
9893
$this->map = $map;
9994
return $this;

src/Validator/Url.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
namespace Swango\HttpServer\Validator;
33
class Url extends Anything {
44
public function __construct(?string $key = null) {
5-
parent::__construct($key ?? '链接', 1, 1024);
5+
parent::__construct($key ?? '链接');
66
$this->cnmsg = '填写正确URL地址';
77
}
88
protected function check(?string $key, &$value): void {
99
parent::check($key, $value);
10-
if ($value{0} == '/')
10+
if ('/' === $value[0]) {
1111
return;
12-
if (! preg_match(
13-
'/^http[s]?:\/\/' . '(([0-9]{1,3}\.){3}[0-9]{1,3}' . // IP形式的URL- 199.194.52.184
14-
'|' . // 允许IP和DOMAIN(域名)
15-
'([0-9a-z_!~*\'()-]+\.)*' . // 三级域验证- www.
16-
'([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' . // 二级域验证
17-
'[a-z]{2,6})' . // 顶级域验证.com or .museum
18-
'(:[0-9]{1,4})?' . // 端口- :80
19-
'((\/\?)|' . // 如果含有文件对文件部分进行校验
20-
'(\/[0-9a-zA-Z_!~\*\'\(\)\.;\?:@&=\+\$,%#-\/]*)?)$/', $value))
12+
}
13+
if (! preg_match('/^http[s]?:\/\/' . '(([0-9]{1,3}\.){3}[0-9]{1,3}' . // IP形式的URL- 199.194.52.184
14+
'|' . // 允许IP和DOMAIN(域名)
15+
'([0-9a-z_!~*\'()-]+\.)*' . // 三级域验证- www.
16+
'([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' . // 二级域验证
17+
'[a-z]{2,6})' . // 顶级域验证.com or .museum
18+
'(:[0-9]{1,4})?' . // 端口- :80
19+
'((\/\?)|' . // 如果含有文件对文件部分进行校验
20+
'(\/[0-9a-zA-Z_!~\*\'\(\)\.;\?:@&=\+\$,%#-\/]*)?)$/', $value)) {
2121
throw new \ExceptionToResponse('Invalid URL', '请填写正确的链接');
22+
}
2223
}
23-
}
24-
24+
}
25+

0 commit comments

Comments
 (0)