TreeTable::createTableRow()   F
last analyzed

Complexity

Conditions 18
Paths 5328

Size

Total Lines 95

Duplication

Lines 23
Ratio 24.21 %

Importance

Changes 0
Metric Value
cc 18
nc 5328
nop 2
dl 23
loc 95
rs 0.669
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php namespace XoopsModules\Smartobject;
2
3
/**
4
 * Contains the classes responsible for displaying a tree table filled with records of SmartObjects
5
 *
6
 * @license    GNU
7
 * @author     marcan <[email protected]>
8
 * @link       http://smartfactory.ca The SmartFactory
9
 * @package    SmartObject
10
 * @subpackage SmartObjectTable
11
 */
12
13
use XoopsModules\Smartobject;
14
15
//require_once SMARTOBJECT_ROOT_PATH . 'class/smartobjecttable.php';
16
17
/**
18
 * SmartObjectTreeTable class
19
 *
20
 * Class representing a tree table for displaying SmartObjects
21
 *
22
 * @package SmartObject
23
 * @author  marcan <[email protected]>
24
 * @link    http://smartfactory.ca The SmartFactory
25
 */
26
class TreeTable extends Smartobject\BaseSmartObjectTable
27
{
28
    /**
29
     * SmartObjectTreeTable constructor.
30
     * @param PersistableObjectHandler $objectHandler
31
     * @param bool                     $criteria
32
     * @param array                    $actions
33
     * @param bool                     $userSide
34
     */
35
    public function __construct(
36
        PersistableObjectHandler $objectHandler,
37
        $criteria = false,
38
        $actions = ['edit', 'delete'],
39
        $userSide = false
40
    ) {
41
        $this->SmartObjectTable($objectHandler, $criteria, $actions, $userSide);
42
        $this->_isTree = true;
43
    }
44
45
    /**
46
     * Get children objects given a specific parentid
47
     *
48
     * @var    int $parentid id of the parent which children we want to retreive
49
     * @return array of SmartObject
50
     */
51
    public function getChildrenOf($parentid = 0)
52
    {
53
        return isset($this->_objects[$parentid]) ? $this->_objects[$parentid] : false;
54
    }
55
56
    /**
57
     * @param     $object
58
     * @param int $level
59
     */
60
    public function createTableRow($object, $level = 0)
61
    {
62
        $aObject = [];
63
64
        $i = 0;
65
66
        $aColumns        = [];
67
        $doWeHaveActions = false;
0 ignored issues
show
Unused Code introduced by
$doWeHaveActions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69
        foreach ($this->_columns as $column) {
70
            $aColumn = [];
71
72 View Code Duplication
            if (0 == $i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
                $class = 'head';
74
            } elseif (0 == $i % 2) {
75
                $class = 'even';
76
            } else {
77
                $class = 'odd';
78
            }
79
80
            if ($column->_customMethodForValue && method_exists($object, $column->_customMethodForValue)) {
81
                $method = $column->_customMethodForValue;
82
                $value  = $object->$method();
83 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                /**
85
                 * If the column is the identifier, then put a link on it
86
                 */
87
                if ($column->getKeyName() == $this->_objectHandler->identifierName) {
88
                    $value = $object->getItemLink();
89
                } else {
90
                    $value = $object->getVar($column->getKeyName());
91
                }
92
            }
93
94
            $space = '';
95
            if ($column->getKeyName() == $this->_objectHandler->identifierName) {
96
                for ($i = 0; $i < $level; ++$i) {
97
                    $space .= '--';
98
                }
99
            }
100
101
            if ('' !== $space) {
102
                $space .= '&nbsp;';
103
            }
104
105
            $aColumn['value'] = $space . $value;
106
            $aColumn['class'] = $class;
107
            $aColumn['width'] = $column->getWidth();
108
            $aColumn['align'] = $column->getAlign();
109
            $aColumn['key']   = $column->getKeyName();
110
111
            $aColumns[] = $aColumn;
112
            ++$i;
113
        }
114
115
        $aObject['columns'] = $aColumns;
116
117
        $class            = 'even' === $class ? 'odd' : 'even';
0 ignored issues
show
Bug introduced by
The variable $class does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
118
        $aObject['class'] = $class;
119
120
        $actions = [];
121
122
        // Adding the custom actions if any
123
        foreach ($this->_custom_actions as $action) {
124
            if (method_exists($object, $action)) {
125
                $actions[] = $object->$action();
126
            }
127
        }
128
129
//        require_once SMARTOBJECT_ROOT_PATH . 'class/smartobjectcontroller.php';
130
        $controller = new ObjectController($this->_objectHandler);
131
132 View Code Duplication
        if (in_array('edit', $this->_actions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            $actions[] = $controller->getEditItemLink($object, false, true);
134
        }
135 View Code Duplication
        if (in_array('delete', $this->_actions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
            $actions[] = $controller->getDeleteItemLink($object, false, true);
137
        }
138
        $aObject['actions'] = $actions;
139
140
        $this->_tpl->assign('smartobject_actions_column_width', count($actions) * 30);
141
        $aObject['id']     = $object->id();
142
        $this->_aObjects[] = $aObject;
143
144
        $childrenObjects = $this->getChildrenOf($object->id());
145
146
        $this->_hasActions = $this->_hasActions ? true : count($actions) > 0;
147
148
        if ($childrenObjects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $childrenObjects of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
149
            ++$level;
150
            foreach ($childrenObjects as $subObject) {
151
                $this->createTableRow($subObject, $level);
152
            }
153
        }
154
    }
155
156
    public function createTableRows()
157
    {
158
        $this->_aObjects = [];
159
160
        if (count($this->_objects) > 0) {
161
            foreach ($this->getChildrenOf() as $object) {
162
                $this->createTableRow($object);
163
            }
164
165
            $this->_tpl->assign('smartobject_objects', $this->_aObjects);
166
        } else {
167
            $colspan = count($this->_columns) + 1;
168
            $this->_tpl->assign('smartobject_colspan', $colspan);
169
        }
170
    }
171
172
    /**
173
     * @return mixed
174
     */
175
    public function fetchObjects()
176
    {
177
        $ret = $this->_objectHandler->getObjects($this->_criteria, 'parentid');
178
179
        return $ret;
180
    }
181
}
182