Skip to content

Commit 1850c59

Browse files
committed
update some and format codes
1 parent eefc29c commit 1850c59

22 files changed

+799
-80
lines changed

.php_cs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,42 @@ This file is part of toolkit/stdlib.
88
@license MIT
99
EOF;
1010

11+
$rules = [
12+
'@PSR2' => true,
13+
'array_syntax' => [
14+
'syntax' => 'short'
15+
],
16+
'list_syntax' => [
17+
'syntax' => 'short'
18+
],
19+
'class_attributes_separation' => true,
20+
'declare_strict_types' => true,
21+
'global_namespace_import' => [
22+
'import_constants' => true,
23+
'import_functions' => true,
24+
],
25+
'header_comment' => [
26+
'comment_type' => 'PHPDoc',
27+
'header' => $header,
28+
'separate' => 'bottom'
29+
],
30+
'no_unused_imports' => true,
31+
'return_type_declaration' => [
32+
'space_before' => 'none',
33+
],
34+
'single_quote' => true,
35+
'standardize_not_equals' => true,
36+
'void_return' => true, // add :void for method
37+
];
38+
1139
return PhpCsFixer\Config::create()
12-
->setRiskyAllowed(true)
13-
->setRules([
14-
'@PSR2' => true,
15-
'array_syntax' => [
16-
'syntax' => 'short'
17-
],
18-
'list_syntax' => [
19-
'syntax' => 'short'
20-
],
21-
'class_attributes_separation' => true,
22-
'declare_strict_types' => true,
23-
'global_namespace_import' => [
24-
'import_constants' => true,
25-
'import_functions' => true,
26-
],
27-
'header_comment' => [
28-
'comment_type' => 'PHPDoc',
29-
'header' => $header,
30-
'separate' => 'bottom'
31-
],
32-
'no_unused_imports' => true,
33-
'single_quote' => true,
34-
'standardize_not_equals' => true,
35-
])
36-
->setFinder(
37-
PhpCsFixer\Finder::create()
38-
// ->exclude('test')
39-
->exclude('runtime')
40-
->exclude('vendor')
41-
->in(__DIR__)
42-
)
43-
->setUsingCache(false);
40+
->setRiskyAllowed(true)
41+
->setRules($rules)
42+
->setFinder(
43+
PhpCsFixer\Finder::create()
44+
// ->exclude('test')
45+
->exclude('runtime')
46+
->exclude('vendor')
47+
->in(__DIR__)
48+
)
49+
->setUsingCache(false);

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
# StdLib
22

3-
stdlib tools for PHP
3+
[![License](https://img.shields.io/github/license/php-toolkit/stdlib)](LICENSE)
4+
[![Php Version](https://img.shields.io/badge/php-%3E7.1.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/stdlib)
5+
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/stdlib.svg)](https://packagist.org/packages/toolkit/stdlib)
6+
7+
Some useful basic tool class for php.
8+
9+
Contains:
10+
11+
- array handle
12+
- object handle
13+
- string handle
14+
- simple autoloader
15+
- dot env load `.env`
16+
- common php helper
17+
- os env info
18+
- and more ...
19+
20+
## Install
21+
22+
```bash
23+
composer require toolkit/stdlib
24+
```
425

526
## License
627

src/Arr/ArrayHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public static function valueTrim(array $data)
250250
return trim($data);
251251
}
252252

253-
array_walk_recursive($data, function (&$value) {
253+
array_walk_recursive($data, function (&$value): void {
254254
$value = trim($value);
255255
});
256256

src/Arr/FixedArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __isset(string $key)
7272
* @param string $key
7373
* @param mixed $value
7474
*/
75-
public function __set(string $key, $value)
75+
public function __set(string $key, $value): void
7676
{
7777
$this->offsetSet($key, $value);
7878
}

src/Helper/PhpHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php declare(strict_types=1);
22
/**
3-
* This file is part of toolkit/phpkit.
3+
* This file is part of toolkit/stdlib.
44
*
55
* @author https://github.com/inhere
6-
* @link https://github.com/php-toolkit/phpkit
6+
* @link https://github.com/php-toolkit/stdlib
77
* @license MIT
88
*/
99

@@ -12,7 +12,6 @@
1212
use Toolkit\Stdlib\Obj\ObjectHelper;
1313
use function array_sum;
1414
use function explode;
15-
use function get_defined_constants;
1615
use function is_array;
1716
use function is_object;
1817
use function is_string;

src/OS.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php declare(strict_types=1);
22
/**
3-
* This file is part of toolkit/phpkit.
3+
* This file is part of toolkit/stdlib.
44
*
55
* @author https://github.com/inhere
6-
* @link https://github.com/php-toolkit/phpkit
6+
* @link https://github.com/php-toolkit/stdlib
77
* @license MIT
88
*/
99

src/Obj/Traits/PropertyAccessByGetterSetterTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait PropertyAccessByGetterSetterTrait
3737
*
3838
* @throws SetPropertyException
3939
*/
40-
public function __set($name, $value)
40+
public function __set($name, $value): void
4141
{
4242
$setter = 'set' . ucfirst($name);
4343

@@ -94,7 +94,7 @@ public function __isset($name)
9494
*
9595
* @throws PropertyException
9696
*/
97-
public function __unset($name)
97+
public function __unset($name): void
9898
{
9999
$setter = 'set' . ucfirst($name);
100100

src/Php.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/stdlib.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/stdlib
7+
* @license MIT
8+
*/
29

310
namespace Toolkit\Stdlib;
411

@@ -11,5 +18,4 @@
1118
*/
1219
class Php extends PhpHelper
1320
{
14-
1521
}

src/Str/HtmlHelper.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,61 @@
99

1010
namespace Toolkit\Stdlib\Str;
1111

12-
use function is_string;
13-
use function htmlspecialchars;
12+
use function array_key_exists;
13+
use function html_entity_decode;
1414
use function htmlentities;
15-
use function strpos;
16-
use function is_array;
15+
use function htmlspecialchars;
1716
use function htmlspecialchars_decode;
18-
use function html_entity_decode;
17+
use function is_array;
18+
use function is_string;
1919
use function preg_match_all;
20-
use function array_key_exists;
20+
use function preg_replace;
21+
use function strpos;
2122

2223
/**
2324
* Class HtmlHelper
25+
*
2426
* @package Toolkit\Stdlib\Str
2527
*/
2628
class HtmlHelper
2729
{
2830
/**
2931
* Encodes special characters into HTML entities.
32+
*
3033
* @param string $text data to be encoded
3134
* @param string $charset
35+
*
3236
* @return string the encoded data
3337
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
3438
*/
35-
public static function encode($text, $charset = 'utf-8'): string
39+
public static function encode(string $text, string $charset = 'utf-8'): string
3640
{
3741
return htmlspecialchars($text, ENT_QUOTES, $charset);
3842
}
3943

4044
/**
4145
* This is the opposite of {@link encode()}.
46+
*
4247
* @param string $text data to be decoded
48+
*
4349
* @return string the decoded data
4450
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
4551
*/
46-
public static function decode($text): string
52+
public static function decode(string $text): string
4753
{
4854
return htmlspecialchars_decode($text, ENT_QUOTES);
4955
}
5056

5157
/**
5258
* @form yii1
59+
*
5360
* @param array $data data to be encoded
5461
* @param string $charset
62+
*
5563
* @return array the encoded data
56-
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
64+
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
5765
*/
58-
public static function encodeArray($data, $charset = 'utf-8'): array
66+
public static function encodeArray($data, string $charset = 'utf-8'): array
5967
{
6068
$d = [];
6169

@@ -84,12 +92,14 @@ public static function encodeArray($data, $charset = 'utf-8'): array
8492
* htmlentities() <--> html_entity_decode() — 将特殊的 HTML 实体转换回普通字符
8593
* htmlspecialchars() <--> htmlspecialchars_decode() — 将特殊的 HTML 实体转换回普通字符
8694
* ENT_COMPAT ENT_QUOTES ENT_NOQUOTES ENT_HTML401 ENT_XML1 ENT_XHTML ENT_HTML5
95+
*
8796
* @param $data
8897
* @param int $type
8998
* @param string $encoding
99+
*
90100
* @return array|mixed|string
91101
*/
92-
public static function escape($data, int $type = 0, $encoding = 'UTF-8')
102+
public static function escape($data, int $type = 0, string $encoding = 'UTF-8')
93103
{
94104
if (is_array($data)) {
95105
foreach ($data as $k => $v) {
@@ -108,26 +118,28 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
108118

109119
//如‘&#x5FD7;’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
110120
if (strpos($data, '&#')) {
111-
$data = \preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $data);
121+
$data = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $data);
112122
}
113123

114124
return $data;
115125
}
116126

117127
/**
118128
* 去掉html转义
119-
* @param $data
120-
* @param int $type
121-
* @param string $encoding
129+
*
130+
* @param string|array $data
131+
* @param int $type
132+
* @param string $encoding
133+
*
122134
* @return array|string
123135
*/
124-
public static function unescap($data, $type = 0, $encoding = 'UTF-8')
136+
public static function unescap($data, $type = 0, string $encoding = 'UTF-8')
125137
{
126138
if (is_array($data)) {
127139
foreach ($data as $k => $v) {
128140
$data[$k] = self::unescap($data, $type, $encoding);
129141
}
130-
} elseif (!$type) {//默认使用 htmlspecialchars_decode()
142+
} elseif (!$type) {// 默认使用 htmlspecialchars_decode()
131143
$data = htmlspecialchars_decode($data, \ENT_QUOTES);
132144
} else {
133145
$data = html_entity_decode($data, \ENT_QUOTES, $encoding);
@@ -138,7 +150,9 @@ public static function unescap($data, $type = 0, $encoding = 'UTF-8')
138150

139151
/**
140152
* Strip img-tags from string
141-
* @param string $string Sting to be cleaned.
153+
*
154+
* @param string $string Sting to be cleaned.
155+
*
142156
* @return string Cleaned string
143157
*/
144158
public static function stripImages(string $string): string
@@ -148,37 +162,44 @@ public static function stripImages(string $string): string
148162

149163
/**
150164
* Strip iframe-tags from string
151-
* @param string $string Sting to be cleaned.
165+
*
166+
* @param string $string Sting to be cleaned.
167+
*
152168
* @return string Cleaned string
153169
*/
154170
public static function stripIframes(string $string): string
155171
{
156-
return \preg_replace('#(<[/]?iframe.*>)#U', '', $string);
172+
return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
157173
}
158174

159175
/**
160176
* stripScript
177+
*
161178
* @param string $string
179+
*
162180
* @return string
163181
*/
164182
public static function stripScript(string $string): string
165183
{
166-
return \preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
184+
return preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
167185
}
168186

169187
/**
170188
* stripStyle
189+
*
171190
* @param string $string
191+
*
172192
* @return string
173193
*/
174194
public static function stripStyle(string $string): string
175195
{
176-
return \preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
196+
return preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
177197
}
178198

179199
/**
180200
* @param string $html
181201
* @param bool|true $onlySrc
202+
*
182203
* @return array
183204
*/
184205
public static function matchImages(string $html, bool $onlySrc = true): array
@@ -199,6 +220,7 @@ public static function matchImages(string $html, bool $onlySrc = true): array
199220

200221
/**
201222
* @param string $html
223+
*
202224
* @return string
203225
*/
204226
public static function minify(string $html): string
@@ -212,6 +234,6 @@ public static function minify(string $html): string
212234
];
213235
$replace = [' ', ' ', '>', '<', '\\1'];
214236

215-
return \preg_replace($search, $replace, $html);
237+
return preg_replace($search, $replace, $html);
216238
}
217239
}

0 commit comments

Comments
 (0)