Completed
Push — master ( abaac9...0583a5 )
by Mihail
04:25
created

MultiSelectField::make()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 39
Code Lines 22

Duplication

Lines 11
Ratio 28.21 %

Importance

Changes 0
Metric Value
dl 11
loc 39
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 22
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 = $this->properties['options'];
37
        $optionsKey = (bool)$this->properties['optionsKey'];
38 View Code Duplication
        if (!Obj::isIterable($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...
39
            throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not iterable');
40
        }
41
        unset($this->properties['options']);
42
        
43
        // set global field type
44
        $this->properties['type'] = 'select';
45
        // add multiple element
46
        $this->properties['multiple'] = null;
47
        $this->properties['name'] .= '[]';
48
        unset($this->properties['value'], $this->properties['options'], $this->properties['optionsKey']);
49
50
        // build options as HTML DOM element: <option @value>@text</option>
51
        $optionsDOM = null;
52
        foreach ($options as $val => $text) {
53
            // check if options key is value
54
            $optionProperty = null;
55
            if ($optionsKey === true) {
56
                $optionProperty['value'] = $val;
57
                // check if current element is active
58 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...
59
                    $optionProperty['selected'] = 'selected';
60
                }
61 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...
62
                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...
63
                    $optionProperty['selected'] = 'selected';
64
                }
65
            }
66
            $optionsDOM .= self::buildContainerTag('option', $optionProperty, $text);
67
        }
68
        
69
        // build <select @properties>@optionsDOM</select>
70
        return self::buildContainerTag('select', $this->properties, $optionsDOM, true);
71
    }
72
}