AnalyticsController::index()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6026
c 0
b 0
f 0
cc 7
nc 5
nop 0
1
<?php
2
3
namespace Tracking\Http\Controllers;
4
5
use Tracking\Services\AnalyticsService;
6
use Illuminate\Support\Facades\Schema;
7
use Spatie\Analytics\Analytics;
8
use Spatie\Analytics\Period;
9
10
class AnalyticsController extends Controller
11
{
12
    protected $service;
13
14
    public function __construct(AnalyticsService $service)
15
    {
16
        parent::__construct();
17
18
        $this->service = $service;
19
    }
20
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');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$visitStats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $visitStats = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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