StatisticsController::index()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 27
rs 8.8571
1
<?php
2
3
namespace App\Http\Controllers\Back;
4
5
use Analytics;
6
use Spatie\Analytics\Period;
7
8
class StatisticsController
9
{
10
    public function index()
11
    {
12
        if (empty(config('laravel-analytics.view_id'))) {
13
            return view('back.statistics.notconfigured');
14
        }
15
16
        $visitors = Analytics::fetchTotalVisitorsAndPageViews(Period::days(365))
17
            ->groupBy(function (array $visitorStatistics) {
18
                return $visitorStatistics['date']->format('Y-m');
19
            })
20
            ->map(function ($visitorStatistics, $yearMonth) {
21
                list($year, $month) = explode('-', $yearMonth);
22
23
                return [
24
                    'date' => "{$month}-{$year}",
25
                    'visitors' => $visitorStatistics->sum('visitors'),
26
                    'pageViews' => $visitorStatistics->sum('pageViews'),
27
                ];
28
            })
29
            ->values();
30
31
        $pages = Analytics::fetchMostVisitedPages(Period::days(365));
32
        $referrers = Analytics::fetchTopReferrers(Period::days(365));
33
        $browsers = Analytics::fetchTopBrowsers(Period::days(365));
34
35
        return view('back.statistics.index')->with(compact('visitors', 'pages', 'referrers', 'browsers'));
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
    }
37
}
38