mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
PHP80 小修 (#187)
* migrate-php80 fix styles fix static analyse * fix some bugs
This commit is contained in:
@@ -12,7 +12,7 @@ use Doctrine\DBAL\ParameterType;
|
||||
class DBConnection implements Connection
|
||||
{
|
||||
/** @var \PDO */
|
||||
private $conn;
|
||||
private object $conn;
|
||||
|
||||
private $pool_name;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class DBPool
|
||||
/**
|
||||
* @var array<string, SwooleObjectPool|WorkermanObjectPool> 连接池列表
|
||||
*/
|
||||
private static $pools = [];
|
||||
private static array $pools = [];
|
||||
|
||||
/**
|
||||
* 通过配置文件创建一个 MySQL 连接池
|
||||
@@ -68,10 +68,9 @@ class DBPool
|
||||
/**
|
||||
* 获取一个数据库连接池
|
||||
*
|
||||
* @param string $name 连接池名称
|
||||
* @return SwooleObjectPool|WorkermanObjectPool
|
||||
* @param string $name 连接池名称
|
||||
*/
|
||||
public static function pool(string $name)
|
||||
public static function pool(string $name): PoolInterface
|
||||
{
|
||||
if (!isset(self::$pools[$name]) && count(self::$pools) !== 1) {
|
||||
throw new \RuntimeException("Pool {$name} not found");
|
||||
|
||||
@@ -9,7 +9,7 @@ use ZM\Store\Database\DBException as DbException;
|
||||
|
||||
class DBQueryBuilder extends QueryBuilder
|
||||
{
|
||||
private $wrapper;
|
||||
private DBWrapper $wrapper;
|
||||
|
||||
public function __construct(DBWrapper $wrapper)
|
||||
{
|
||||
@@ -18,10 +18,9 @@ class DBQueryBuilder extends QueryBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DBStatementWrapper|int
|
||||
* @throws DbException
|
||||
*/
|
||||
public function execute()
|
||||
public function execute(): DBStatementWrapper|int
|
||||
{
|
||||
if ($this->getType() === self::SELECT) {
|
||||
return $this->wrapper->executeQuery($this->getSQL(), $this->getParameters(), $this->getParameterTypes());
|
||||
|
||||
@@ -13,12 +13,8 @@ use Doctrine\DBAL\ParameterType;
|
||||
|
||||
class DBStatement implements \IteratorAggregate, Statement
|
||||
{
|
||||
/** @var \PDOStatement */
|
||||
private $statement;
|
||||
|
||||
public function __construct($obj)
|
||||
public function __construct(private \PDOStatement $statement)
|
||||
{
|
||||
$this->statement = $obj;
|
||||
}
|
||||
|
||||
public function closeCursor()
|
||||
|
||||
@@ -14,11 +14,8 @@ use Doctrine\DBAL\ForwardCompatibility\Result;
|
||||
|
||||
class DBStatementWrapper
|
||||
{
|
||||
public ?Result $stmt;
|
||||
|
||||
public function __construct(?Result $stmt)
|
||||
public function __construct(public ?Result $stmt)
|
||||
{
|
||||
$this->stmt = $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ class DBWrapper
|
||||
{
|
||||
try {
|
||||
$db_list = config()->get('global.database');
|
||||
if (isset($db_list[$name]) || count($db_list) === 1) {
|
||||
if (isset($db_list[$name]) || (is_countable($db_list) ? count($db_list) : 0) === 1) {
|
||||
if ($name === '') {
|
||||
$name = array_key_first($db_list);
|
||||
}
|
||||
@@ -66,10 +66,9 @@ class DBWrapper
|
||||
|
||||
/**
|
||||
* wrapper method
|
||||
* @return array|false
|
||||
* @throws DBException
|
||||
*/
|
||||
public function fetchAssociative(string $query, array $params = [], array $types = [])
|
||||
public function fetchAssociative(string $query, array $params = [], array $types = []): array|false
|
||||
{
|
||||
try {
|
||||
return $this->connection->fetchAssociative($query, $params, $types);
|
||||
@@ -80,10 +79,9 @@ class DBWrapper
|
||||
|
||||
/**
|
||||
* wrapper method
|
||||
* @return array|false
|
||||
* @throws DBException
|
||||
*/
|
||||
public function fetchNumeric(string $query, array $params = [], array $types = [])
|
||||
public function fetchNumeric(string $query, array $params = [], array $types = []): array|false
|
||||
{
|
||||
try {
|
||||
return $this->connection->fetchNumeric($query, $params, $types);
|
||||
@@ -182,10 +180,9 @@ class DBWrapper
|
||||
|
||||
/**
|
||||
* wrapper method
|
||||
* @param mixed $value
|
||||
* @param null|int|string|Type $type
|
||||
*/
|
||||
public function quote($value, $type = ParameterType::STRING)
|
||||
public function quote(mixed $value, $type = ParameterType::STRING)
|
||||
{
|
||||
return $this->connection->quote($value, $type);
|
||||
}
|
||||
@@ -414,7 +411,7 @@ class DBWrapper
|
||||
* @return int|string the number of affected rows
|
||||
* @throws DBException
|
||||
*/
|
||||
public function executeStatement(string $sql, array $params = [], array $types = [])
|
||||
public function executeStatement(string $sql, array $params = [], array $types = []): int|string
|
||||
{
|
||||
try {
|
||||
return $this->connection->executeStatement($sql, $params, $types);
|
||||
@@ -436,7 +433,7 @@ class DBWrapper
|
||||
* @param null|string $name name of the sequence object from which the ID should be returned
|
||||
* @return false|int|string a string representation of the last inserted ID
|
||||
*/
|
||||
public function lastInsertId(?string $name = null)
|
||||
public function lastInsertId(?string $name = null): false|int|string
|
||||
{
|
||||
return $this->connection->lastInsertId($name);
|
||||
}
|
||||
@@ -600,13 +597,10 @@ class DBWrapper
|
||||
*/
|
||||
private function getConnectionClass(string $type): string
|
||||
{
|
||||
switch ($type) {
|
||||
case 'mysql':
|
||||
return MySQLDriver::class;
|
||||
case 'sqlite':
|
||||
return SQLiteDriver::class;
|
||||
default:
|
||||
throw new DBException('Unknown database type: ' . $type);
|
||||
}
|
||||
return match ($type) {
|
||||
'mysql' => MySQLDriver::class,
|
||||
'sqlite' => SQLiteDriver::class,
|
||||
default => throw new DBException('Unknown database type: ' . $type),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,13 @@ class FileSystem
|
||||
/**
|
||||
* 递归或非递归扫描目录,可返回相对目录的文件列表或绝对目录的文件列表
|
||||
*
|
||||
* @param string $dir 目录
|
||||
* @param bool $recursive 是否递归扫描子目录
|
||||
* @param bool|string $relative 是否返回相对目录,如果为true则返回相对目录,如果为false则返回绝对目录
|
||||
* @param bool $include_dir 非递归模式下,是否包含目录
|
||||
* @return array|false
|
||||
* @param string $dir 目录
|
||||
* @param bool $recursive 是否递归扫描子目录
|
||||
* @param bool|string $relative 是否返回相对目录,如果为true则返回相对目录,如果为false则返回绝对目录
|
||||
* @param bool $include_dir 非递归模式下,是否包含目录
|
||||
* @since 2.5
|
||||
*/
|
||||
public static function scanDirFiles(string $dir, bool $recursive = true, $relative = false, bool $include_dir = false)
|
||||
public static function scanDirFiles(string $dir, bool $recursive = true, bool|string $relative = false, bool $include_dir = false): array|false
|
||||
{
|
||||
$dir = zm_dir($dir);
|
||||
// 不是目录不扫,直接 false 处理
|
||||
@@ -114,26 +113,26 @@ class FileSystem
|
||||
* @param bool|string $return_path_value 是否返回文件路径,返回文件路径的话传入字符串
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getClassesPsr4(string $dir, string $base_namespace, $rule = null, $return_path_value = false): array
|
||||
public static function getClassesPsr4(string $dir, string $base_namespace, mixed $rule = null, bool|string $return_path_value = false): array
|
||||
{
|
||||
// 预先读取下composer的file列表
|
||||
$composer = ZMUtil::getComposerMetadata();
|
||||
$classes = [];
|
||||
// 扫描目录,使用递归模式,相对路径模式,因为下面此路径要用作转换成namespace
|
||||
$files = FileSystem::scanDirFiles($dir, true, true);
|
||||
$files = self::scanDirFiles($dir, true, true);
|
||||
foreach ($files as $v) {
|
||||
$pathinfo = pathinfo($v);
|
||||
if (($pathinfo['extension'] ?? '') == 'php') {
|
||||
$path = rtrim($dir, '/') . '/' . rtrim($pathinfo['dirname'], './') . '/' . $pathinfo['basename'];
|
||||
|
||||
// 过滤不包含类的文件
|
||||
$tokens = token_get_all(file_get_contents($path));
|
||||
$tokens = \PhpToken::tokenize(file_get_contents($path));
|
||||
$found = false;
|
||||
foreach ($tokens as $token) {
|
||||
if (!is_array($token)) {
|
||||
if (!$token instanceof \PhpToken) {
|
||||
continue;
|
||||
}
|
||||
if ($token[0] === T_CLASS) {
|
||||
if ($token->is(T_CLASS)) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ use ZM\Exception\ZMKnownException;
|
||||
|
||||
class FileLock
|
||||
{
|
||||
private static $lock_file_handle = [];
|
||||
private static array $lock_file_handle = [];
|
||||
|
||||
private static $name_hash = [];
|
||||
private static array $name_hash = [];
|
||||
|
||||
/**
|
||||
* 基于文件的锁,适用于跨进程操作资源用的
|
||||
@@ -19,7 +19,7 @@ class FileLock
|
||||
*/
|
||||
public static function lock(string $name)
|
||||
{
|
||||
self::$name_hash[$name] = self::$name_hash[$name] ?? md5($name);
|
||||
self::$name_hash[$name] ??= md5($name);
|
||||
$lock_file = zm_dir(TMP_DIR . '/.zm_' . zm_instance_id() . self::$name_hash[$name] . '.lock');
|
||||
self::$lock_file_handle[$name] = fopen($lock_file, 'w');
|
||||
if (self::$lock_file_handle[$name] === false) {
|
||||
|
||||
Reference in New Issue
Block a user