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 |
||
15 | class MapBlock extends BaseBlock { |
||
|
|||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | * @config |
||
20 | */ |
||
21 | private static $db = [ |
||
22 | 'Coordinates' => 'Varchar(255)', // center coordinates |
||
23 | 'ZoomLevel' => 'Int(3)', // zoom level of map |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | * @config |
||
29 | */ |
||
30 | private static $has_many = [ |
||
31 | 'Markers' => 'Marker', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | * @config |
||
37 | */ |
||
38 | private static $has_one = [ |
||
39 | 'GlobalMarker' => 'Image', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * Google Maps styles in string of JSON format. See |
||
44 | * docs/GOOGLE_MAPS_BLOCK.md how to use it. |
||
45 | * |
||
46 | * @var string|null |
||
47 | * @config |
||
48 | */ |
||
49 | private static $map_styles = null; |
||
50 | |||
51 | /** |
||
52 | * This will load javascript dependency to initialize |
||
53 | * user client map element. |
||
54 | * |
||
55 | * @var bool |
||
56 | * @config |
||
57 | */ |
||
58 | private static $load_javascript = true; |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function singular_name() { |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function plural_name() { |
||
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getCoordinatesAsOption() { |
||
90 | |||
91 | /** |
||
92 | * @return \FieldList |
||
93 | */ |
||
94 | public function getCMSFields() { |
||
130 | |||
131 | /** |
||
132 | * @return \HTMLText |
||
133 | */ |
||
134 | public function forTemplate() { |
||
141 | |||
142 | /** |
||
143 | * @return bool|string |
||
144 | */ |
||
145 | public function getMarkersAsJson() { |
||
173 | |||
174 | /** |
||
175 | * @return bool|string |
||
176 | */ |
||
177 | public function getOptionsAsJson() { |
||
218 | |||
219 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.