Skip to content

Commit 1dbd830

Browse files
author
liutao
committed
feat: format
1 parent c888e58 commit 1dbd830

File tree

8 files changed

+192
-187
lines changed

8 files changed

+192
-187
lines changed

.php-cs-fixer.php

Lines changed: 66 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -2,217 +2,103 @@
22

33
declare(strict_types=1);
44

5-
return (new PhpCsFixer\Config())
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\Finder;
7+
8+
return (new Config())
69
->setRiskyAllowed(true)
710
->setRules([
811
'@PSR2' => true,
912
'@Symfony' => true,
1013
'@DoctrineAnnotation' => true,
1114
'@PhpCsFixer' => true,
12-
'global_namespace_import' => [
13-
'import_classes' => true,
14-
'import_constants' => true,
15-
'import_functions' => null,
15+
'array_syntax' => [
16+
'syntax' => 'short',
1617
],
17-
'array_indentation' => true,
18-
'array_syntax' => ['syntax' => 'short'],
19-
'binary_operator_spaces' => [
20-
'default' => 'single_space',
18+
'list_syntax' => [
19+
'syntax' => 'short',
20+
],
21+
'concat_space' => [
22+
'spacing' => 'one',
2123
],
22-
'blank_line_after_namespace' => true,
23-
'blank_line_after_opening_tag' => true,
2424
'blank_line_before_statement' => [
2525
'statements' => [
26-
'continue',
27-
'return',
26+
'declare',
2827
],
2928
],
30-
'blank_lines_before_namespace' => true,
31-
'control_structure_braces' => true,
32-
'control_structure_continuation_position' => [
33-
'position' => 'same_line',
34-
],
35-
'curly_braces_position' => [
36-
'control_structures_opening_brace' => 'same_line',
37-
'functions_opening_brace' => 'next_line_unless_newline_at_signature_end',
38-
'anonymous_functions_opening_brace' => 'same_line',
39-
'classes_opening_brace' => 'next_line_unless_newline_at_signature_end',
40-
'anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end',
41-
'allow_single_line_empty_anonymous_classes' => false,
42-
'allow_single_line_anonymous_functions' => false,
43-
],
44-
'cast_spaces' => true,
45-
'class_attributes_separation' => [
46-
'elements' => [
47-
'const' => 'one',
48-
'method' => 'one',
49-
'property' => 'one',
50-
'trait_import' => 'none',
29+
'general_phpdoc_annotation_remove' => [
30+
'annotations' => [
31+
'author',
5132
],
5233
],
53-
'class_definition' => [
54-
'multi_line_extends_each_single_line' => true,
55-
'single_item_single_line' => true,
56-
'single_line' => true,
57-
],
58-
'clean_namespace' => true,
59-
'compact_nullable_typehint' => true,
60-
'concat_space' => [
61-
'spacing' => 'one',
34+
'ordered_imports' => [
35+
'imports_order' => [
36+
'class', 'function', 'const',
37+
],
38+
'sort_algorithm' => 'alpha',
6239
],
63-
'constant_case' => ['case' => 'lower'],
64-
'declare_equal_normalize' => true,
65-
'declare_parentheses' => true,
66-
'elseif' => true,
67-
'encoding' => true,
68-
'full_opening_tag' => true,
69-
'fully_qualified_strict_types' => true,
70-
'function_declaration' => true,
71-
'general_phpdoc_tag_rename' => true,
72-
'heredoc_to_nowdoc' => true,
73-
'include' => true,
74-
'increment_style' => ['style' => 'post'],
75-
'indentation_type' => true,
76-
'integer_literal_case' => true,
77-
'lambda_not_used_import' => true,
78-
'linebreak_after_opening_tag' => true,
79-
'line_ending' => true,
80-
'list_syntax' => true,
81-
'lowercase_cast' => true,
82-
'lowercase_keywords' => true,
83-
'lowercase_static_reference' => true,
84-
'magic_method_casing' => true,
85-
'magic_constant_casing' => true,
86-
'method_argument_space' => [
87-
'on_multiline' => 'ignore',
40+
'yoda_style' => [
41+
'always_move_variable' => false,
42+
'equal' => false,
43+
'identical' => false,
8844
],
89-
'method_chaining_indentation' => true,
9045
'multiline_whitespace_before_semicolons' => [
9146
'strategy' => 'no_multi_line',
9247
],
93-
'native_function_casing' => true,
94-
'native_function_type_declaration_casing' => true,
95-
'no_alias_functions' => true,
96-
'no_alias_language_construct_call' => true,
97-
'no_alternative_syntax' => true,
98-
'no_binary_string' => true,
99-
'no_blank_lines_after_class_opening' => true,
100-
'no_blank_lines_after_phpdoc' => true,
101-
'no_closing_tag' => true,
102-
'no_empty_phpdoc' => true,
103-
'no_empty_statement' => true,
48+
'constant_case' => [
49+
'case' => 'lower',
50+
],
51+
'class_attributes_separation' => true,
52+
'combine_consecutive_unsets' => true,
53+
// 注释结尾是否必须要加句号
54+
'phpdoc_summary' => false,
55+
// 文件严格模式
56+
'declare_strict_types' => true,
57+
'linebreak_after_opening_tag' => true,
58+
'lowercase_static_reference' => true,
10459
'no_extra_blank_lines' => [
10560
'tokens' => [
61+
'attribute',
62+
'break',
63+
'case',
64+
'continue',
65+
'curly_brace_block',
66+
'default',
10667
'extra',
68+
'parenthesis_brace_block',
69+
'return',
70+
'square_brace_block',
71+
'switch',
10772
'throw',
108-
'use',
73+
'use'
10974
],
11075
],
111-
'no_leading_import_slash' => true,
112-
'no_leading_namespace_whitespace' => true,
113-
'no_mixed_echo_print' => [
114-
'use' => 'echo',
115-
],
116-
'no_multiline_whitespace_around_double_arrow' => true,
117-
'no_multiple_statements_per_line' => true,
118-
'no_short_bool_cast' => true,
119-
'no_singleline_whitespace_before_semicolons' => true,
120-
'no_spaces_after_function_name' => true,
121-
'no_space_around_double_colon' => true,
122-
'no_spaces_around_offset' => [
123-
'positions' => ['inside', 'outside'],
124-
],
125-
'no_spaces_inside_parenthesis' => true,
126-
'no_superfluous_phpdoc_tags' => [
127-
'allow_mixed' => true,
128-
'allow_unused_params' => true,
129-
],
130-
'no_trailing_comma_in_singleline' => true,
131-
'no_trailing_whitespace' => true,
132-
'no_trailing_whitespace_in_comment' => true,
133-
'no_unneeded_control_parentheses' => [
134-
'statements' => ['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield'],
76+
'single_line_comment_style' => [
77+
'comment_types' => [
78+
],
13579
],
136-
'no_unneeded_curly_braces' => true,
137-
'no_unset_cast' => true,
80+
'no_useless_else' => true,
13881
'no_unused_imports' => true,
139-
'no_unreachable_default_argument_value' => true,
140-
'no_useless_return' => true,
141-
'no_whitespace_before_comma_in_array' => true,
142-
'no_whitespace_in_blank_line' => true,
143-
'normalize_index_brace' => true,
14482
'not_operator_with_successor_space' => true,
145-
'nullable_type_declaration' => true,
146-
'nullable_type_declaration_for_default_null_value' => [
147-
'use_nullable_type_declaration' => false,
148-
],
149-
'object_operator_without_whitespace' => true,
150-
'ordered_imports' => ['sort_algorithm' => 'alpha'],
151-
'psr_autoloading' => false,
152-
'phpdoc_indent' => true,
153-
'phpdoc_inline_tag_normalizer' => true,
154-
'phpdoc_no_access' => true,
155-
'phpdoc_no_package' => true,
156-
'phpdoc_no_useless_inheritdoc' => true,
157-
'phpdoc_order' => [
158-
'order' => ['param', 'return', 'throws'],
159-
],
160-
'phpdoc_scalar' => true,
161-
'phpdoc_separation' => [
162-
'groups' => [
163-
['deprecated', 'link', 'see', 'since'],
164-
['author', 'copyright', 'license'],
165-
['category', 'package', 'subpackage'],
166-
['property', 'property-read', 'property-write'],
167-
['param', 'return'],
168-
],
169-
],
170-
'phpdoc_single_line_var_spacing' => true,
171-
'phpdoc_summary' => false,
172-
'phpdoc_to_comment' => false,
173-
'phpdoc_tag_type' => [
174-
'tags' => [
175-
'inheritdoc' => 'inline',
176-
],
177-
],
178-
'phpdoc_trim' => true,
179-
'phpdoc_types' => true,
180-
'phpdoc_var_without_name' => true,
181-
'return_type_declaration' => ['space_before' => 'none'],
182-
'self_accessor' => false,
183-
'self_static_accessor' => true,
184-
'short_scalar_cast' => true,
185-
'simplified_null_return' => false,
186-
'single_blank_line_at_eof' => true,
187-
'single_class_element_per_statement' => [
188-
'elements' => ['const', 'property'],
189-
],
190-
'single_import_per_statement' => true,
191-
'single_line_after_imports' => true,
192-
'single_line_comment_style' => [
193-
'comment_types' => ['hash'],
194-
],
83+
// 全局命名空间导入
84+
'global_namespace_import' => true,
85+
'not_operator_with_space' => false,
86+
'ordered_class_elements' => true,
87+
'php_unit_strict' => false,
88+
'phpdoc_separation' => false,
19589
'single_quote' => true,
196-
'single_space_around_construct' => true,
197-
'space_after_semicolon' => true,
19890
'standardize_not_equals' => true,
199-
'statement_indentation' => true,
200-
'switch_case_semicolon_to_colon' => true,
201-
'switch_case_space' => true,
202-
'ternary_operator_spaces' => true,
203-
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
204-
'trim_array_spaces' => true,
205-
'type_declaration_spaces' => true,
206-
'types_spaces' => true,
207-
'unary_operator_spaces' => true,
208-
'visibility_required' => [
209-
'elements' => ['method', 'property'],
210-
],
211-
'whitespace_after_comma_in_array' => true,
212-
])->setFinder(
213-
PhpCsFixer\Finder::create()
91+
'multiline_comment_opening_closing' => true,
92+
])
93+
->setFinder(
94+
Finder::create()
95+
->exclude('bin')
21496
->exclude('public')
21597
->exclude('runtime')
21698
->exclude('vendor')
99+
->exclude('database/migrations')
100+
->exclude('database/seeders')
217101
->in(__DIR__)
218-
);
102+
)
103+
->setUsingCache(true)
104+
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache');

app/Controller/IndexController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Dto\Request\IndexGetReq;
88
use App\Dto\Response\IndexGetRes;
9+
use App\Util\JsonResponse;
910
use MicroPHP\Framework\Controller;
1011
use MicroPHP\Framework\Http\Response;
1112
use MicroPHP\Framework\Http\ServerRequest;
@@ -27,7 +28,7 @@ class IndexController extends Controller
2728

2829
#[Get(summary: '首页', tags: [self::TAG])]
2930
#[SuccessJsonResponse(ref: IndexGetRes::class)]
30-
public function index(ServerRequest $request): Response
31+
public function index(): Response
3132
{
3233
return $this->json('Hello World');
3334
}
@@ -37,6 +38,10 @@ public function index(ServerRequest $request): Response
3738
#[SuccessJsonResponse(ref: IndexGetRes::class)]
3839
public function get(ServerRequest $request): Response
3940
{
40-
return $this->json(new IndexGetRes(['id' => $request->input('id'), 'name' => 'hello']));
41+
$param = IndexGetReq::fromRequest($request);
42+
43+
return $this->json(
44+
JsonResponse::success(new IndexGetRes(['id' => $param->id, 'name' => 'hello']))
45+
);
4146
}
4247
}

app/Dto/Request/IndexGetReq.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace App\Dto\Request;
56

6-
use MicroPHP\Data\Data;
7+
use MicroPHP\Framework\Dto\Request\BaseReq;
78
use MicroPHP\Swagger\Schema\Property;
89
use MicroPHP\Swagger\Schema\Schema;
10+
use Symfony\Component\Validator\Constraints\Positive;
911

1012
#[Schema(title: 'index请求参数')]
11-
class IndexGetReq extends Data
13+
class IndexGetReq extends BaseReq
1214
{
1315
#[Property(title: 'ID', example: 1)]
14-
public int $id = 1;
15-
}
16+
#[Positive(message: 'ID必须为正整数')]
17+
public int $id;
18+
}

app/Dto/Response/IndexGetRes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace App\Dto\Response;
@@ -15,4 +16,4 @@ class IndexGetRes extends Data
1516

1617
#[Property(title: '名字', example: 'test')]
1718
public string $name;
18-
}
19+
}

app/Enum/StatusEnum.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Enum;
6+
7+
use MicroPHP\Contract\Enum\Attributes\EnumClass;
8+
use MicroPHP\Contract\Enum\Attributes\EnumProperty;
9+
use MicroPHP\Contract\Enum\EnumHelper;
10+
11+
#[EnumClass(name: '状态码枚举类')]
12+
enum StatusEnum: int
13+
{
14+
use EnumHelper;
15+
16+
#[EnumProperty(label: 'success')]
17+
case SUCCESS = 200;
18+
19+
#[EnumProperty(label: 'bad request')]
20+
case BAD_REQUEST = 400;
21+
22+
#[EnumProperty(label: 'system error')]
23+
case ERROR = 500;
24+
}

0 commit comments

Comments
 (0)