Conditions | 1 |
Total Lines | 154 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public function dataOptions(): array |
||
38 | { |
||
39 | return [ |
||
40 | [ |
||
41 | new Composite([ |
||
42 | new Number(max: 13, pattern: '/1/'), |
||
43 | new Number(max: 14, pattern: '/2/'), |
||
44 | ]), |
||
45 | [ |
||
46 | 'skipOnEmpty' => false, |
||
47 | 'skipOnError' => false, |
||
48 | 'rules' => [ |
||
49 | [ |
||
50 | Number::class, |
||
51 | 'min' => null, |
||
52 | 'max' => 13, |
||
53 | 'incorrectInputMessage' => [ |
||
54 | 'template' => 'The allowed types are integer, float and string.', |
||
55 | 'parameters' => [], |
||
56 | ], |
||
57 | 'notNumberMessage' => [ |
||
58 | 'template' => 'Value must be a number.', |
||
59 | 'parameters' => [], |
||
60 | ], |
||
61 | 'lessThanMinMessage' => [ |
||
62 | 'template' => 'Value must be no less than {min}.', |
||
63 | 'parameters' => ['min' => null], |
||
64 | ], |
||
65 | 'greaterThanMaxMessage' => [ |
||
66 | 'template' => 'Value must be no greater than {max}.', |
||
67 | 'parameters' => ['max' => 13], |
||
68 | ], |
||
69 | 'skipOnEmpty' => false, |
||
70 | 'skipOnError' => false, |
||
71 | 'pattern' => '/1/', |
||
72 | ], |
||
73 | [ |
||
74 | Number::class, |
||
75 | 'min' => null, |
||
76 | 'max' => 14, |
||
77 | 'incorrectInputMessage' => [ |
||
78 | 'template' => 'The allowed types are integer, float and string.', |
||
79 | 'parameters' => [], |
||
80 | ], |
||
81 | 'notNumberMessage' => [ |
||
82 | 'template' => 'Value must be a number.', |
||
83 | 'parameters' => [], |
||
84 | ], |
||
85 | 'lessThanMinMessage' => [ |
||
86 | 'template' => 'Value must be no less than {min}.', |
||
87 | 'parameters' => ['min' => null], |
||
88 | ], |
||
89 | 'greaterThanMaxMessage' => [ |
||
90 | 'template' => 'Value must be no greater than {max}.', |
||
91 | 'parameters' => ['max' => 14], |
||
92 | ], |
||
93 | 'skipOnEmpty' => false, |
||
94 | 'skipOnError' => false, |
||
95 | 'pattern' => '/2/', |
||
96 | ], |
||
97 | ], |
||
98 | ], |
||
99 | ], |
||
100 | 'rule without options' => [ |
||
101 | new Composite([ |
||
102 | new Number(max: 13, pattern: '/1/'), |
||
103 | new RuleWithoutOptions(), |
||
104 | ]), |
||
105 | [ |
||
106 | 'skipOnEmpty' => false, |
||
107 | 'skipOnError' => false, |
||
108 | 'rules' => [ |
||
109 | [ |
||
110 | Number::class, |
||
111 | 'min' => null, |
||
112 | 'max' => 13, |
||
113 | 'incorrectInputMessage' => [ |
||
114 | 'template' => 'The allowed types are integer, float and string.', |
||
115 | 'parameters' => [], |
||
116 | ], |
||
117 | 'notNumberMessage' => [ |
||
118 | 'template' => 'Value must be a number.', |
||
119 | 'parameters' => [], |
||
120 | ], |
||
121 | 'lessThanMinMessage' => [ |
||
122 | 'template' => 'Value must be no less than {min}.', |
||
123 | 'parameters' => [ |
||
124 | 'min' => null, |
||
125 | ], |
||
126 | ], |
||
127 | 'greaterThanMaxMessage' => [ |
||
128 | 'template' => 'Value must be no greater than {max}.', |
||
129 | 'parameters' => [ |
||
130 | 'max' => 13, |
||
131 | ], |
||
132 | ], |
||
133 | 'skipOnEmpty' => false, |
||
134 | 'skipOnError' => false, |
||
135 | 'pattern' => '/1/', |
||
136 | ], |
||
137 | [ |
||
138 | RuleWithoutOptions::class, |
||
139 | ], |
||
140 | ], |
||
141 | ], |
||
142 | ], |
||
143 | 'callable' => [ |
||
144 | new Composite([ |
||
145 | static fn () => (new Result())->addError('Bad value.'), |
||
146 | ]), |
||
147 | [ |
||
148 | 'skipOnEmpty' => false, |
||
149 | 'skipOnError' => false, |
||
150 | 'rules' => [ |
||
151 | [ |
||
152 | Callback::class, |
||
153 | 'method' => null, |
||
154 | 'skipOnEmpty' => false, |
||
155 | 'skipOnError' => false, |
||
156 | ], |
||
157 | ], |
||
158 | ], |
||
159 | ], |
||
160 | 'inheritance' => [ |
||
161 | new class () extends Composite { |
||
162 | public function getRules(): iterable |
||
163 | { |
||
164 | return [ |
||
165 | new Required(), |
||
166 | ]; |
||
167 | } |
||
168 | |||
169 | public function getOptions(): array |
||
170 | { |
||
171 | return [ |
||
172 | 'specific-key' => 42, |
||
173 | 'rules' => $this->dumpRulesAsArray(), |
||
174 | ]; |
||
175 | } |
||
176 | }, |
||
177 | [ |
||
178 | 'specific-key' => 42, |
||
179 | 'rules' => [ |
||
180 | [ |
||
181 | Required::class, |
||
182 | 'message' => [ |
||
183 | 'template' => 'Value cannot be blank.', |
||
184 | 'parameters' => [], |
||
185 | ], |
||
186 | 'notPassedMessage' => [ |
||
187 | 'template' => 'Value not passed.', |
||
188 | 'parameters' => [], |
||
189 | ], |
||
190 | 'skipOnError' => false, |
||
191 | ], |
||
354 |