Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/Analytics/ChartDataCommandHelper.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Kunstmaan\DashboardBundle\Command\Helper\Analytics;
3
4
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
6
7
class ChartDataCommandHelper extends AbstractAnalyticsCommandHelper
8
{
9
    /**
10
     * get extra data
11
     *
12
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
13
     */
14
    protected function getExtra(AnalyticsOverview $overview) {
15
        $timespan = $overview->getTimespan() - $overview->getStartOffset();
16
        $extra = parent::getExtra($overview);
17
18
        if ($timespan <= 1) {
19
            $extra['dimensions'] = 'ga:date,ga:hour';
20
        } else if ($timespan <= 7) {
21
            $extra['dimensions'] = 'ga:date,ga:hour';
22
        } else if ($timespan <= 31) {
23
            $extra['dimensions'] = 'ga:week,ga:day,ga:date';
24
        } else {
25
            $extra['dimensions'] = 'ga:isoYearIsoWeek';
26
        }
27
        return $extra;
28
    }
29
30
    /**
31
     * get data and save it for the overview
32
     *
33
     * @param AnalyticsOverview $overview The overview
34
     */
35
    public function getData(&$overview)
36
    {
37
        $this->output->writeln("\t" . 'Fetching chart data..');
38
39
        // execute the query
40
        $metrics = 'ga:sessions, ga:users, ga:newUsers, ga:pageviews';
41
        $rows = $this->executeQuery($overview, $metrics);
42
43
        $chartData = array();
44
        $chartDataMaxValue = 0;
45
        $timespan = $overview->getTimespan() - $overview->getStartOffset();
46
        foreach ($rows as $row) {
47
            // metrics
48
            $sessions = $row[count($row) - 4];
49
            $users = $row[count($row) - 3];
50
            $newusers = $row[count($row) - 2];
51
            $pageviews = $row[count($row) - 1];
52
53
            $maxvalue = max($sessions, $users, $newusers, $pageviews);
54
            // set max chartdata value
55
            if ($chartDataMaxValue < $maxvalue) {
56
                $chartDataMaxValue = $maxvalue;
57
            }
58
59
            // timestamp
60 View Code Duplication
            if ($timespan <= 1) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
                $timestamp = mktime($row[1], 0, 0, substr($row[0], 4, 2), substr($row[0], 6, 2), substr($row[0], 0, 4));
62
                $timestamp = date('Y-m-d H:00', $timestamp);
63
            } else if ($timespan <= 7) {
64
                $timestamp = mktime($row[1], 0, 0, substr($row[0], 4, 2), substr($row[0], 6, 2), substr($row[0], 0, 4));
65
                $timestamp = date('Y-m-d H:00', $timestamp);
66
            } else if ($timespan <= 31) {
67
                $timestamp = mktime(0, 0, 0, substr($row[2], 4, 2), substr($row[2], 6, 2), substr($row[2], 0, 4));
68
                $timestamp = date('Y-m-d H:00', $timestamp);
69
            } else {
70
                $timestamp = strtotime(substr($row[0], 0, 4) . 'W' . substr($row[0], 4, 2));
71
                $timestamp = date('Y-m-d H:00', $timestamp);
72
            }
73
74
            // add to chart array
75
            $chartEntry = array(
76
                'timestamp' => $timestamp,
77
                'sessions' => $sessions,
78
                'users' => $users,
79
                'newusers' => $newusers,
80
                'pageviews' => $pageviews
81
82
            );
83
            $chartData[] = $chartEntry;
84
        }
85
86
        // adding data to the overview
87
        $overview->setChartDataMaxValue($chartDataMaxValue);
88
        $overview->setChartData(json_encode($chartData, JSON_NUMERIC_CHECK));
0 ignored issues
show
json_encode($chartData, JSON_NUMERIC_CHECK) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
    }
90
91
}
92