Conditions | 2 |
Total Lines | 74 |
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 |
||
125 | public function dataValidationFailed(): array |
||
126 | { |
||
127 | return [ |
||
128 | [ |
||
129 | 41, |
||
130 | [ |
||
131 | new Callback(static function (mixed $value, object $rule, ValidationContext $context): Result { |
||
132 | $result = new Result(); |
||
133 | if ($value !== 42) { |
||
134 | $result->addError('Value should be 42!'); |
||
135 | } |
||
136 | |||
137 | return $result; |
||
138 | }), |
||
139 | ], |
||
140 | ['' => ['Value should be 42!']], |
||
141 | ], |
||
142 | 'custom error' => [ |
||
143 | 41, |
||
144 | [ |
||
145 | new Callback(static function (mixed $value, object $rule, ValidationContext $context): Result { |
||
146 | $result = new Result(); |
||
147 | if ($value !== 42) { |
||
148 | $result->addError('Custom error'); |
||
149 | } |
||
150 | |||
151 | return $result; |
||
152 | }), |
||
153 | ], |
||
154 | ['' => ['Custom error']], |
||
155 | ], |
||
156 | 'non-static callable' => [ |
||
157 | new class (1) { |
||
158 | public function __construct( |
||
159 | #[Callback(method: 'validateName')] |
||
160 | #[Callback(method: 'staticValidateName')] |
||
161 | private $age, |
||
162 | ) { |
||
163 | } |
||
164 | |||
165 | private function validateName(mixed $value, RuleInterface $rule, ValidationContext $context): Result |
||
166 | { |
||
167 | if ($value !== $this->age) { |
||
168 | throw new RuntimeException('Method scope was not bound to the object.'); |
||
169 | } |
||
170 | |||
171 | $result = new Result(); |
||
172 | $result->addError('Hello from non-static method.'); |
||
173 | |||
174 | return $result; |
||
175 | } |
||
176 | |||
177 | private static function staticValidateName( |
||
178 | mixed $value, |
||
179 | RuleInterface $rule, |
||
180 | ValidationContext $context |
||
181 | ): Result { |
||
182 | if ($value !== $context->getDataSet()->getAttributeValue('age')) { |
||
183 | throw new RuntimeException('Method scope was not bound to the object.'); |
||
184 | } |
||
185 | |||
186 | $result = new Result(); |
||
187 | $result->addError('Hello from static method.'); |
||
188 | |||
189 | return $result; |
||
190 | } |
||
191 | }, |
||
192 | null, |
||
193 | ['age' => ['Hello from non-static method.', 'Hello from static method.']], |
||
194 | ], |
||
195 | 'class attribute' => [ |
||
196 | new CallbackDto(7, 42), |
||
197 | null, |
||
198 | ['' => ['7 / 42']], |
||
199 | ], |
||
247 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.