Passed
Push — master ( 3bf788...763f00 )
by Jon
02:39 queued 10s
created

Location::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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