Skip to content

Commit 584d1f8

Browse files
committed
breaking: migrate to php 80, for the src/Util src/Helper
1 parent 460c1c5 commit 584d1f8

13 files changed

+90
-122
lines changed

Diff for: src/Helper/DataHelper.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DataHelper
3838
*
3939
* @return bool
4040
*/
41-
public static function boolean($val, bool $nullAsFalse = false): bool
41+
public static function boolean(int|string $val, bool $nullAsFalse = false): bool
4242
{
4343
if ($val !== null && !is_scalar($val)) {
4444
return (bool)$val;
@@ -55,7 +55,7 @@ public static function boolean($val, bool $nullAsFalse = false): bool
5555
*
5656
* @return bool
5757
*/
58-
public static function toBool($val, bool $nullAsFalse = false): bool
58+
public static function toBool(mixed $val, bool $nullAsFalse = false): bool
5959
{
6060
return self::boolean($val, $nullAsFalse);
6161
}
@@ -65,7 +65,7 @@ public static function toBool($val, bool $nullAsFalse = false): bool
6565
*
6666
* @return string
6767
*/
68-
public static function toString($val): string
68+
public static function toString(mixed $val): string
6969
{
7070
if (is_scalar($val)) {
7171
if (is_bool($val)) {

Diff for: src/Helper/DateHelper.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DateHelper
2727
*
2828
* @return bool
2929
*/
30-
public static function isTimestamp($timestamp): bool
30+
public static function isTimestamp(int|string $timestamp): bool
3131
{
3232
if (!is_numeric($timestamp) || 10 !== strlen($timestamp)) {
3333
return false;
@@ -89,7 +89,7 @@ public static function todayEnd(): int
8989
/**
9090
* @return false|int
9191
*/
92-
public static function tomorrowBegin()
92+
public static function tomorrowBegin(): bool|int
9393
{
9494
return mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));
9595
}
@@ -194,21 +194,14 @@ public static function before(int $time, array $unit = []): ?string
194194
$nowTime = time();
195195
$diffTime = $nowTime - $time;
196196

197-
switch (true) {
198-
case $time < ($nowTime - 31536000):
199-
return floor($diffTime / 31536000) . $unit[0];
200-
case $time < ($nowTime - 2592000):
201-
return floor($diffTime / 2592000) . $unit[1];
202-
case $time < ($nowTime - 604800):
203-
return floor($diffTime / 604800) . $unit[2];
204-
case $time < ($nowTime - 86400):
205-
return floor($diffTime / 86400) . $unit[3];
206-
case $time < ($nowTime - 3600):
207-
return floor($diffTime / 3600) . $unit[4];
208-
case $time < ($nowTime - 60):
209-
return floor($diffTime / 60) . $unit[5];
210-
default:
211-
return floor($diffTime) . $unit[6];
212-
}
197+
return match (true) {
198+
$time < ($nowTime - 31536000) => floor($diffTime / 31536000) . $unit[0],
199+
$time < ($nowTime - 2592000) => floor($diffTime / 2592000) . $unit[1],
200+
$time < ($nowTime - 604800) => floor($diffTime / 604800) . $unit[2],
201+
$time < ($nowTime - 86400) => floor($diffTime / 86400) . $unit[3],
202+
$time < ($nowTime - 3600) => floor($diffTime / 3600) . $unit[4],
203+
$time < ($nowTime - 60) => floor($diffTime / 60) . $unit[5],
204+
default => floor($diffTime) . $unit[6],
205+
};
213206
}
214207
}

Diff for: src/Helper/Format.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function beforeTime(int $secs): string
8080
}
8181

8282
/**
83-
* @param string|float $mTime value is microtime(1)
83+
* @param string|float|null $mTime value is microtime(1)
8484
*
8585
* @return string
8686
*/
@@ -189,17 +189,11 @@ public static function convertBytes(int|float|string $value): int
189189
$len = strlen($value);
190190
$qty = (int)substr($value, 0, $len - 1);
191191
$unit = strtolower(substr($value, $len - 1));
192-
switch ($unit) {
193-
case 'k':
194-
$qty *= 1024;
195-
break;
196-
case 'm':
197-
$qty *= 1048576;
198-
break;
199-
case 'g':
200-
$qty *= 1073741824;
201-
break;
202-
}
192+
$qty *= match ($unit) {
193+
'k' => 1024,
194+
'm' => 1048576,
195+
'g' => 1073741824,
196+
};
203197

204198
return $qty;
205199
}

Diff for: src/Helper/IntHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function getMin(int $val1, int $val2): int
4848
*
4949
* @return false|mixed|string
5050
*/
51-
public static function int8($i)
51+
public static function int8($i): mixed
5252
{
5353
return is_int($i) ? pack('c', $i) : unpack('c', $i)[1];
5454
}

Diff for: src/Helper/JsonHelper.php

+13-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use stdClass;
1515
use function array_merge;
1616
use function basename;
17-
use function defined;
1817
use function dirname;
1918
use function file_exists;
2019
use function file_get_contents;
@@ -27,15 +26,10 @@
2726
use function preg_replace;
2827
use function trim;
2928
use const JSON_PRETTY_PRINT;
29+
use const JSON_THROW_ON_ERROR;
3030
use const JSON_UNESCAPED_SLASHES;
3131
use const JSON_UNESCAPED_UNICODE;
3232

33-
// Compatible with lower versions
34-
if (!defined('JSON_THROW_ON_ERROR')) {
35-
define('JSON_THROW_ON_ERROR', 4194304); // since php 7.3
36-
// class JsonException extends RuntimeException {}
37-
}
38-
3933
/**
4034
* Class JsonHelper
4135
*
@@ -53,7 +47,7 @@ class JsonHelper
5347
* @return string
5448
* @noinspection PhpDocMissingThrowsInspection
5549
*/
56-
public static function enc($data, int $flags = 0, int $depth = 512): string
50+
public static function enc(mixed $data, int $flags = 0, int $depth = 512): string
5751
{
5852
/** @noinspection PhpUnhandledExceptionInspection */
5953
return self::encode($data, $flags, $depth);
@@ -69,10 +63,10 @@ public static function enc($data, int $flags = 0, int $depth = 512): string
6963
* @return string
7064
* @noinspection PhpDocMissingThrowsInspection
7165
*/
72-
public static function encode($data, int $options = 0, int $depth = 512): string
66+
public static function encode(mixed $data, int $options = 0, int $depth = 512): string
7367
{
7468
/** @noinspection PhpUnhandledExceptionInspection */
75-
return (string)json_encode($data, \JSON_THROW_ON_ERROR | $options, $depth);
69+
return (string)json_encode($data, JSON_THROW_ON_ERROR | $options, $depth);
7670
}
7771

7872
/**
@@ -86,7 +80,7 @@ public static function encode($data, int $options = 0, int $depth = 512): string
8680
* @noinspection PhpDocMissingThrowsInspection
8781
*/
8882
public static function encodeCN(
89-
$data,
83+
mixed $data,
9084
int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
9185
int $depth = 512
9286
): string {
@@ -114,7 +108,7 @@ public static function pretty($data): string
114108
* @noinspection PhpDocMissingThrowsInspection
115109
*/
116110
public static function prettyJSON(
117-
$data,
111+
mixed $data,
118112
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
119113
): string {
120114
/** @noinspection PhpUnhandledExceptionInspection */
@@ -161,13 +155,12 @@ public static function unescapedUnicode($data): string
161155

162156
/**
163157
* @param string $json
164-
*
165158
* @param bool $assoc
166159
*
167-
* @return array|mixed
160+
* @return array|stdClass
168161
* @noinspection PhpDocMissingThrowsInspection
169162
*/
170-
public static function dec(string $json, bool $assoc = true)
163+
public static function dec(string $json, bool $assoc = true): array|stdClass
171164
{
172165
/** @noinspection PhpUnhandledExceptionInspection */
173166
$data = json_decode($json, $assoc, 512, JSON_THROW_ON_ERROR);
@@ -190,7 +183,7 @@ public static function dec(string $json, bool $assoc = true)
190183
* @return array|object
191184
* @noinspection PhpDocMissingThrowsInspection
192185
*/
193-
public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
186+
public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): object|array
194187
{
195188
/** @noinspection PhpUnhandledExceptionInspection */
196189
$data = json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR | $options);
@@ -214,7 +207,7 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
214207
* @return array|object
215208
* @noinspection PhpDocMissingThrowsInspection
216209
*/
217-
public static function decodeFile(string $jsonFile, bool $assoc = false, int $depth = 512, int $options = 0)
210+
public static function decodeFile(string $jsonFile, bool $assoc = false, int $depth = 512, int $options = 0): object|array
218211
{
219212
if (!is_file($jsonFile)) {
220213
throw new InvalidArgumentException("json file not found: $jsonFile");
@@ -232,7 +225,7 @@ public static function decodeFile(string $jsonFile, bool $assoc = false, int $de
232225
*
233226
* @return array|stdClass
234227
*/
235-
public static function parse(string $data, bool $toArray = true)
228+
public static function parse(string $data, bool $toArray = true): array|stdClass
236229
{
237230
if (is_file($data)) {
238231
return self::parseFile($data, $toArray);
@@ -247,7 +240,7 @@ public static function parse(string $data, bool $toArray = true)
247240
*
248241
* @return array|stdClass
249242
*/
250-
public static function parseFile(string $jsonFile, bool $toArray = true)
243+
public static function parseFile(string $jsonFile, bool $toArray = true): array|stdClass
251244
{
252245
if (!is_file($jsonFile)) {
253246
throw new InvalidArgumentException("File not found: $jsonFile");
@@ -264,7 +257,7 @@ public static function parseFile(string $jsonFile, bool $toArray = true)
264257
* @return array|stdClass
265258
* @noinspection PhpDocMissingThrowsInspection
266259
*/
267-
public static function parseString(string $json, bool $toArray = true)
260+
public static function parseString(string $json, bool $toArray = true): array|stdClass
268261
{
269262
if (!$json = trim($json)) {
270263
return $toArray ? [] : new stdClass();

Diff for: src/Helper/PhpHelper.php

+17-16
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@ class PhpHelper
5656
/**
5757
* @var ReflectionClass[]
5858
*/
59-
private static $reflects = [];
59+
private static array $reflects = [];
6060

6161
/**
6262
* @var ReflectionMethod[]
6363
*/
64-
private static $reflectMths = [];
64+
private static array $reflectMths = [];
6565

6666
/**
67-
* @param string|object $classOrObj
67+
* @param object|string $classOrObj
6868
*
6969
* @return ReflectionClass
7070
* @throws ReflectionException
7171
*/
72-
public static function reflectClass($classOrObj): ReflectionClass
72+
public static function reflectClass(object|string $classOrObj): ReflectionClass
7373
{
7474
$id = is_string($classOrObj) ? $classOrObj : get_class($classOrObj);
7575

@@ -81,13 +81,13 @@ public static function reflectClass($classOrObj): ReflectionClass
8181
}
8282

8383
/**
84-
* @param string|object $classOrObj
84+
* @param object|string $classOrObj
8585
* @param string $method
8686
*
8787
* @return ReflectionMethod
8888
* @throws ReflectionException
8989
*/
90-
public static function reflectMethod($classOrObj, string $method): ReflectionMethod
90+
public static function reflectMethod(object|string $classOrObj, string $method): ReflectionMethod
9191
{
9292
$id = is_string($classOrObj) ? $classOrObj : get_class($classOrObj);
9393
$id .= '.' . $method;
@@ -124,7 +124,7 @@ public static function getReflectMethods(ReflectionClass $reflectClass, int $fla
124124
*
125125
* @return mixed
126126
*/
127-
public static function value($value)
127+
public static function value($value): mixed
128128
{
129129
if (is_callable($value)) {
130130
return $value();
@@ -153,11 +153,11 @@ public static function newMemoryStream(string $mode = 'rwb')
153153
* get $_SERVER value
154154
*
155155
* @param string $name
156-
* @param string|mixed $default
156+
* @param mixed $default
157157
*
158158
* @return mixed
159159
*/
160-
public static function serverParam(string $name, $default = '')
160+
public static function serverParam(string $name, mixed $default = ''): mixed
161161
{
162162
$name = strtoupper($name);
163163

@@ -170,7 +170,7 @@ public static function serverParam(string $name, $default = '')
170170
*
171171
* @return mixed
172172
*/
173-
public static function call($cb, ...$args)
173+
public static function call(mixed $cb, ...$args): mixed
174174
{
175175
if (is_string($cb)) {
176176
// function
@@ -199,7 +199,7 @@ public static function call($cb, ...$args)
199199
*
200200
* @return mixed
201201
*/
202-
public static function callByArray(callable $cb, array $args)
202+
public static function callByArray(callable $cb, array $args): mixed
203203
{
204204
return self::call($cb, ...$args);
205205
}
@@ -209,12 +209,12 @@ public static function callByArray(callable $cb, array $args)
209209
* - 会先尝试用 setter 方法设置属性
210210
* - 再尝试直接设置属性
211211
*
212-
* @param mixed $object An object instance
212+
* @param object $object An object instance
213213
* @param array $options
214214
*
215215
* @return mixed
216216
*/
217-
public static function initObject($object, array $options)
217+
public static function initObject(object $object, array $options): object
218218
{
219219
return ObjectHelper::init($object, $options);
220220
}
@@ -223,13 +223,13 @@ public static function initObject($object, array $options)
223223
* 获取资源消耗
224224
*
225225
* @param int $startTime
226-
* @param int|float $startMem
226+
* @param float|int $startMem
227227
* @param array $info
228228
* @param bool $realUsage
229229
*
230230
* @return array
231231
*/
232-
public static function runtime(int $startTime, $startMem, array $info = [], bool $realUsage = false): array
232+
public static function runtime(int $startTime, float|int $startMem, array $info = [], bool $realUsage = false): array
233233
{
234234
$info['startTime'] = $startTime;
235235
$info['endTime'] = microtime(true);
@@ -384,9 +384,10 @@ public static function toString($anyData): string
384384
/**
385385
* @param string $pathname
386386
* @param int|string $projectId This must be a one character
387+
*
387388
* @return int|string
388389
*/
389-
public static function ftok(string $pathname, $projectId)
390+
public static function ftok(string $pathname, int|string $projectId): int|string
390391
{
391392
if (strlen($projectId) > 1) {
392393
throw new RuntimeException("The project id must be a one character(int/str). Input: $projectId");

Diff for: src/Util/AutoLoader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function findFile(string $class): bool|string
302302
*
303303
* @return bool|string
304304
*/
305-
private function findFileWithExtension(string $class)
305+
private function findFileWithExtension(string $class): bool|string
306306
{
307307
// PSR-4 lookup
308308
$logicalPathPsr4 = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';

Diff for: src/Util/Contract/PipelineInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function add(callable $stage): PipelineInterface;
3232
*
3333
* @return mixed
3434
*/
35-
public function run($payload);
35+
public function run(mixed $payload): mixed;
3636

3737
/**
3838
* Makes pipeline callable. Does same as {@see run()}
@@ -41,5 +41,5 @@ public function run($payload);
4141
*
4242
* @return mixed
4343
*/
44-
public function __invoke($payload);
44+
public function __invoke(mixed $payload): mixed;
4545
}

0 commit comments

Comments
 (0)