Skip to content

Commit

Permalink
修复一些可能存在的数组键名是 int 类型时的错误 (#563)
Browse files Browse the repository at this point in the history
* 修复缓存 setMultiple() 方法传参数组键名是数字

* 修复

* 修复一些可能存在的数组键名是 int 时的错误

* 修复一些可能存在的数组键名是 int 时的错误

* 优化代码

* 优化代码

* 格式化代码
  • Loading branch information
Yurunsoft authored Jul 13, 2023
1 parent c93aa90 commit 919c540
Show file tree
Hide file tree
Showing 42 changed files with 80 additions and 61 deletions.
11 changes: 11 additions & 0 deletions dev/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,14 @@ function checkPorts(array $ports, string $host = '127.0.0.1', int $tryCount = 30
register_shutdown_function($shutdownCallback);
})();
}

function array_keys_string(array $array): array
{
$keys = [];
foreach ($array as $key => $_)
{
$keys[] = (string) $key;
}

return $keys;
}
2 changes: 1 addition & 1 deletion src/Cache/Handler/Apcu.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function setMultiple($values, $ttl = null)
$newValues = [];
foreach ($values as $k => $v)
{
$newValues[$this->parseKey($k)] = $v;
$newValues[$this->parseKey((string) $k)] = $v;
}

return [] === apcu_store($newValues, null, (int) $ttl);
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Handler/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function setMultiple($values, $ttl = null)
}
foreach ($values as $key => $value)
{
$result = $result && $this->set($key, $value, $ttl);
$result = $result && $this->set((string) $key, $value, $ttl);
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Handler/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function setMultiple($values, $ttl = null)
$object = self::$storage;
foreach ($values as $key => $value)
{
$object->set($key, $value, $ttl ?? 0);
$object->set((string) $key, $value, $ttl ?? 0);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function setMultiple($values, $ttl = null)
}
foreach ($setValues as $k => $v)
{
$setValues[$this->parseKey($k)] = $this->encode($v);
$setValues[$this->parseKey((string) $k)] = $this->encode($v);
}
// ttl 支持 \DateInterval 格式
if ($ttl instanceof \DateInterval)
Expand All @@ -138,7 +138,7 @@ public function setMultiple($values, $ttl = null)
{
foreach ($setValues as $k => $v)
{
$redis->expire($k, $ttl);
$redis->expire((string) $k, $ttl);
}
}
foreach ($redis->exec() as $result)
Expand Down
1 change: 1 addition & 0 deletions src/Cache/Handler/RedisHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public function setMultiple($values, $ttl = null)
$setValues = [];
foreach ($_setValues as $k => $v)
{
$k = (string) $k;
$this->parseKey($k, $member);
$setValues[$k]['member'][] = $member;
$setValues[$k]['value'][] = $this->encode($v);
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Handler/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function setMultiple($values, $ttl = null)
$object = $this->getObject();
foreach ($values as $key => $value)
{
$object->set($key, $value, $ttl ?? 0);
$object->set((string) $key, $value, $ttl ?? 0);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Components/fpm/src/Http/Message/FpmResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ private function sendHeaders(): void
{
foreach ($headers as $name => $_)
{
header($name . ':' . $this->getHeaderLine($name));
header($name . ':' . $this->getHeaderLine((string) $name));
}
}
// cookie
$cookieParams = $this->getCookieParams();
if ($cookieParams)
{
foreach ($cookieParams as $name => $cookie)
foreach ($cookieParams as $cookie)
{
setcookie($cookie['key'], $cookie['value'], ['expires' => $cookie['expire'] ?? 0, 'path' => $cookie['path'] ?? '/', 'domain' => $cookie['domain'] ?? '', 'secure' => $cookie['secure'] ?? false, 'httponly' => $cookie['httponly'] ?? false]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/grpc/src/Client/GrpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function send(string $package, string $service, string $name, \Google\Pro
{
foreach ($metadata as $k => $v)
{
$request = $request->withHeader($k, $v);
$request = $request->withHeader((string) $k, $v);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Components/grpc/src/Proxy/Http/GrpcHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function request(string $service, string $method, Message $message, strin
$responseMetadata = [];
foreach ($response->getHeaders() as $name => $_)
{
$name = strtolower($name);
$name = strtolower((string) $name);
if (str_starts_with($name, 'grpc-'))
{
$responseMetadata[$name] = $response->getHeaderLine($name);
Expand Down Expand Up @@ -113,7 +113,7 @@ protected function prepareHttpRequest(string $service, string $method, Message $
{
foreach ($metadata as $k => $v)
{
$httpRequest->header($k, $v);
$httpRequest->header((string) $k, $v);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Components/grpc/src/Proxy/Http/GrpcHttpProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ public function proxy(string $poolName, IHttpRequest $request, IHttpResponse $re
ProtobufUtil::setMessageData($grpcRequest, $requestData, true);
}

/** @var GrpcClient $client */
$client = RpcClientPool::getInstance($poolName);
/** @var GrpcService $service */
$service = $client->getService($serviceName, $interface);

// metadata
$metadata = [];
foreach ($request->getHeaders() as $name => $_)
{
if (str_starts_with(strtolower($name), 'grpc-'))
if (str_starts_with(strtolower((string) $name), 'grpc-'))
{
$metadata[$name] = $request->getHeaderLine($name);
}
}

/** @var GrpcClient $client */
$client = RpcClientPool::getInstance($poolName);
/** @var GrpcService $service */
$service = $client->getService($serviceName, $interface);

// send
if (false === ($streamId = $service->send($methodName, $grpcRequest, $metadata)))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Components/jwt/src/Bean/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getBuilderInstance(?string $name = null): Builder
{
foreach ($headers as $k => $v)
{
$builder->withHeader($k, $v);
$builder->withHeader((string) $k, $v);
}
}
$signer = $config->getSignerInstance();
Expand Down Expand Up @@ -156,7 +156,7 @@ public function getBuilderInstance(?string $name = null): Builder
{
foreach ($headers as $k => $v)
{
$builder->withHeader($k, $v);
$builder->withHeader((string) $k, $v);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/src/Db/Query/Builder/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function parseField(array $fields): string
$params = &$this->params;
foreach ($fields as $k => $v)
{
if (is_numeric($k))
if (\is_int($k))
{
if ($v instanceof Field)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (!is_numeric($k))
if (!\is_int($k))
{
$fields[] = $query->fieldQuote($k);
$valueParams[] = $v->toString($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (is_numeric($k))
if (\is_int($k))
{
$setStrs[] = $v->toString($query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (is_numeric($k))
if (\is_int($k))
{
$setStrs[] = $v->toString($query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function send(): self
$cookieParams = $this->getCookieParams();
if ($cookieParams)
{
foreach ($cookieParams as $name => $cookie)
foreach ($cookieParams as $cookie)
{
$this->addHeader(ResponseHeader::SET_COOKIE, $this->cookieArrayToHeader($cookie));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/shared-memory/src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(array $options = [])
$this->client = new \Yurun\Swoole\SharedMemory\Client\Client($options);
foreach ($options['storeTypes'] as $k => $v)
{
if (is_numeric($k))
if (\is_int($k))
{
$refClass = new \ReflectionClass($v);
$this->objects[$refClass->getShortName()] = new $v($this->client);
Expand Down
2 changes: 1 addition & 1 deletion src/Components/swoole/src/Cron/CronManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __init(): void
$realTasks = &$this->realTasks;
foreach ($this->tasks as $id => $task)
{
$realTasks[$id] = new CronTask($id, $task['type'], $task['task'], $task['cron'], $task['data'] ?? null, $task['lockExpire'] ?? 120, $task['unique'] ?? null, $task['redisPool'] ?? null, $task['lockWaitTimeout'] ?? 10, $task['force'] ?? false, $task['successLog'] ?? true);
$realTasks[$id] = new CronTask((string) $id, $task['type'], $task['task'], $task['cron'], $task['data'] ?? null, $task['lockExpire'] ?? 120, $task['unique'] ?? null, $task['redisPool'] ?? null, $task['lockWaitTimeout'] ?? 10, $task['force'] ?? false, $task['successLog'] ?? true);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Components/swoole/src/Http/Message/SwooleResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private function sendHeaders(): void
{
foreach ($this->headers as $name => $headers)
{
$name = (string) $name;
$swooleResponse->header($name, $this->getHeaderLine($name));
}
}
Expand All @@ -77,7 +78,7 @@ private function sendHeaders(): void
{
foreach ($this->trailers as $name => $value)
{
$swooleResponse->trailer($name, $value);
$swooleResponse->trailer((string) $name, $value);
}
}
// status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function getClientIdByFlag(string $flag): array
public function getClientIdsByFlags(array $flags): array
{
$result = $this->useRedis(fn (RedisHandler $redis) => $redis->hMget($this->key . ':binder', $flags));
foreach ($result as $k => $v)
foreach ($result as $k => $v)
{
$result[$k] = (array) $v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function defaultHandShake(IHttpRequest $request, IHttpResponse $response

foreach ($headers as $key => $val)
{
$response = $response->setHeader($key, $val);
$response = $response->setHeader((string) $key, $val);
}

return $response->setStatus(StatusCode::SWITCHING_PROTOCOLS);
Expand Down
2 changes: 1 addition & 1 deletion src/Components/swoole/src/Util/AtomicManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function setNames(array $names): void
}
foreach ($names as $key => $value)
{
if (is_numeric($key))
if (\is_int($key))
{
self::$atomics[$value] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/swoole/src/Util/MemoryTableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static function setNames(array $names): void
}
foreach ($names as $key => $value)
{
if (is_numeric($key))
if (\is_int($key))
{
self::$tables[$value] = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private function sendHeaders(): void
{
foreach ($this->headers as $name => $headers)
{
$name = (string) $name;
$response->header($name, $this->getHeaderLine($name));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/App/Contract/BaseApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function loadDotEnv(): void
// 加载 .env 配置
foreach ($_ENV as $name => $value)
{
Config::set($name, $value);
Config::set((string) $name, $value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Drivers/TPdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function execute(array $inputParameters = null): bool
{
foreach ($inputParameters as $k => $v)
{
if (is_numeric($k))
if (\is_int($k))
{
$statement->bindValue($k + 1, $v, $this->getDataTypeByValue($v));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/Builder/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function parseField(array $fields): string
$params = &$this->params;
foreach ($fields as $k => $v)
{
if (is_numeric($k))
if (\is_int($k))
{
if ($v instanceof Field)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/Builder/InsertBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (!is_numeric($k))
if (!\is_int($k))
{
$fields[] = $query->fieldQuote($k);
$valueParams[] = $v->toString($query);
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/Builder/ReplaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (is_numeric($k))
if (\is_int($k))
{
$setStrs[] = $v->toString($query);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/Builder/UpdateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function build(...$args): string
{
if ($v instanceof \Imi\Db\Query\Raw)
{
if (is_numeric($k))
if (\is_int($k))
{
$setStrs[] = $v->toString($query);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public function orderRaw($raw, array $binds = []): self
{
foreach ($raw as $k => $v)
{
if (is_numeric($k))
if (\is_int($k))
{
$fieldName = $v;
$direction = 'asc';
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Relation/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ private static function parseManyToManyQueryFields(string $middleModel, string $
$field->setTable($rightTable);
$field->setField($name);
}
foreach ($rightModelMeta->getSqlColumns() as $name => $sqlAnnotations)
foreach ($rightModelMeta->getSqlColumns() as $sqlAnnotations)
{
/** @var \Imi\Model\Annotation\Sql $sqlAnnotation */
$sqlAnnotation = $sqlAnnotations[0];
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Traits/TModelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private function hasCustomFields(): bool
$k = array_key_first($field);
$v = $field[$k] ?? null;

if (is_numeric($k))
if (\is_int($k))
{
if ('*' === $v)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Server/ConnectionContext/StoreHandler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public function getClientIdByFlag(string $flag): array
public function getClientIdsByFlags(array $flags): array
{
$result = $this->useRedis(fn (RedisHandler $redis) => $redis->hMget($this->key . ':binder', $flags));
foreach ($result as $k => $v)
foreach ($result as $k => $v)
{
$result[$k] = (array) $v;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/TcpServer/Route/TcpRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function checkCondition($data, TcpRouteAnnotation $annotation): bool
}
foreach ($annotation->condition as $name => $value)
{
if (ObjectArrayHelper::get($data, $name) !== $value)
if (ObjectArrayHelper::get($data, (string) $name) !== $value)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/UdpServer/Route/UdpRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function checkCondition($data, UdpRouteAnnotation $annotation): bool
}
foreach ($annotation->condition as $name => $value)
{
if (ObjectArrayHelper::get($data, $name) !== $value)
if (ObjectArrayHelper::get($data, (string) $name) !== $value)
{
return false;
}
Expand Down
Loading

0 comments on commit 919c540

Please sign in to comment.