BooleanField   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 26.56 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 17
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 1
A inflate() 0 3 1
A render() 0 17 2
A headElements() 17 17 1

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
namespace rtens\domin\delivery\web\fields;
3
4
use rtens\domin\delivery\web\HeadElements;
5
use rtens\domin\Parameter;
6
use rtens\domin\delivery\web\Element;
7
use rtens\domin\delivery\web\WebField;
8
use watoki\reflect\type\BooleanType;
9
10
class BooleanField implements WebField {
11
12
    /**
13
     * @param Parameter $parameter
14
     * @return bool
15
     */
16
    public function handles(Parameter $parameter) {
17
        return $parameter->getType() == new BooleanType();
18
    }
19
20
    /**
21
     * @param Parameter $parameter
22
     * @param string $serialized
23
     * @return mixed
24
     */
25
    public function inflate(Parameter $parameter, $serialized) {
26
        return !!$serialized;
27
    }
28
29
    /**
30
     * @param Parameter $parameter
31
     * @param mixed $value
32
     * @return string
33
     */
34
    public function render(Parameter $parameter, $value) {
35
        return implode('', [
36
            new Element('input', [
37
                'type' => 'hidden',
38
                'name' => $parameter->getName(),
39
                'value' => 0
40
            ]),
41
            new Element('input', array_merge([
42
                'class' => 'boolean-switch',
43
                'type' => 'checkbox',
44
                'name' => $parameter->getName(),
45
                'value' => 1,
46
            ], $value ? [
47
                'checked' => 'checked'
48
            ] : []))
49
        ]);
50
    }
51
52
    /**
53
     * @param Parameter $parameter
54
     * @return array|Element[]
55
     */
56 View Code Duplication
    public function headElements(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
57
        return [
58
            HeadElements::bootstrap(),
59
            HeadElements::jquery(),
60
            HeadElements::style('//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css'),
61
            HeadElements::script('//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js'),
62
            new Element('script', [], ["
63
                $(function () {
64
                    $('.boolean-switch').bootstrapSwitch({
65
                        size: 'small',
66
                        onColor: 'success',
67
                        onText: 'Yes',
68
                        offText: 'No'
69
                    });
70
                });"])
71
        ];
72
    }
73
}