SmartFormParentCategoryElement::getOptionArray()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 5
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartobject\Form\Elements;
2
3
/**
4
 * Contains the SmartObjectControl class
5
 *
6
 * @license    GNU
7
 * @author     marcan <[email protected]>
8
 * @link       http://smartfactory.ca The SmartFactory
9
 * @package    SmartObject
10
 * @subpackage SmartObjectForm
11
 */
12
13
use XoopsModules\Smartobject;
14
15
/**
16
 * Class SmartFormParentCategoryElement
17
 * @package XoopsModules\Smartobject\Form\Elements
18
 */
19
class SmartFormParentCategoryElement extends \XoopsFormSelect
20
{
21
    /**
22
     * SmartFormParentcategoryElement constructor.
23
     * @param string $object
24
     * @param string $key
25
     */
26
    public function __construct($object, $key)
27
    {
28
        $addNoParent = isset($object->controls[$key]['addNoParent']) ? $object->controls[$key]['addNoParent'] : true;
29
        $criteria    = new \CriteriaCompo();
30
        $criteria->setSort('weight, name');
31
        $categoryHandler = xoops_getModuleHandler('category', $object->handler->_moduleName);
32
        $categories      = $categoryHandler->getObjects($criteria);
33
34
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
35
        $mytree = new \XoopsObjectTree($categories, 'categoryid', 'parentid');
36
        parent::__construct($object->vars[$key]['form_caption'], $key, $object->getVar($key, 'e'));
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $object (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
38
        $ret     = [];
39
        $options = $this->getOptionArray($mytree, 'name', 0, '', $ret);
40
        if ($addNoParent) {
41
            $newOptions = ['0' => '----'];
42
            foreach ($options as $k => $v) {
43
                $newOptions[$k] = $v;
44
            }
45
            $options = $newOptions;
46
        }
47
        $this->addOptionArray($options);
48
    }
49
50
    /**
51
     * Get options for a category select with hierarchy (recursive)
52
     *
53
     * @param \XoopsObjectTree $tree
54
     * @param string          $fieldName
55
     * @param int             $key
56
     * @param string          $prefix_curr
57
     * @param array           $ret
58
     *
59
     * @return array
60
     */
61
    public function getOptionArray($tree, $fieldName, $key, $prefix_curr = '', &$ret)
62
    {
63
        if ($key > 0) {
64
            $value       = $tree->tree[$key]['obj']->getVar($tree->_myId);
0 ignored issues
show
Unused Code introduced by
$value 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...
65
            $ret[$key]   = $prefix_curr . $tree->tree[$key]['obj']->getVar($fieldName);
66
            $prefix_curr .= '-';
67
        }
68
        if (isset($tree->tree[$key]['child']) && !empty($tree->tree[$key]['child'])) {
69
            foreach ($tree->tree[$key]['child'] as $childkey) {
70
                $this->getOptionArray($tree, $fieldName, $childkey, $prefix_curr, $ret);
71
            }
72
        }
73
74
        return $ret;
75
    }
76
}
77