toArray()   C
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 5
nop 0
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * @category        Payone
16
 * @package         Payone_Settings
17
 * @subpackage      Data
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone_Settings
28
 * @subpackage      Data
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
abstract class Payone_Settings_Data_ConfigFile_Abstract
34
    implements Payone_Settings_Data_ConfigFile_Interface
35
{
36
    /**
37
     * @abstract
38
     * @return array
39
     */
40
    public function toArray()
41
    {
42
        $array = array();
43
44
45
        foreach ($this as $key => $data)
0 ignored issues
show
Bug introduced by
The expression $this of type this<Payone_Settings_Data_ConfigFile_Abstract> is not traversable.
Loading history...
46
        {
47
            if ($data === null || $key == 'key') {
48
                continue;
49
            }
50
51
            if ($data instanceof Payone_Settings_Data_ConfigFile_Interface) {
52
                /** @var Payone_Api_Request_Parameter_Interface $data */
53
                $array[$key] = $data->toArray();
54
            }
55
            elseif (is_array($data))
56
            {
57
                foreach ($data as $innerKey => $innerValue)
58
                {
59
                    if ($innerValue instanceof Payone_Settings_Data_ConfigFile_Interface) {
60
                        /** @var Payone_Api_Request_Parameter_Interface $innerValue */
61
                        $array[$key][$innerValue->getKey()] = $innerValue->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Payone_Api_Request_Parameter_Interface as the method getKey() does only exist in the following implementations of said interface: Payone_Api_Request_Parameter_Paydata_DataItem.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
62
                    }
63
                    else {
64
                        $array[$key][$innerKey] = $innerValue;
65
                    }
66
                }
67
            }
68
            else {
69
                $array[$key] = $data;
70
            }
71
        }
72
73
        return $array;
74
    }
75
}
76