SmartFormSelectElement   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 58.62 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 34
loc 58
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __construct() 34 48 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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