Skip to content

Commit e3e9940

Browse files
yangzong18huiguangjun
authored andcommitted
compatibility with php 8.5
1 parent 406b28d commit e3e9940

File tree

13 files changed

+334
-168
lines changed

13 files changed

+334
-168
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
fail-fast: false
99
matrix:
1010
operating-system: [ubuntu-latest, windows-latest, macos-latest]
11-
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
11+
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
1212
steps:
1313
- name: Setup PHP, with composer and extensions
1414
uses: shivammathur/setup-php@v2

src/ClientImpl.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ function ($response) use ($input) {
6969
$responseRo = new \ReflectionObject($response);
7070
if ($responseRo->hasProperty('stream')) {
7171
$pp = $responseRo->getProperty('stream');
72-
$pp->setAccessible(true);
72+
if (PHP_VERSION_ID < 80100) {
73+
$pp->setAccessible(true);
74+
}
7375
$pp->setValue($response, $body);
7476
}
7577
}
@@ -629,9 +631,10 @@ private function buildRequestContext(OperationInput &$input, array &$options)
629631
}
630632
}
631633
}
632-
if ($value != null) {
633-
$request = $request->withAddedHeader('Content-Type', $value);
634+
if ($value == null) {
635+
$value = 'application/octet-stream';
634636
}
637+
$request = $request->withAddedHeader('Content-Type', $value);
635638
}
636639

637640
// signing context

src/Deserializer.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ private static function deserializeXmlModel(\SimpleXMLElement $element, $class):
9797

9898
$type = (string)$property->getType();
9999
$value = self::deserializeXmlAny($element->$name, $type, $annotation);
100-
$property->setAccessible(true);
100+
if (PHP_VERSION_ID < 80100) {
101+
$property->setAccessible(true);
102+
}
101103
$property->setValue($obj, $value);
102104
}
103105

@@ -120,12 +122,12 @@ private static function castToBool(string $value): bool
120122

121123
private static function castToInt(string $value): int
122124
{
123-
return (int) $value;
125+
return (int)$value;
124126
}
125127

126128
private static function castToFloat(string $value): float
127129
{
128-
return (float) $value;
130+
return (float)$value;
129131
}
130132

131133
private static function castToAny(string $value, string $type, ?string $format)
@@ -179,21 +181,29 @@ public static function deserializeOutput(ResultModel $result, OperationOutput $o
179181

180182
//common part
181183
$p = $ro->getProperty('status');
182-
$p->setAccessible(true);
184+
if (PHP_VERSION_ID < 80100) {
185+
$p->setAccessible(true);
186+
}
183187
$p->setValue($result, $output->getStatus());
184188

185189
$p = $ro->getProperty('statusCode');
186-
$p->setAccessible(true);
190+
if (PHP_VERSION_ID < 80100) {
191+
$p->setAccessible(true);
192+
}
187193
$p->setValue($result, $output->GetStatusCode());
188194

189195
$headers = $output->getHeaders() ?? [];
190196
$p = $ro->getProperty('headers');
191-
$p->setAccessible(true);
197+
if (PHP_VERSION_ID < 80100) {
198+
$p->setAccessible(true);
199+
}
192200
$p->setValue($result, $headers);
193201

194202
if (isset($headers['x-oss-request-id'])) {
195203
$p = $ro->getProperty('requestId');
196-
$p->setAccessible(true);
204+
if (PHP_VERSION_ID < 80100) {
205+
$p->setAccessible(true);
206+
}
197207
$p->setValue($result, $headers['x-oss-request-id']);
198208
}
199209

@@ -233,8 +243,10 @@ public static function deserializeOutputHeaders(ResultModel $result, OperationOu
233243
continue;
234244
}
235245

236-
$value = self::castToAny( $headers[$name], $annotation->type, $annotation->format);
237-
$property->setAccessible(true);
246+
$value = self::castToAny($headers[$name], $annotation->type, $annotation->format);
247+
if (PHP_VERSION_ID < 80100) {
248+
$property->setAccessible(true);
249+
}
238250
$property->setValue($result, $value);
239251
}
240252

@@ -243,15 +255,17 @@ public static function deserializeOutputHeaders(ResultModel $result, OperationOu
243255
$property = $item['property'];
244256
$prefix = strtolower($annotation->rename);
245257
$len = strlen($prefix);
246-
$meta = [];
247-
foreach ($headers as $key => $value) {
258+
$meta = [];
259+
foreach ($headers as $key => $value) {
248260
if (strncasecmp($key, $prefix, $len) == 0) {
249261
$meta[strtolower(substr($key, $len))] = $value;
250262
}
251263
}
252264

253265
if (count($meta) > 0) {
254-
$property->setAccessible(true);
266+
if (PHP_VERSION_ID < 80100) {
267+
$property->setAccessible(true);
268+
}
255269
$property->setValue($result, $meta);
256270
}
257271
}
@@ -280,7 +294,9 @@ public static function deserializeOutputBody(ResultModel $result, OperationOutpu
280294

281295
if ('xml' === $annotation->format) {
282296
$value = self::deserializeXml($content, $annotation->type, $annotation->rename);
283-
$property->setAccessible(true);
297+
if (PHP_VERSION_ID < 80100) {
298+
$property->setAccessible(true);
299+
}
284300
$property->setValue($result, $value);
285301
} else {
286302
throw new DeserializationExecption("Unsupport body format:$annotation->format");
@@ -292,7 +308,7 @@ public static function deserializeOutputBody(ResultModel $result, OperationOutpu
292308
}
293309

294310
public static function deserializeOutputInnerBody(ResultModel $result, OperationOutput $output)
295-
{
311+
{
296312
$body = $output->getBody();
297313
if ($body == null) {
298314
return;

src/Retry/EqualJitterBackoff.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public function __construct(float $baseDelay, float $maxBackOff)
3030
{
3131
$this->baseDelay = $baseDelay;
3232
$this->maxBackOff = $maxBackOff;
33-
$this->attemptCelling = intval(log(PHP_FLOAT_MAX / $baseDelay, 2));
33+
$value = log(PHP_FLOAT_MAX / $baseDelay, 2);
34+
if (is_finite($value)) {
35+
$intValue = (int)$value;
36+
} else {
37+
$intValue = 64;
38+
}
39+
$this->attemptCelling = $intValue;
3440
}
3541

3642
public function backoffDelay(int $attempt, ?\Throwable $reason): float
@@ -39,9 +45,9 @@ public function backoffDelay(int $attempt, ?\Throwable $reason): float
3945

4046
$delay = min(2 ** $attempt * $this->baseDelay, $this->maxBackOff);
4147

42-
$half = $delay/2;
48+
$half = $delay / 2;
4349

44-
$rand = mt_rand(0,1000000)/1000000;
50+
$rand = mt_rand(0, 1000000) / 1000000;
4551

4652
return $half + $rand * ($half + 1);
4753
}

src/Retry/FullJitterBackoff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public function __construct(float $baseDelay, float $maxBackOff)
2929
{
3030
$this->baseDelay = $baseDelay;
3131
$this->maxBackOff = $maxBackOff;
32-
$this->attemptCelling = intval(log(PHP_FLOAT_MAX / $baseDelay, 2));
32+
$value = log(PHP_FLOAT_MAX / $baseDelay, 2);
33+
if (is_finite($value)) {
34+
$intValue = (int)$value;
35+
} else {
36+
$intValue = 64;
37+
}
38+
$this->attemptCelling = $intValue;
3339
}
3440

3541
public function backoffDelay(int $attempt, ?\Throwable $reason): float

src/Serializer.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private static function serializeXmlModel(\XMLWriter $writer, string $name, Mode
100100

101101
// children element
102102
foreach ($ro->getProperties() as $property) {
103-
$property->setAccessible(true);
103+
if (PHP_VERSION_ID < 80100) {
104+
$property->setAccessible(true);
105+
}
104106
$val = $property->getValue($value);
105107
if (!isset($val)) {
106108
continue;
@@ -130,7 +132,9 @@ public static function serializeInput(RequestModel $request, OperationInput $inp
130132

131133
//headers
132134
$hp = $ro->getProperty('headers');
133-
$hp->setAccessible(true);
135+
if (PHP_VERSION_ID < 80100) {
136+
$hp->setAccessible(true);
137+
}
134138
$h = $hp->getValue($request);
135139
if (is_array($h)) {
136140
foreach ($h as $key => $value) {
@@ -140,7 +144,9 @@ public static function serializeInput(RequestModel $request, OperationInput $inp
140144

141145
//parameters
142146
$pp = $ro->getProperty('parameters');
143-
$pp->setAccessible(true);
147+
if (PHP_VERSION_ID < 80100) {
148+
$pp->setAccessible(true);
149+
}
144150
$p = $pp->getValue($request);
145151
if (is_array($p)) {
146152
foreach ($p as $key => $value) {
@@ -150,15 +156,19 @@ public static function serializeInput(RequestModel $request, OperationInput $inp
150156

151157
//payload
152158
$pd = $ro->getProperty('payload');
153-
$pd->setAccessible(true);
159+
if (PHP_VERSION_ID < 80100) {
160+
$pd->setAccessible(true);
161+
}
154162
$payload = $pd->getValue($request);
155163
if ($payload instanceof StreamInterface) {
156164
$input->setBody($payload);
157165
}
158166

159167
// all properties in request
160168
foreach ($ro->getProperties() as $property) {
161-
$property->setAccessible(true);
169+
if (PHP_VERSION_ID < 80100) {
170+
$property->setAccessible(true);
171+
}
162172
$val = $property->getValue($request);
163173
if (!isset($val)) {
164174
if (Functions::isRequiredProperty($property)) {

src/Signer/SignerV1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function authHeader(SigningContext $signingCtx): void
147147
$signingCtx->time = (new DateTime('now', new DateTimeZone('UTC')))->modify('+' . $signingCtx->clockOffset . 'seconds');
148148
}
149149

150-
$datetime = $signingCtx->time->format(DATE_RFC7231);
150+
$datetime = $signingCtx->time->format("D, d M Y H:i:s \G\M\T");
151151
$request = $request->withHeader(self::DATE_HEADER, $datetime);
152152

153153
// Credentials information

src/Signer/SignerV4.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function authHeader(SigningContext $signingCtx): void
112112
$signingCtx->time = (new DateTime('now', new DateTimeZone('UTC')))->modify('+' . $signingCtx->clockOffset . 'seconds');
113113
}
114114
$datetime = $signingCtx->time->format("Ymd\THis\Z");
115-
$dateGmt = $signingCtx->time->format(DATE_RFC7231);
115+
$dateGmt = $signingCtx->time->format("D, d M Y H:i:s \G\M\T");
116116
$date = $signingCtx->time->format("Ymd");
117117
$request = $request->withHeader(self::OSS_DATE_HEADER, $datetime)
118118
->withHeader(self::DATE_HEADER, $dateGmt);
@@ -134,14 +134,14 @@ public function authHeader(SigningContext $signingCtx): void
134134
// CanonicalRequest
135135
$signingCtx->request = $request;
136136
$canonicalRequest = $this->calcCanonicalRequest($signingCtx, $additionalHeaders);
137-
//printf("canonicalRequest:%s" . PHP_EOL, $canonicalRequest);
137+
// printf("canonicalRequest:%s" . PHP_EOL, $canonicalRequest);
138138
// StringToSign
139139
$stringToSign = $this->calcStringToSign($datetime, $scope, $canonicalRequest);
140140
$signingCtx->stringToSign = $stringToSign;
141-
//printf("stringToSign:%s" . PHP_EOL, $stringToSign);
141+
// printf("stringToSign:%s" . PHP_EOL, $stringToSign);
142142
// Signature
143143
$signature = $this->calcSignature($cred->getAccessKeySecret(), $date, $region, $product, $stringToSign);
144-
//printf("signature:%s" . PHP_EOL, $signature);
144+
// printf("signature:%s" . PHP_EOL, $signature);
145145

146146
// credential
147147
$buf = 'OSS4-HMAC-SHA256 Credential=';

src/Transform/Functions.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ public static function serializeInputLite(RequestModel $request, OperationInput
200200

201201
//headers
202202
$hp = $ro->getProperty('headers');
203-
$hp->setAccessible(true);
203+
if (PHP_VERSION_ID < 80100){
204+
$hp->setAccessible(true);
205+
}
204206
$h = $hp->getValue($request);
205207
if (is_array($h)) {
206208
foreach ($h as $key => $value) {
@@ -210,7 +212,9 @@ public static function serializeInputLite(RequestModel $request, OperationInput
210212

211213
//parameters
212214
$pp = $ro->getProperty('parameters');
213-
$pp->setAccessible(true);
215+
if (PHP_VERSION_ID < 80100){
216+
$pp->setAccessible(true);
217+
}
214218
$p = $pp->getValue($request);
215219
if (is_array($p)) {
216220
foreach ($p as $key => $value) {
@@ -220,7 +224,9 @@ public static function serializeInputLite(RequestModel $request, OperationInput
220224

221225
//payload
222226
$pd = $ro->getProperty('payload');
223-
$pd->setAccessible(true);
227+
if (PHP_VERSION_ID < 80100){
228+
$pd->setAccessible(true);
229+
}
224230
$payload = $pd->getValue($request);
225231
if ($payload instanceof \Psr\Http\Message\StreamInterface) {
226232
$input->setBody($payload);

0 commit comments

Comments
 (0)