Conditions | 7 |
Paths | 5 |
Total Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
21 | public function index() |
||
22 | { |
||
23 | if (!is_null(\Illuminate\Support\Facades\Config::get('tracking.analytics.view_id')) && \Illuminate\Support\Facades\Config::get('tracking.conf.analytics') == 'google') { |
||
24 | $period = Period::days(7); |
||
25 | |||
26 | foreach (app(Analytics::class)->fetchVisitorsAndPageViews($period) as $view) { |
||
27 | $visitStats['date'][] = $view['date']->format('Y-m-d'); |
||
|
|||
28 | $visitStats['visitors'][] = $view['visitors']; |
||
29 | $visitStats['pageViews'][] = $view['pageViews']; |
||
30 | } |
||
31 | |||
32 | return view('tracking::analytics.google', compact('visitStats', 'period')); |
||
33 | } elseif (is_null(\Illuminate\Support\Facades\Config::get('tracking.analytics.conf')) || \Illuminate\Support\Facades\Config::get('tracking.conf.analytics') == 'internal') { |
||
34 | if (Schema::hasTable('analytics')) { |
||
35 | return view('tracking::analytics.internal') |
||
36 | ->with('stats', $this->service->getDays(15)) |
||
37 | ->with('topReferers', $this->service->topReferers(15)) |
||
38 | ->with('topBrowsers', $this->service->topBrowsers(15)) |
||
39 | ->with('topPages', $this->service->topPages(15)); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | return view('tracking::analytics.empty'); |
||
44 | } |
||
45 | } |
||
46 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.