Conditions | 14 |
Paths | 2 |
Total Lines | 110 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function safeUp() |
||
20 | { |
||
21 | $widgetResults = (new Query()) |
||
22 | ->select('*') |
||
23 | ->from(['{{%widgets}}']) |
||
24 | ->where(['type' => 'Analytics_Reports']) |
||
25 | ->all(); |
||
26 | |||
27 | if (!empty($widgetResults)) { |
||
28 | |||
29 | foreach ($widgetResults as $result) { |
||
30 | $settings = Json::decode($result['settings']); |
||
31 | $newSettings = []; |
||
32 | |||
33 | if (!empty($settings['type'])) { |
||
34 | switch ($settings['type']) { |
||
35 | case 'visits': |
||
36 | $newSettings = [ |
||
37 | 'menu' => 'audienceOverview', |
||
38 | 'dimension' => '', |
||
39 | 'metric' => 'ga:sessions', |
||
40 | 'chart' => 'area', |
||
41 | 'period' => 'month', |
||
42 | ]; |
||
43 | break; |
||
44 | |||
45 | case 'geo': |
||
46 | $newSettings = [ |
||
47 | 'menu' => 'location', |
||
48 | 'dimension' => 'ga:country', |
||
49 | 'metric' => 'ga:pageviewsPerSession', |
||
50 | 'chart' => 'geo', |
||
51 | 'period' => 'month', |
||
52 | ]; |
||
53 | break; |
||
54 | |||
55 | case 'mobile': |
||
56 | $newSettings = [ |
||
57 | 'menu' => 'mobile', |
||
58 | 'dimension' => 'ga:deviceCategory', |
||
59 | 'metric' => 'ga:sessions', |
||
60 | 'chart' => 'pie', |
||
61 | 'period' => 'week', |
||
62 | ]; |
||
63 | break; |
||
64 | |||
65 | case 'pages': |
||
66 | $newSettings = [ |
||
67 | 'menu' => 'allPages', |
||
68 | 'dimension' => 'ga:pagePath', |
||
69 | 'metric' => 'ga:pageviews', |
||
70 | 'chart' => 'table', |
||
71 | 'period' => 'week', |
||
72 | ]; |
||
73 | break; |
||
74 | |||
75 | case 'acquisition': |
||
76 | $newSettings = [ |
||
77 | 'menu' => 'allChannels', |
||
78 | 'dimension' => 'ga:channelGrouping', |
||
79 | 'metric' => 'ga:sessions', |
||
80 | 'chart' => 'table', |
||
81 | 'period' => 'week', |
||
82 | ]; |
||
83 | break; |
||
84 | |||
85 | case 'technology': |
||
86 | $newSettings = [ |
||
87 | 'menu' => 'browserOs', |
||
88 | 'dimension' => 'ga:browser', |
||
89 | 'metric' => 'ga:sessions', |
||
90 | 'chart' => 'pie', |
||
91 | 'period' => 'week', |
||
92 | ]; |
||
93 | break; |
||
94 | |||
95 | case 'conversions': |
||
96 | $newSettings = [ |
||
97 | 'menu' => 'goals', |
||
98 | 'dimension' => 'ga:goalCompletionLocation', |
||
99 | 'metric' => 'ga:goalCompletionsAll', |
||
100 | 'chart' => 'area', |
||
101 | 'period' => 'week', |
||
102 | ]; |
||
103 | break; |
||
104 | |||
105 | case 'counts': |
||
106 | case 'custom': |
||
107 | case 'realtime': |
||
108 | $newSettings = [ |
||
109 | 'menu' => 'audienceOverview', |
||
110 | 'dimension' => '', |
||
111 | 'metric' => 'ga:sessions', |
||
112 | 'chart' => 'area', |
||
113 | 'period' => 'month', |
||
114 | ]; |
||
115 | break; |
||
116 | } |
||
117 | |||
118 | |||
119 | // Update rows |
||
120 | |||
121 | $newSettings = Json::encode($newSettings); |
||
122 | |||
123 | $this->update('{{%widgets}}', ['type' => 'Analytics_Explorer', 'settings' => $newSettings], ['id' => $result['id']]); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return true; |
||
129 | } |
||
141 |