DashboardController::index()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Back;
4
5
use Analytics;
6
use Spatie\Analytics\Period;
7
use Illuminate\Support\Collection;
8
use Spatie\Activitylog\Models\Activity;
9
10
class DashboardController
11
{
12
    public function index()
13
    {
14
        $logItems = $this->getLatestActivityItems();
15
16
        $view = view('back.dashboard.index')->with(compact('logItems'));
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...
17
18
        if (empty(config('laravel-analytics.view_id'))) {
19
            return $view;
20
        }
21
22
        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(14));
23
24
        $dates = $analyticsData->pluck('date');
25
        $visitors = $analyticsData->pluck('visitors');
26
        $pageViews = $analyticsData->pluck('pageViews');
27
28
        return $view->with(compact('dates', 'visitors', 'pageViews'));
29
    }
30
31
    protected function getLatestActivityItems(): Collection
32
    {
33
        return Activity::with('causer')
34
            ->latest()
35
            ->limit(30)
36
            ->get();
37
    }
38
}
39