SmartFormSelectElement::__construct()   C
last analyzed

Complexity

Conditions 12
Paths 104

Size

Total Lines 48

Duplication

Lines 34
Ratio 70.83 %

Importance

Changes 0
Metric Value
cc 12
nc 104
nop 2
dl 34
loc 48
rs 6.9333
c 0
b 0
f 0

How to fix   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\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 SmartFormSelectElement
17
 * @package XoopsModules\Smartobject\Form\Elements
18
 */
19
class SmartFormSelectElement extends \XoopsFormSelect
20
{
21
    public $multiple = false;
22
23
    /**
24
     * SmartFormSelectElement constructor.
25
     * @param string $object
26
     * @param string $key
27
     */
28
    public function __construct($object, $key)
29
    {
30
        $var  = $object->vars[$key];
31
        $size = isset($var['size']) ? $var['size'] : ($this->multiple ? 5 : 1);
32
33
        // Adding the options inside this SelectBox
34
        // If the custom method is not from a module, than it's from the core
35
        $control = $object->getControl($key);
0 ignored issues
show
Bug introduced by
The method getControl 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...
36
37
        $value = isset($control['value']) ? $control['value'] : $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...
38
39
        parent::__construct($var['form_caption'], $key, $value, $size, $this->multiple);
40
41 View Code Duplication
        if (isset($control['options'])) {
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...
42
            $this->addOptionArray($control['options']);
43
        } else {
44
            // let's find if the method we need to call comes from an already defined object
45
            if (isset($control['object'])) {
46
                if (method_exists($control['object'], $control['method'])) {
47
                    if ($option_array = $control['object']->{$control['method']}()) {
48
                        // Adding the options array to the XoopsFormSelect
49
                        $this->addOptionArray($option_array);
50
                    }
51
                }
52
            } else {
53
                // finding the itemHandler; if none, let's take the itemHandler of the $object
54
                if (isset($control['itemHandler'])) {
55
                    if (!$control['module']) {
56
                        // Creating the specified core object handler
57
                        $controlHandler = xoops_getHandler($control['itemHandler']);
58
                    } else {
59
                        $controlHandler = xoops_getModuleHandler($control['itemHandler'], $control['module']);
60
                    }
61
                } else {
62
                    $controlHandler = $object->handler;
63
                }
64
65
                // Checking if the specified method exists
66
                if (method_exists($controlHandler, $control['method'])) {
67
                    // TODO: How could I pass the parameters in the following call ...
68
                    if ($option_array = $controlHandler->{$control['method']}()) {
69
                        // Adding the options array to the XoopsFormSelect
70
                        $this->addOptionArray($option_array);
71
                    }
72
                }
73
            }
74
        }
75
    }
76
}
77