CrudEditAction::validates()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
crap 2.0185
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action;
13
14
use CakeDC\Api\Exception\ValidationException;
15
16
/**
17
 * Class CrudEditAction
18
 *
19
 * @package CakeDC\Api\Service\Action
20
 */
21
class CrudEditAction extends CrudAction
22
{
23
24
    /**
25
     * Apply validation process.
26
     *
27
     * @return bool
28
     */
29 3
    public function validates()
30
    {
31 3
        $validator = $this->getTable()->getValidator();
32 3
        $errors = $validator->errors($this->getData(), false);
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Validation\Validator::errors() has been deprecated with message: 3.9.0 Renamed to validate()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33 3
        if (!empty($errors)) {
34
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
35
        }
36
37 3
        return true;
38
    }
39
40
    /**
41
     * Execute action.
42
     *
43
     * @return mixed
44
     */
45 6
    public function execute()
46
    {
47 6
        $entity = $this->_getEntity($this->_id);
48 4
        $entity = $this->_patchEntity($entity, $this->getData());
0 ignored issues
show
Documentation introduced by
$entity is of type object<Cake\Collection\Collection>, but the function expects a object<Cake\Datasource\EntityInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
50 4
        return $this->_save($entity);
51
    }
52
}
53