Conditions | 14 |
Paths | 672 |
Total Lines | 183 |
Code Lines | 125 |
Lines | 23 |
Ratio | 12.57 % |
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 |
||
17 | public function index() |
||
18 | { |
||
19 | // Visitors |
||
20 | $startDate = Carbon::now()->subDays(7); |
||
21 | $endDate = Carbon::now()->subDay(); |
||
22 | $visitorsData = AnalyticsFacade::performQuery( |
||
23 | Period::create($startDate, $endDate), |
||
24 | 'ga:sessions,ga:pageviews', |
||
25 | [ |
||
26 | 'dimensions' => 'ga:date', |
||
27 | 'sort' => 'ga:date' |
||
28 | ] |
||
29 | ); |
||
30 | |||
31 | $data = []; |
||
32 | foreach ($visitorsData->rows as $row) { |
||
33 | $row[0] = Carbon::createFromFormat('Ymd', $row[0]); |
||
34 | |||
35 | array_push($data, $row); |
||
36 | } |
||
37 | $visitorsData->rows = array_reverse($data); |
||
38 | |||
39 | // Browsers |
||
40 | $startDate = Carbon::create(2014, 07, 01); |
||
41 | $browsersData = AnalyticsFacade::performQuery( |
||
42 | Period::create($startDate, $endDate), |
||
43 | 'ga:pageviews', |
||
44 | [ |
||
45 | 'dimensions' => 'ga:browser', |
||
46 | 'sort' => 'ga:pageviews', |
||
47 | 'filters' => 'ga:browser==Chrome,' |
||
48 | .'ga:browser==Firefox,' |
||
49 | .'ga:browser==Internet Explorer,' |
||
50 | .'ga:browser==Safari,' |
||
51 | .'ga:browser==Opera' |
||
52 | ] |
||
53 | ); |
||
54 | |||
55 | $data = []; |
||
56 | foreach ($browsersData->rows as $browser) { |
||
57 | $percent = round(($browser[1] * 100) / $browsersData->totalsForAllResults['ga:pageviews'], 1); |
||
58 | |||
59 | switch ($browser[0]) { |
||
60 | case 'Chrome': |
||
61 | $color = '#00acac'; |
||
62 | break; |
||
63 | case 'Firefox': |
||
64 | $color = '#f4645f'; |
||
65 | break; |
||
66 | case 'Safari': |
||
67 | $color = '#727cb6'; |
||
68 | break; |
||
69 | case 'Opera': |
||
70 | $color = '#348fe2'; |
||
71 | break; |
||
72 | case 'Internet Explorer': |
||
73 | $color = '#75e376'; |
||
74 | break; |
||
75 | default: |
||
76 | $color = '#ddd'; |
||
77 | } |
||
78 | |||
79 | $browser[] = $percent; |
||
80 | $browser[] = $color; |
||
81 | |||
82 | array_push($data, $browser); |
||
83 | } |
||
84 | $browsersData->rows = array_reverse($data); |
||
85 | |||
86 | // Countries |
||
87 | $countriesData = AnalyticsFacade::performQuery( |
||
88 | Period::create($startDate, $endDate), |
||
89 | 'ga:pageviews,ga:sessions', |
||
90 | [ |
||
91 | 'dimensions' => 'ga:countryIsoCode', |
||
92 | 'sort' => '-ga:pageviews' |
||
93 | ] |
||
94 | ); |
||
95 | |||
96 | foreach ($countriesData->rows as $country) { |
||
97 | $countries[$country[0]] = [ |
||
98 | 'ga:pageviews' => $country[1], |
||
99 | 'ga:sessions' => $country[2], |
||
100 | 'percent' => round(($country[1] * 100) / $countriesData->totalsForAllResults['ga:pageviews'], 1) |
||
101 | ]; |
||
102 | |||
103 | if (!in_array($country[0], ['ZZ'])) { |
||
104 | $countryInstance = country((string)strtolower($country[0])); |
||
105 | $countries[$country[0]] += [ |
||
106 | 'countryName' => $countryInstance->getName() |
||
107 | ]; |
||
108 | } else { |
||
109 | $countries[$country[0]] += [ |
||
110 | 'countryName' => 'Unknown' |
||
111 | ]; |
||
112 | } |
||
113 | } |
||
114 | $countriesData->rows = $countries; |
||
115 | $topCountries = array_slice($countries, 0, 3); |
||
116 | |||
117 | // Devices |
||
118 | $startDate = Carbon::now()->subMonths(7); |
||
119 | $devicesData = AnalyticsFacade::performQuery( |
||
120 | Period::create($startDate, $endDate), |
||
121 | 'ga:pageviews', |
||
122 | [ |
||
123 | 'dimensions' => 'ga:mobileDeviceBranding,ga:yearMonth', |
||
124 | 'sort' => '-ga:mobileDeviceBranding', |
||
125 | 'filters' => 'ga:mobileDeviceBranding==Apple,' |
||
126 | .'ga:mobileDeviceBranding==Samsung,' |
||
127 | .'ga:mobileDeviceBranding==Google,' |
||
128 | .'ga:mobileDeviceBranding==HTC,' |
||
129 | .'ga:mobileDeviceBranding==Microsoft' |
||
130 | ] |
||
131 | ); |
||
132 | |||
133 | $devicesGraph = []; |
||
134 | View Code Duplication | for ($i = 0; $i < 8; $i++) { |
|
135 | $date = Carbon::now()->subMonths($i); |
||
136 | |||
137 | $devicesGraph[$date->format('Ym')] = [ |
||
138 | 'period' => $date->format('Y-m-d'), |
||
139 | 'Apple' => 0, |
||
140 | 'Samsung' => 0, |
||
141 | 'Google' => 0, |
||
142 | 'HTC' => 0, |
||
143 | 'Microsoft' => 0 |
||
144 | ]; |
||
145 | } |
||
146 | |||
147 | foreach ($devicesData->rows as $device) { |
||
148 | $devicesGraph[$device[1]][$device[0]] = $device[2]; |
||
149 | } |
||
150 | $devicesGraph = array_reverse($devicesGraph); |
||
151 | $devicesGraph = collect($devicesGraph)->toJson(); |
||
152 | |||
153 | // Operating System |
||
154 | $startDate = Carbon::now()->subMonths(7); |
||
155 | $operatingSystemData = AnalyticsFacade::performQuery( |
||
156 | Period::create($startDate, $endDate), |
||
157 | 'ga:pageviews', |
||
158 | [ |
||
159 | 'dimensions' => 'ga:operatingSystem,ga:yearMonth', |
||
160 | 'sort' => '-ga:operatingSystem', |
||
161 | 'filters' => 'ga:operatingSystem==Windows,' |
||
162 | .'ga:operatingSystem==Macintosh,' |
||
163 | .'ga:operatingSystem==Linux,' |
||
164 | .'ga:operatingSystem==(not set)' |
||
165 | ] |
||
166 | ); |
||
167 | |||
168 | $operatingSystemGraph = []; |
||
169 | View Code Duplication | for ($i = 0; $i < 8; $i++) { |
|
170 | $date = Carbon::now()->subMonths($i); |
||
171 | |||
172 | $operatingSystemGraph[$date->format('Ym')] = [ |
||
173 | 'period' => $date->format('Y-m-d'), |
||
174 | 'Windows' => 0, |
||
175 | 'Macintosh' => 0, |
||
176 | 'Linux' => 0, |
||
177 | '(not set)' => 0 |
||
178 | ]; |
||
179 | } |
||
180 | |||
181 | foreach ($operatingSystemData->rows as $os) { |
||
182 | $operatingSystemGraph[$os[1]][$os[0]] = $os[2]; |
||
183 | } |
||
184 | $operatingSystemGraph = array_reverse($operatingSystemGraph); |
||
185 | $operatingSystemGraph = collect($operatingSystemGraph)->toJson(); |
||
186 | |||
187 | return view( |
||
188 | 'Admin::page.index', |
||
189 | [ |
||
190 | 'breadcrumbs' => $this->breadcrumbs, |
||
191 | 'visitorsData' => $visitorsData, |
||
192 | 'browsersData' => $browsersData, |
||
193 | 'countriesData' => $countriesData, |
||
194 | 'topCountries' => $topCountries, |
||
195 | 'devicesGraph' => $devicesGraph, |
||
196 | 'operatingSystemGraph' => $operatingSystemGraph |
||
197 | ] |
||
198 | ); |
||
199 | } |
||
200 | } |
||
201 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.