Skip to content

Commit efa1969

Browse files
authored
Added php cs fixer. (#8)
1 parent 9e96b35 commit efa1969

12 files changed

Lines changed: 376 additions & 133 deletions

.php-cs-fixer.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
8+
use PhpCsFixer\Config;
9+
use PhpCsFixer\Finder;
10+
11+
$header = <<<'EOF'
12+
本文件属于KK馆版权所有。
13+
This file belong to KKGUAN.
14+
EOF;
15+
16+
return (new Config())
17+
->setRiskyAllowed(true)
18+
->setRules([
19+
'@PSR2' => true,
20+
'@Symfony' => true,
21+
'@DoctrineAnnotation' => true,
22+
'@PhpCsFixer' => true,
23+
'header_comment' => [
24+
'comment_type' => 'PHPDoc',
25+
'header' => $header,
26+
'separate' => 'none',
27+
'location' => 'after_declare_strict',
28+
],
29+
'array_syntax' => [
30+
'syntax' => 'short',
31+
],
32+
'list_syntax' => [
33+
'syntax' => 'short',
34+
],
35+
'concat_space' => [
36+
'spacing' => 'one',
37+
],
38+
'blank_line_before_statement' => [
39+
'statements' => [
40+
'declare',
41+
],
42+
],
43+
'general_phpdoc_annotation_remove' => [
44+
'annotations' => [
45+
'author',
46+
],
47+
],
48+
'ordered_imports' => [
49+
'imports_order' => [
50+
'class', 'function', 'const',
51+
],
52+
'sort_algorithm' => 'alpha',
53+
],
54+
'single_line_comment_style' => [
55+
'comment_types' => [
56+
],
57+
],
58+
'yoda_style' => [
59+
'always_move_variable' => false,
60+
'equal' => false,
61+
'identical' => false,
62+
],
63+
'phpdoc_align' => [
64+
'align' => 'left',
65+
],
66+
'multiline_whitespace_before_semicolons' => [
67+
'strategy' => 'no_multi_line',
68+
],
69+
'constant_case' => [
70+
'case' => 'lower',
71+
],
72+
'global_namespace_import' => [
73+
'import_classes' => true,
74+
'import_constants' => true,
75+
'import_functions' => true,
76+
],
77+
'class_attributes_separation' => true,
78+
'combine_consecutive_unsets' => true,
79+
'declare_strict_types' => true,
80+
'linebreak_after_opening_tag' => true,
81+
'lowercase_static_reference' => true,
82+
'no_useless_else' => true,
83+
'no_unused_imports' => true,
84+
'not_operator_with_successor_space' => true,
85+
'not_operator_with_space' => false,
86+
'ordered_class_elements' => true,
87+
'php_unit_strict' => false,
88+
'phpdoc_separation' => false,
89+
'single_quote' => true,
90+
'standardize_not_equals' => true,
91+
'multiline_comment_opening_closing' => true,
92+
'single_line_empty_body' => false,
93+
])
94+
->setFinder(
95+
Finder::create()
96+
->exclude('public')
97+
->exclude('runtime')
98+
->exclude('vendor')
99+
->in(__DIR__)
100+
)
101+
->setUsingCache(false);

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"hyperf/validation": "^3.0",
2121
"jchook/phpunit-assert-throws": "^1.0",
2222
"mockery/mockery": "^1.4",
23-
"phpunit/phpunit": "^9.5"
23+
"phpunit/phpunit": "^9.5",
24+
"friendsofphp/php-cs-fixer": "^3.0"
2425
},
2526
"autoload": {
2627
"psr-4": {
@@ -44,6 +45,7 @@
4445
}
4546
},
4647
"scripts": {
47-
"test": "phpunit -c phpunit.xml --colors=always"
48+
"test": "phpunit -c phpunit.xml --colors=always",
49+
"cs-fix": "@php vendor/bin/php-cs-fixer fix $1"
4850
}
4951
}

examples/base.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
38
require __DIR__ . '/../vendor/autoload.php';
49

510
use KK\Validation\ValidationErrorDumper;
@@ -38,11 +43,11 @@
3843

3944
$validator = new Validator([
4045
'type' => 'required|numeric',
41-
'foo.*.bar' => 'numeric|max:256'
46+
'foo.*.bar' => 'numeric|max:256',
4247
]);
4348
try {
4449
$validator->validate([
45-
'type' => 'xxx'
50+
'type' => 'xxx',
4651
]);
4752
} catch (ValidationException $e) {
4853
// Attribute 'type' violates the following rules: numeric
@@ -51,27 +56,27 @@
5156
try {
5257
$validator->validate([
5358
'type' => 1,
54-
'foo' => [['bar' => '1024']]
59+
'foo' => [['bar' => '1024']],
5560
]);
5661
} catch (ValidationException $e) {
5762
// Attribute 'foo.0.bar' violates the following rules: max:256
5863
echo ValidationErrorDumper::dump($e->errors()) . "\n";
5964
}
6065

6166
$validator = new Validator([
62-
'a.*.b.*.c.*.d.*.e' => 'numeric'
67+
'a.*.b.*.c.*.d.*.e' => 'numeric',
6368
]);
6469
try {
6570
$validator->validate([
66-
'a' => [['b' => [['c' => [['d' => [['e' => 'xxx']]]]]]]]
71+
'a' => [['b' => [['c' => [['d' => [['e' => 'xxx']]]]]]]],
6772
]);
6873
} catch (ValidationException $e) {
6974
// Attribute 'a.0.b.0.c.0.d.0.e' violates the following rules: numeric
7075
echo ValidationErrorDumper::dump($e->errors()) . "\n";
7176
}
7277

7378
$validator = new Validator([
74-
'*' => 'numeric'
79+
'*' => 'numeric',
7580
]);
7681
try {
7782
$validator->validate(['0', '1', '2', '3']);
@@ -88,7 +93,7 @@
8893
}
8994

9095
$validator = new Validator([
91-
'foo.*' => 'integer'
96+
'foo.*' => 'integer',
9297
]);
9398
try {
9499
$validator->validate(['foo' => ['0', '0.1', '0.2', '1']]);
@@ -105,7 +110,7 @@
105110
}
106111

107112
$validator = new Validator([
108-
'*.*.*' => 'integer'
113+
'*.*.*' => 'integer',
109114
]);
110115
try {
111116
$validator->validate(['foo' => ['bar' => 'not array']]);
@@ -126,7 +131,7 @@
126131
}
127132

128133
$validator = new Validator([
129-
'foo' => 'string|max:255'
134+
'foo' => 'string|max:255',
130135
]);
131136
try {
132137
$validator->validate(['foo' => []]);
@@ -142,7 +147,7 @@
142147
}
143148

144149
$validator = new Validator([
145-
'foo' => 'required|max:255'
150+
'foo' => 'required|max:255',
146151
]);
147152
try {
148153
$validator->validate(['foo' => null]);
@@ -158,7 +163,7 @@
158163
}
159164

160165
$validator = new Validator([
161-
'foo' => 'max:255'
166+
'foo' => 'max:255',
162167
]);
163168
try {
164169
$validator->validate(['foo' => null]);
@@ -167,7 +172,7 @@
167172
}
168173

169174
$validator = new Validator([
170-
'foo' => 'min:1|max:255'
175+
'foo' => 'min:1|max:255',
171176
]);
172177
try {
173178
$validator->validate(['foo' => null]);
@@ -189,7 +194,7 @@
189194
}
190195

191196
$validator = new Validator([
192-
'foo' => 'in:1, 2, 3'
197+
'foo' => 'in:1, 2, 3',
193198
]);
194199
try {
195200
$validator->validate(['foo' => null]);
@@ -226,7 +231,7 @@
226231
}
227232

228233
$validator = new Validator([
229-
'foo' => 'required|array|in:9, 8, 7, 6, 5, 4, 3' // in map
234+
'foo' => 'required|array|in:9, 8, 7, 6, 5, 4, 3', // in map
230235
]);
231236
try {
232237
$validator->validate(['foo' => []]);
@@ -242,7 +247,7 @@
242247
}
243248

244249
$validator = new Validator([
245-
'foo' => 'alpha'
250+
'foo' => 'alpha',
246251
]);
247252
try {
248253
$validator->validate(['foo' => '1']);
@@ -257,7 +262,7 @@
257262
}
258263

259264
$validator = new Validator([
260-
'foo' => 'alpha_num'
265+
'foo' => 'alpha_num',
261266
]);
262267
try {
263268
$validator->validate(['foo' => 'xyz123']);
@@ -272,7 +277,7 @@
272277
}
273278

274279
$validator = new Validator([
275-
'foo' => 'alpha_dash'
280+
'foo' => 'alpha_dash',
276281
]);
277282
try {
278283
$validator->validate(['foo' => 'xyz_123-v4']);

src/Adapter/HyperfValidator.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
8+
39
namespace KK\Validation\Adapter;
410

5-
use Hyperf\Contract\TranslatorInterface;
6-
use Hyperf\Contract\ValidatorInterface;
711
use Hyperf\Collection\Arr;
812
use Hyperf\Contract\MessageBag as MessageBagContract;
13+
use Hyperf\Contract\TranslatorInterface;
14+
use Hyperf\Contract\ValidatorInterface;
15+
use Hyperf\Stringable\Str;
916
use Hyperf\Support\Fluent;
1017
use Hyperf\Support\MessageBag;
11-
use Hyperf\Stringable\Str;
1218
use Hyperf\Validation\Concerns;
1319
use Hyperf\Validation\Contract\PresenceVerifierInterface;
1420
use Hyperf\Validation\ValidationException;
@@ -17,8 +23,8 @@
1723
use Psr\Container\ContainerInterface;
1824
use RuntimeException;
1925

20-
use function Hyperf\Collection\data_get;
2126
use function Hyperf\Collection\collect;
27+
use function Hyperf\Collection\data_get;
2228

2329
class HyperfValidator implements ValidatorInterface
2430
{
@@ -155,7 +161,7 @@ class HyperfValidator implements ValidatorInterface
155161
/**
156162
* The Presence Verifier implementation.
157163
*
158-
* @var \Hyperf\Validation\Contract\PresenceVerifierInterface
164+
* @var PresenceVerifierInterface
159165
*/
160166
protected $presenceVerifier;
161167

@@ -230,10 +236,15 @@ public function validated(): array
230236
}
231237

232238
$results = [];
233-
234239
$missingValue = Str::random(10);
235240

236-
foreach (array_keys($this->getRules()) as $key) {
241+
foreach ($this->getRules() as $key => $rules) {
242+
// 跳过包含通配符的规则键,避免创建虚拟的["*"]键
243+
// 这些规则只用于验证,不应该影响最终的数据结构
244+
if (is_string($key) && str_contains($key, '*')) {
245+
continue;
246+
}
247+
237248
$value = data_get($this->getData(), $key, $missingValue);
238249

239250
if ($value !== $missingValue) {
@@ -340,7 +351,7 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
340351
/**
341352
* Get the Presence Verifier implementation.
342353
*
343-
* @throws \RuntimeException
354+
* @throws RuntimeException
344355
*/
345356
public function getPresenceVerifier(): PresenceVerifierInterface
346357
{

src/ValidationErrorDumper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
8+
39
namespace KK\Validation;
410

511
use function implode;

src/ValidationException.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
8+
39
namespace KK\Validation;
410

11+
use Exception;
512
use JetBrains\PhpStorm\Pure;
613

7-
class ValidationException extends \Exception
14+
class ValidationException extends Exception
815
{
916
#[Pure]
1017
public function __construct(protected array $errors)

src/ValidationPair.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* 本文件属于KK馆版权所有。
6+
* This file belong to KKGUAN.
7+
*/
8+
39
namespace KK\Validation;
410

511
use InvalidArgumentException;
12+
613
use function array_map;
714
use function count;
815
use function explode;
9-
use function implode;
1016

1117
class ValidationPair
1218
{

0 commit comments

Comments
 (0)