DashboardController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 18 2
A getLatestActivityItems() 0 7 1
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