Completed
Push — master ( 329d6c...681acd )
by Mihail
03:11
created

MultiSelectField::make()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 40
Code Lines 23

Duplication

Lines 8
Ratio 20 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 8
loc 40
rs 4.909
cc 9
eloc 23
nc 6
nop 0
1
<?php
2
namespace Ffcms\Core\Helper\HTML\Form;
3
4
use Ffcms\Core\Helper\HTML\System\NativeGenerator;
5
use Ffcms\Core\Helper\Type\Obj;
6
use Ffcms\Core\Helper\Type\Arr;
7
use Ffcms\Core\Exception\SyntaxException;
8
9
class MultiSelectField extends NativeGenerator implements iField
10
{
11
    private $properties;
12
    private $name;
13
    private $value;
14
15
    /**
16
     * MultiSelectField constructor. Pass params inside.
17
     * @param array $properties
18
     * @param string $name
19
     * @param string|null $value
20
     */
21
    public function __construct($properties, $name, $value = null)
22
    {
23
        $this->properties = $properties;
24
        $this->name = $name;
25
        $this->value = $value;
26
    }
27
28
    /**
29
     * Build <select @properties>@optionsDOM</select> container 
30
     * {@inheritDoc}
31
     * @see \Ffcms\Core\Helper\HTML\Form\iField::make()
32
     */
33
    public function make()
34
    {
35
        // check if options is defined
36
        $options = false;
0 ignored issues
show
Unused Code introduced by
$options 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...
37
        $optionsKey = (bool)$this->properties['optionsKey'];
38
        if (isset($this->properties['options']) && Obj::isArray($this->properties['options'])) {
39
            $options = $this->properties['options'];
40
        } else {
41
            throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not defined');
42
        }
43
        
44
        // set global field type
45
        $this->properties['type'] = 'select';
46
        // add multiple element
47
        $this->properties['multiple'] = null;
48
        $this->properties['name'] .= '[]';
49
        unset($this->properties['value'], $this->properties['options'], $this->properties['optionsKey']);
50
51
        // build options as HTML DOM element: <option @value>@text</option>
52
        $optionsDOM = null;
53
        foreach ($options as $val => $text) {
54
            // check if options key is value
55
            $optionProperty = null;
56
            if ($optionsKey === true) {
57
                $optionProperty['value'] = $val;
58
                // check if current element is active
59 View Code Duplication
                if (Obj::isArray($this->value) && Arr::in((string)$val, $this->value)) {
0 ignored issues
show
Documentation introduced by
$this->value is of type string|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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...
60
                    $optionProperty['selected'] = 'selected';
61
                }
62 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...
63
                if (Obj::isArray($this->value) && Arr::in((string)$text, $this->value)) {
0 ignored issues
show
Documentation introduced by
$this->value is of type string|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
                    $optionProperty['selected'] = 'selected';
65
                }
66
            }
67
            $optionsDOM .= self::buildContainerTag('option', $optionProperty, $text);
68
        }
69
        
70
        // build <select @properties>@optionsDOM</select>
71
        return self::buildContainerTag('select', $this->properties, $optionsDOM, true);
72
    }
73
}