@@ -189,10 +189,11 @@ public static function zhSubStr($str, $start = 0, $length, $charset = 'utf-8', $
189189 * @param array|string $param -
190190 * @internal param string $chars
191191 * @return string
192+ * @throws \Exception
192193 */
193194 public static function random ($ length , array $ param = []): string
194195 {
195- $ param = array_merge ([
196+ $ param = \ array_merge ([
196197 'prefix ' => '' ,
197198 'suffix ' => '' ,
198199 'chars ' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '
@@ -211,24 +212,28 @@ public static function random($length, array $param = []): string
211212
212213 /**
213214 * @param int $length
214- * @return bool| string
215+ * @return string
215216 */
216- public static function genSalt ($ length = 32 )
217+ public static function genSalt (int $ length = 32 ): string
217218 {
218- return substr (str_replace ('+ ' , '. ' , base64_encode (hex2bin (random_token ($ length )))), 0 , 44 );
219+ return \substr (
220+ \str_replace ('+ ' , '. ' , \base64_encode (\hex2bin (\random_token ($ length )))),
221+ 0 ,
222+ 44
223+ );
219224 }
220225
221226 /**
222227 * @param int $length
223228 * @return bool|string
224229 */
225- public static function genUid ($ length = 7 )
230+ public static function genUid (int $ length = 7 ): string
226231 {
227232 if (!\is_int ($ length ) || $ length > 32 || $ length < 1 ) {
228233 $ length = 7 ;
229234 }
230235
231- return substr (hash ('md5 ' , uniqid ('' , true )), 0 , $ length );
236+ return \ substr (\ hash ('md5 ' , \ uniqid ('' , true )), 0 , $ length );
232237 }
233238
234239 /**
@@ -268,7 +273,7 @@ public static function strtolower($str)
268273 return $ str ;
269274 }
270275
271- return \function_exists ('mb_strtolower ' ) ? mb_strtolower ($ str , 'utf-8 ' ) : strtolower ($ str );
276+ return \function_exists ('mb_strtolower ' ) ? \ mb_strtolower ($ str , 'utf-8 ' ) : \ strtolower ($ str );
272277 }
273278
274279 /**
@@ -281,7 +286,7 @@ public static function strtoupper($str)
281286 return $ str ;
282287 }
283288
284- return \function_exists ('mb_strtoupper ' ) ? mb_strtoupper ($ str , 'utf-8 ' ) : strtoupper ($ str );
289+ return \function_exists ('mb_strtoupper ' ) ? \ mb_strtoupper ($ str , 'utf-8 ' ) : \ strtoupper ($ str );
285290 }
286291
287292 /**
@@ -291,13 +296,13 @@ public static function strtoupper($str)
291296 * @param string $encoding
292297 * @return bool|string
293298 */
294- public static function substr ($ str , $ start , $ length = false , $ encoding = 'utf-8 ' )
299+ public static function substr (string $ str , int $ start , int $ length = null , string $ encoding = 'utf-8 ' )
295300 {
296301 if (\function_exists ('mb_substr ' )) {
297- return mb_substr ($ str , ( int ) $ start , ($ length === false ? self ::strlen ($ str ) : (int )$ length ), $ encoding );
302+ return mb_substr ($ str , $ start , ($ length === null ? self ::strlen ($ str ) : (int )$ length ), $ encoding );
298303 }
299304
300- return substr ($ str , $ start , ($ length === false ? self ::strlen ($ str ) : (int )$ length ));
305+ return substr ($ str , $ start , ($ length === null ? self ::strlen ($ str ) : (int )$ length ));
301306 }
302307
303308 /**
@@ -307,23 +312,25 @@ public static function substr($str, $start, $length = false, $encoding = 'utf-8'
307312 * @param string $encoding
308313 * @return bool|int
309314 */
310- public static function strpos ($ str , $ find , $ offset = 0 , $ encoding = 'UTF-8 ' )
315+ public static function strpos (string $ str , string $ find , int $ offset = 0 , string $ encoding = 'UTF-8 ' )
311316 {
312- return \function_exists ('mb_strpos ' ) ? mb_strpos ($ str , $ find , $ offset , $ encoding ) : strpos ($ str , $ find ,
313- $ offset );
317+ return \function_exists ('mb_strpos ' ) ?
318+ mb_strpos ($ str , $ find , $ offset , $ encoding ) :
319+ strpos ($ str , $ find , $ offset );
314320 }
315321
316322 /**
317- * @param $str
318- * @param $find
323+ * @param string $str
324+ * @param string $find
319325 * @param int $offset
320326 * @param string $encoding
321327 * @return bool|int
322328 */
323- public static function strrpos ($ str , $ find , $ offset = 0 , $ encoding = 'utf-8 ' )
329+ public static function strrpos (string $ str , string $ find , int $ offset = 0 , string $ encoding = 'utf-8 ' )
324330 {
325- return \function_exists ('mb_strrpos ' ) ? mb_strrpos ($ str , $ find , $ offset , $ encoding ) : strrpos ($ str , $ find ,
326- $ offset );
331+ return \function_exists ('mb_strrpos ' ) ?
332+ mb_strrpos ($ str , $ find , $ offset , $ encoding ) :
333+ strrpos ($ str , $ find , $ offset );
327334 }
328335
329336 /**
@@ -350,7 +357,7 @@ public static function ucwords($str): string
350357 * @param string $sep
351358 * @return array
352359 */
353- public static function toArray ($ str , $ sep = ', ' ): array
360+ public static function toArray (string $ str , string $ sep = ', ' ): array
354361 {
355362 $ array = [];
356363
@@ -373,23 +380,23 @@ public static function toArray($str, $sep = ','): array
373380 * @param string $sep
374381 * @return array
375382 */
376- public static function str2array ($ str , $ sep = ', ' ): array
383+ public static function str2array (string $ str , string $ sep = ', ' ): array
377384 {
378385 $ str = trim ($ str , "$ sep " );
379386
380387 if (!$ str ) {
381388 return [];
382389 }
383390
384- return preg_split ("/\s* $ sep\s*/ " , $ str , -1 , PREG_SPLIT_NO_EMPTY );
391+ return \ preg_split ("/\s* $ sep\s*/ " , $ str , -1 , PREG_SPLIT_NO_EMPTY );
385392 }
386393
387394 /**
388395 * @param string $path
389396 * @param string $separator
390397 * @return array
391398 */
392- public static function split2Array ($ path , $ separator = '. ' ): array
399+ public static function split2Array (string $ path , string $ separator = '. ' ): array
393400 {
394401 return array_values (array_filter (explode ($ separator , $ path ), '\strlen ' ));
395402 }
@@ -421,7 +428,7 @@ public static function truncate($str, $max_length, $suffix = '...'): string
421428 * @param null|int $length
422429 * @return string
423430 */
424- public static function truncate_two ( $ str , $ start , $ length = null ): string
431+ public static function truncate2 ( string $ str , int $ start , int $ length = null ): string
425432 {
426433 if (!$ length ) {
427434 $ length = $ start ;
@@ -448,7 +455,7 @@ public static function truncate_two($str, $start, $length = null): string
448455 * @param array $options
449456 * @return bool|string
450457 */
451- public static function truncateString ( $ text , $ length = 120 , array $ options = array ())
458+ public static function truncate3 ( string $ text , int $ length = 120 , array $ options = array ())
452459 {
453460 $ default = [
454461 'ellipsis ' => '... ' ,
@@ -575,7 +582,12 @@ public static function truncateString($text, $length = 120, array $options = arr
575582 return $ truncate ;
576583 }
577584
578- public static function toCamel ($ str , $ upperFirstChar = false )
585+ /**
586+ * @param $str
587+ * @param bool $upperFirstChar
588+ * @return mixed
589+ */
590+ public static function toCamel (string $ str , bool $ upperFirstChar = false ): string
579591 {
580592 return self ::toCamelCase ($ str , $ upperFirstChar );
581593 }
@@ -587,9 +599,9 @@ public static function toCamel($str, $upperFirstChar = false)
587599 * @param bool $upperFirstChar
588600 * @return mixed
589601 */
590- public static function toCamelCase ($ str , $ upperFirstChar = false )
602+ public static function toCamelCase (string $ str , bool $ upperFirstChar = false ): string
591603 {
592- $ str = self ::strtolower ($ str );
604+ $ str = ( string ) self ::strtolower ($ str );
593605
594606 if ($ upperFirstChar ) {
595607 $ str = self ::ucfirst ($ str );
@@ -600,7 +612,7 @@ public static function toCamelCase($str, $upperFirstChar = false)
600612 }, $ str );
601613 }
602614
603- public static function toSnake ($ str , $ sep = '_ ' ): string
615+ public static function toSnake (string $ str , string $ sep = '_ ' ): string
604616 {
605617 return self ::toSnakeCase ($ str , $ sep );
606618 }
@@ -611,7 +623,7 @@ public static function toSnake($str, $sep = '_'): string
611623 * @param string $sep
612624 * @return string
613625 */
614- public static function toSnakeCase ($ str , $ sep = '_ ' ): string
626+ public static function toSnakeCase (string $ str , string $ sep = '_ ' ): string
615627 {
616628 // 'CMSCategories' => 'cms_categories'
617629 // 'RangePrice' => 'range_price'
@@ -620,18 +632,18 @@ public static function toSnakeCase($str, $sep = '_'): string
620632
621633 /**
622634 * 驼峰式 <=> 下划线式
623- * @param [type] $str [description]
635+ * @param string $str [description]
624636 * @param bool $toCamelCase
625637 * true : 驼峰式 => 下划线式
626638 * false : 驼峰式 <= 下划线式
627- * @return mixed| string
639+ * @return string
628640 */
629- public static function nameChange ($ str , $ toCamelCase = true )
641+ public static function nameChange (string $ str , bool $ toCamelCase = true ): string
630642 {
631643 $ str = trim ($ str );
632644
633645 // 默认 :下划线式 =>驼峰式
634- if (( bool ) $ toCamelCase ) {
646+ if ($ toCamelCase ) {
635647 if (strpos ($ str , '_ ' ) === false ) {
636648 return $ str ;
637649 }
@@ -647,7 +659,7 @@ public static function nameChange($str, $toCamelCase = true)
647659 }
648660
649661 // 驼峰式 => 下划线式
650- return strtolower (preg_replace ('/((?<=[a-z])(?=[A-Z]))/ ' , '_ ' , $ str ));
662+ return \ strtolower (preg_replace ('/((?<=[a-z])(?=[A-Z]))/ ' , '_ ' , $ str ));
651663 }
652664
653665 /**
0 commit comments