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

RadioField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 21.21 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 14
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B make() 14 37 6

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
2
3
namespace Ffcms\Core\Helper\HTML\Form;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\HTML\System\NativeGenerator;
9
use Ffcms\Core\Helper\Type\Obj;
10
11
class RadioField extends NativeGenerator implements iField
12
{
13
    private $properties;
14
    private $name;
15
    private $value;
16
17
    /**
18
     * RadioField constructor. Pass inside values.
19
     * @param array $properties
20
     * @param string $name
21
     * @param string|null $value
22
     */
23
    public function __construct($properties, $name, $value = null)
24
    {
25
        $this->properties = $properties;
26
        $this->name = $name;
27
        $this->value = $value;
28
29
        // set default input type
30
        $this->properties['type'] = 'radio';
31
    }
32
33
    /**
34
     * Make function of current field type. Return compiled html response
35
     * @return string
36
     * @throws \Ffcms\Core\Exception\NativeException
37
     * @throws \Ffcms\Core\Exception\SyntaxException
38
     */
39
    public function make()
40
    {
41
        // get options from properties
42
        $options = $this->properties['options'];
43 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...
44
            throw new SyntaxException('Radio field ' . self::nohtml($this->name) . ' have no iterable options');
45
        }
46
        unset($this->properties['options'], $this->properties['value']);
47
48
        // options is defined as key->value array?
49
        $optionsKey = $this->properties['optionsKey'] === true;
50
        unset($this->properties['optionsKey']);
51
        $build = null;
52
        // build output dom html
53
        foreach ($options as $idx => $value) {
54
            $property = $this->properties;
55 View Code Duplication
            if ($optionsKey === true) { // radio button as [value => text_description] - values is a key
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...
56
                $property['value'] = $idx;
57
                if ($idx == $this->value) {
58
                    $property['checked'] = null; // def boolean attribute html5
59
                }
60
            } else { // radio button only with [value] data
61
                $property['value'] = $value;
62
                if ($value == $this->value) {
63
                    $property['checked'] = null; // def boolean attribute html5
64
                }
65
            }
66
67
            // get template and concat avg response
68
            $build .= App::$View->render('native/form/radio_list', [
69
                'tag' => self::buildSingleTag('input', $property),
70
                'text' => $value,
71
            ]);
72
        }
73
74
        return $build;
75
    }
76
}