Skip to content

Commit 2342236

Browse files
LordSimalArshid
authored andcommitted
deprecate Form::_execute (cakephp#18725)
1 parent 45efd35 commit 2342236

2 files changed

Lines changed: 93 additions & 20 deletions

File tree

src/Form/Form.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Cake\Utility\Hash;
2424
use Cake\Validation\ValidatorAwareInterface;
2525
use Cake\Validation\ValidatorAwareTrait;
26+
use ReflectionMethod;
27+
use function Cake\Core\deprecationWarning;
2628

2729
/**
2830
* Form abstraction used to create forms not tied to ORM backed models,
@@ -33,7 +35,7 @@
3335
*
3436
* This class is most useful when subclassed. In a subclass you
3537
* should define the `_buildSchema`, `validationDefault` and optionally,
36-
* the `_execute` methods. These allow you to declare your form's
38+
* the `process` methods. These allow you to declare your form's
3739
* fields, validation and primary action respectively.
3840
*
3941
* Forms are conventionally placed in the `App\Form` namespace.
@@ -243,7 +245,7 @@ public function setErrors(array $errors)
243245
/**
244246
* Execute the form if it is valid.
245247
*
246-
* First validates the form, then calls the `_execute()` hook method.
248+
* First validates the form, then calls the `process()` hook method.
247249
* This hook method can be implemented in subclasses to perform
248250
* the action of the form. This may be sending email, interacting
249251
* with a remote API, or anything else you may need.
@@ -256,21 +258,45 @@ public function setErrors(array $errors)
256258
* @param array $data Form data.
257259
* @param array<string, mixed> $options List of options.
258260
* @return bool False on validation failure, otherwise returns the
259-
* result of the `_execute()` method.
261+
* result of the `process()` method.
260262
*/
261263
public function execute(array $data, array $options = []): bool
262264
{
263-
$this->_data = $data;
265+
// check for deprecated _execute() method - https://github.com/cakephp/cakephp/pull/18725
266+
$childClass = static::class;
267+
$parentClass = self::class;
268+
$method = new ReflectionMethod($childClass, '_execute');
269+
$hasOverwrittenExecute = $method->getDeclaringClass()->getName() !== $parentClass;
264270

271+
$this->_data = $data;
265272
$options += ['validate' => true];
266273

267274
if ($options['validate'] === false) {
268-
return $this->_execute($data);
275+
if ($hasOverwrittenExecute) {
276+
deprecationWarning(
277+
'5.3.0',
278+
'The _execute() method is deprecated. Override the process() method instead.',
279+
);
280+
281+
return $this->_execute($data);
282+
}
283+
284+
return $this->process($data);
269285
}
270286

271287
$validator = $options['validate'] === true ? static::DEFAULT_VALIDATOR : $options['validate'];
288+
$validateResult = $this->validate($data, $validator);
289+
290+
if ($hasOverwrittenExecute) {
291+
deprecationWarning(
292+
'5.3.0',
293+
'The _execute() method is deprecated. Override the process() method instead.',
294+
);
272295

273-
return $this->validate($data, $validator) && $this->_execute($data);
296+
return $validateResult && $this->_execute($data);
297+
}
298+
299+
return $validateResult && $this->process($data);
274300
}
275301

276302
/**
@@ -280,8 +306,22 @@ public function execute(array $data, array $options = []): bool
280306
*
281307
* @param array $data Form data.
282308
* @return bool
309+
* @deprecated 5.3.0 Override process() instead.
283310
*/
284311
protected function _execute(array $data): bool
312+
{
313+
return $this->process($data);
314+
}
315+
316+
/**
317+
* Hook method to be implemented in subclasses.
318+
*
319+
* Used by `execute()` to execute the form's action.
320+
*
321+
* @param array $data Form data.
322+
* @return bool
323+
*/
324+
protected function process(array $data): bool
285325
{
286326
return true;
287327
}

tests/TestCase/Form/FormTest.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,21 @@ public function testSetErrors(): void
165165
*/
166166
public function testExecuteInvalid(): void
167167
{
168-
$form = new class extends Form {
169-
// phpcs:ignore CakePHP.NamingConventions.ValidFunctionName.PublicWithUnderscore
170-
public function _execute(array $data): bool
171-
{
172-
throw new Exception('Should not be called');
173-
}
174-
};
175-
$form->getValidator()
176-
->add('email', 'format', ['rule' => 'email']);
177-
$data = [
178-
'email' => 'rong',
179-
];
180-
181-
$this->assertFalse($form->execute($data));
168+
$this->deprecated(function (): void {
169+
$form = new class extends Form {
170+
protected function _execute(array $data): bool
171+
{
172+
throw new Exception('Should not be called');
173+
}
174+
};
175+
$form->getValidator()
176+
->add('email', 'format', ['rule' => 'email']);
177+
$data = [
178+
'email' => 'rong',
179+
];
180+
181+
$this->assertFalse($form->execute($data));
182+
});
182183
}
183184

184185
/**
@@ -196,6 +197,38 @@ public function testExecuteValid(): void
196197
$this->assertTrue($form->execute($data));
197198
}
198199

200+
/**
201+
* test execute() when data is valid.
202+
*/
203+
public function testExecuteWithProcess(): void
204+
{
205+
$form = new class extends Form {
206+
public function process(array $data): bool
207+
{
208+
return false;
209+
}
210+
};
211+
212+
$this->assertFalse($form->execute([]));
213+
}
214+
215+
/**
216+
* test execute()
217+
*/
218+
public function testExecuteWithExecuteAndNoValidate(): void
219+
{
220+
$this->deprecated(function (): void {
221+
$form = new class extends Form {
222+
protected function _execute(array $data): bool
223+
{
224+
return false;
225+
}
226+
};
227+
228+
$this->assertFalse($form->execute([], ['validate' => false]));
229+
});
230+
}
231+
199232
/**
200233
* test execute() when data is valid.
201234
*/

0 commit comments

Comments
 (0)