Completed
Branch master (856cc5)
by Mihail
07:21 queued 04:29
created

MultiCheckboxField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace Ffcms\Core\Helper\HTML\Form;
3
4
use Ffcms\Core\App;
5
use Ffcms\Core\Helper\HTML\System\NativeGenerator;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Helper\Type\Arr;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Exception\SyntaxException;
10
11
class MultiCheckboxField extends NativeGenerator implements iField
12
{
13
14
    private $properties;
15
16
    private $name;
17
18
    private $value;
19
20
    /**
21
     * MultiCheckboxField constructor.
22
     * Pass params inside.
23
     * 
24
     * @param array $properties            
25
     * @param string $name            
26
     * @param string|null $value            
27
     */
28
    public function __construct($properties, $name, $value = null)
29
    {
30
        $this->properties = $properties;
31
        $this->name = $name;
32
        $this->value = $value;
33
    }
34
35
    /**
36
     * Build <input type="checkbox" checked {$properties} /> response
37
     * @todo: fix bug when option is unseted
38
     * {@inheritDoc}
39
     * @see \Ffcms\Core\Helper\HTML\Form\iField::make()
40
     */
41
    public function make()
42
    {
43
        // check if options is defined
44
        $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...
45
        if (isset($this->properties['options']) && Obj::isArray($this->properties['options'])) {
46
            $options = $this->properties['options'];
47
        } else {
48
            throw new SyntaxException('Options for field ' . self::nohtml($this->name) . ' is not defined');
49
        }
50
        
51
        // set field type
52
        $this->properties['type'] = 'checkbox';
53
        // set this field as array html dom object
54
        $this->properties['name'] .= '[]';
55
        unset($this->properties['value'], $this->properties['id']);
56
        
57
        $build = null;
58
        foreach ($options as $opt) {
59
            // check if this is active element
60
            if (Obj::isArray($this->value) && Arr::in($opt, $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...
61
                $this->properties['checked'] = null;
62
            } else {
63
                unset($this->properties['checked']); // remove checked if it setted before
64
            }
65
            $this->properties['value'] = $opt;
66
            // apply structured checkboxes style for each item
67
            $build .= App::$View->render('native/form/multi_checkboxes_list', [
68
                'item' => self::buildSingleTag('input', $this->properties) . self::nohtml($opt)
69
            ]);
70
        }
71
        
72
        return $build;
73
    }
74
}