Location::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 10
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace FlatPlan\Components;
2
3
class Location extends AbstractComponent {
4
5
    protected $mapType;
6
    protected $caption;
7
    protected $accessibilityCaption;
8
    protected $latitude;
9
    protected $longitude;
10
    protected $items;
11
    protected $span;
12
13
    protected $roles    = ['map', 'place'];
14
    protected $mapTypes = ['standard', 'hybrid', 'satellite'];
15
16
    /**
17
     * @param string $role
18
     * @param string $mapType
19
     * @param string $caption
20
     * @param string $accessibilityCaption
21
     * @param string $latitude
22
     * @param string $longitude
23
     * @param array $items
24
     * @param array $span
25
     * @return void
26
     */
27
    public function __construct($role, $mapType = 'standard', $caption = '', $accessibilityCaption = '', $latitude = null, $longitude = null, $items = array(), $span = array())
28
    {
29
        $this->setRole($role);
30
        $this->setMapType($mapType);
31
        $this->setCaption($caption);
32
        $this->setAccessibilityCaption($accessibilityCaption);
33
        $this->setLatitude($latitude);
34
        $this->setLongitude($longitude);
35
        $this->setItems($items);
36
        $this->setSpan($span);
37
    }
38
39
    private function setMapType($mapType)
40
    {
41
        if (!in_array($mapType, $this->mapTypes)) {
42
            throw new \ErrorException('Invalid map type supplied.');
43
        }
44
        $this->mapType = $mapType;
45
    }
46
47
    private function getMapType()
48
    {
49
        return $this->mapType;
50
    }
51
52
    private function setCaption($caption)
53
    {
54
        $this->caption = $caption;
55
    }
56
57
    private function getCaption()
58
    {
59
        return $this->caption;
60
    }
61
62
    private function setAccessibilityCaption($accessibilityCaption)
63
    {
64
        $this->accessibilityCaption = $accessibilityCaption;
65
    }
66
67
    private function getAccessibilityCaption()
68
    {
69
        return $this->accessibilityCaption;
70
    }
71
72
    private function setLatitude($latitude)
73
    {
74
        $this->latitude = (float) $latitude;
75
    }
76
77
    private function getLatitude()
78
    {
79
        return $this->latitude;
80
    }
81
82
    private function setLongitude($longitude)
83
    {
84
        $this->longitude = (float) $longitude;
85
    }
86
87
    private function getLongitude()
88
    {
89
        return $this->longitude;
90
    }
91
92
    private function setItems($items = array())
93
    {
94
        if (is_array($items) && !empty($items)) {
95
            $pins = array();
96
            foreach ($items as $item) {
97
                $pin = new \stdClass();
98
                $pin->latitude  = (float) $item['latitude'];
99
                $pin->longitude = (float) $item['longitude'];
100
                if (isset($item['caption'])) {
101
                    $pin->caption = $item['caption'];
102
                }
103
                array_push($pins, $pin);
104
            }
105
            $this->items = $pins;
106
        }
107
    }
108
109
    private function getItems()
110
    {
111
        return $this->items;
112
    }
113
114
    private function setSpan($span = array())
115
    {
116
        if (is_array($span) && count($span) === 2) {
117
            $mapSpan = new \stdClass();
118
            $mapSpan->latitudeDelta = (float) $span['latitudeDelta'];
119
            $mapSpan->longitudeDelta = (float) $span['longitudeDelta'];
120
            $this->span = $mapSpan;
121
        }
122
    }
123
124
    private function getSpan()
125
    {
126
        return $this->span;
127
    }
128
129
    public function getComponent()
130
    {
131
        $component = new \stdClass();
132
        $component->role                 = $this->getRole();
133
        $component->mapType              = $this->getMapType();
134
        $component->caption              = $this->getCaption();
135
        $component->accessibilityCaption = $this->getAccessibilityCaption();
136
        $component->latitude             = $this->getLatitude();
137
        $component->longitude            = $this->getLongitude();
138
        $component->items                = $this->getItems();
139
        $component->span                 = $this->getSpan();
140
        $component->layout               = $this->getLayout();
141
        $component->style                = $this->getStyle();
142
        if (!is_null($this->behaviour)) {
143
            $component->behaviour = $this->getBehaviour();
144
        }
145
        return $component;
146
    }
147
}
148