Completed
Push — master ( 2ad47c...163a33 )
by Cheren
44:31
created

Table   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeMarshal() 0 4 1
A _prepareParamsData() 0 7 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\ORM;
17
18
use JBZoo\Data\JSON;
19
use Cake\Event\Event;
20
use Cake\Database\Type;
21
use Cake\ORM\Table as CakeTable;
22
23
/**
24
 * Class Table
25
 *
26
 * @package Core\ORM
27
 */
28
class Table extends CakeTable
29
{
30
31
    /**
32
     * Callback before request data is converted into entities.
33
     *
34
     * @param Event $event
35
     * @param \ArrayObject $data
36
     * @param \ArrayObject $options
37
     * @return void
38
     * @SuppressWarnings("unused")
39
     */
40
    public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $this->_prepareParamsData($data);
43
    }
44
45
    /**
46
     * Prepare params data.
47
     *
48
     * @param \ArrayObject $data
49
     * @return void
50
     */
51
    protected function _prepareParamsData(\ArrayObject $data)
52
    {
53
        if (isset($data['params'])) {
54
            $params = new JSON((array) $data['params']);
55
            $data->offsetSet('params', $params);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ArrayObject as the method offsetSet() does only exist in the following sub-classes of ArrayObject: ArrayObjectExt, Cake\ORM\SaveOptionsBuilder, JBZoo\Data\Data, JBZoo\Data\Ini, JBZoo\Data\JSON, JBZoo\Data\PHPArray, JBZoo\Data\Yml. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
        }
57
    }
58
}
59