fix: resolve PHPStan static analysis warnings

This commit is contained in:
xboard
2025-05-07 19:48:19 +08:00
parent db235c10e8
commit 97e7ffccae
86 changed files with 2335 additions and 1206 deletions

View File

@@ -13,7 +13,11 @@ class ClashMeta implements ProtocolInterface
private $servers;
private $user;
public function __construct($user, $servers, array $options = null)
/**
* @param mixed $user 用户实例
* @param array $servers 服务器列表
*/
public function __construct($user, $servers)
{
$this->user = $user;
$this->servers = $servers;
@@ -78,6 +82,18 @@ class ClashMeta implements ProtocolInterface
array_push($proxy, self::buildTuic($user['uuid'], $item));
array_push($proxies, $item['name']);
}
if ($item['type'] === 'socks') {
array_push($proxy, self::buildSocks5($user['uuid'], $item));
array_push($proxies, $item['name']);
}
if ($item['type'] === 'http') {
array_push($proxy, self::buildHttp($user['uuid'], $item));
array_push($proxies, $item['name']);
}
if ($item['type'] === 'mieru') {
array_push($proxy, self::buildMieru($user['uuid'], $item));
array_push($proxies, $item['name']);
}
}
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
@@ -176,7 +192,7 @@ class ClashMeta implements ProtocolInterface
if (data_get($protocol_settings, 'network_settings.header.type', 'none') !== 'none') {
$array['http-opts'] = [
'headers' => data_get($protocol_settings, 'network_settings.header.request.headers'),
'path' => \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
'path' => \Illuminate\Support\Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
];
}
break;
@@ -369,9 +385,78 @@ class ClashMeta implements ProtocolInterface
return $array;
}
public static function buildMieru($password, $server)
{
$protocol_settings = data_get($server, 'protocol_settings', []);
$array = [
'name' => $server['name'],
'type' => 'mieru',
'server' => $server['host'],
'port' => $server['port'],
'username' => $password,
'password' => $password,
'transport' => strtoupper(data_get($protocol_settings, 'transport', 'TCP')),
'multiplexing' => data_get($protocol_settings, 'multiplexing', 'MULTIPLEXING_LOW')
];
// 如果配置了端口范围
if (isset($server['ports'])) {
$array['port-range'] = $server['ports'];
}
return $array;
}
public static function buildSocks5($password, $server)
{
$protocol_settings = $server['protocol_settings'];
$array = [];
$array['name'] = $server['name'];
$array['type'] = 'socks5';
$array['server'] = $server['host'];
$array['port'] = $server['port'];
$array['udp'] = true;
$array['username'] = $password;
$array['password'] = $password;
// TLS 配置
if (data_get($protocol_settings, 'tls')) {
$array['tls'] = true;
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
}
return $array;
}
public static function buildHttp($password, $server)
{
$protocol_settings = $server['protocol_settings'];
$array = [];
$array['name'] = $server['name'];
$array['type'] = 'http';
$array['server'] = $server['host'];
$array['port'] = $server['port'];
$array['username'] = $password;
$array['password'] = $password;
// TLS 配置
if (data_get($protocol_settings, 'tls')) {
$array['tls'] = true;
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
}
return $array;
}
private function isMatch($exp, $str)
{
return @preg_match($exp, $str);
try {
return preg_match($exp, $str) === 1;
} catch (\Exception $e) {
return false;
}
}
private function isRegex($exp)
@@ -379,6 +464,10 @@ class ClashMeta implements ProtocolInterface
if (empty($exp)) {
return false;
}
return @preg_match($exp, '') !== false;
try {
return preg_match($exp, '') !== false;
} catch (\Exception $e) {
return false;
}
}
}