Completed
Push — master ( 07e3a8...9561cd )
by William
16:58 queued 14:23
created

ReportsHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 84.91%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 127
ccs 45
cts 53
cp 0.8491
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A linkToReport() 0 5 1
A getLineChartData() 0 9 2
A createReportsLinks() 0 8 2
A linkToReportFromIncident() 0 5 1
A entriesFromIncidents() 0 15 3
A getChartArray() 0 18 3
A getStacktracesForIncidents() 0 19 3
1
<?php
2
3
namespace App\View\Helper;
4
5
use Cake\Utility\Inflector;
6
use const ENT_HTML5;
7
use const ENT_QUOTES;
8
use function count;
9
use function htmlspecialchars;
10
use function implode;
11
12
class ReportsHelper extends AppHelper
13
{
14
    /** @var string */
15
    public $helpers = ['Incidents'];
16
17
    /**
18
     * @param mixed  $entries    Entries
19
     * @param int    $totalCount Total count
20
     * @param string $key        Key
21
     * @return string HTML
22
     */
23 8
    public function entriesFromIncidents($entries, int $totalCount, string $key): string
24
    {
25
        //$entries = Sanitize::clean($entries);
26 8
        $values = [];
27 8
        foreach ($entries as $entry) {
28 8
            $values[] = $entry . '[' . $key . '] <span class="count">('
29 8
                . $entry['count'] . ')</span>';
30
        }
31 8
        $fullString = implode(', ', $values);
32 8
        $remaining = $totalCount - count($values);
33 8
        if ($remaining) {
34
            $fullString .= ' <small>and ' . $remaining . ' others</small>';
35
        }
36
37 8
        return $fullString;
38
    }
39
40
    /**
41
     * @param mixed $reports The reports
42
     * @return string comma separated list
43
     */
44 4
    public function createReportsLinks($reports): string
45
    {
46 4
        $links = [];
47 4
        foreach ($reports as $report) {
48 4
            $links[] = $this->linkToReport($report);
49
        }
50
51 4
        return implode(', ', $links);
52
    }
53
54
    /**
55
     * @param mixed $report The report
56
     * @return string HTML <a> link
57
     */
58 12
    public function linkToReport($report): string
59
    {
60 12
        $reportId = $report['id'];
61
62 12
        return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>';
63
    }
64
65
    /**
66
     * @param mixed $incident The incident
67
     * @return string HTML <a> link
68
     */
69 4
    public function linkToReportFromIncident($incident): string
70
    {
71 4
        $reportId = $incident['report_id'];
72
73 4
        return '<a href="/' . BASE_DIR . 'reports/view/' . $reportId . '">#' . $reportId . '</a>';
74
    }
75
76
    /**
77
     * @param mixed $incidents The incidents
78
     * @return string HTML
79
     */
80 4
    public function getStacktracesForIncidents($incidents): string
81
    {
82 4
        $count = 0;
83 4
        $html = '<div class="row">';
84 4
        foreach ($incidents as $incident) {
85 4
            $class = 'well span5';
86
87 4
            if ($count % 2 === 1) {
88
                $class .= ' ';
89
            } else {
90 4
                $html .= '</div><div class="row">';
91
            }
92
93 4
            $html .= $this->Incidents->getStacktrace($incident, $class);
0 ignored issues
show
Bug Best Practice introduced by
The property Incidents does not exist on App\View\Helper\ReportsHelper. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getStacktrace() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            $html .= $this->Incidents->/** @scrutinizer ignore-call */ getStacktrace($incident, $class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
Are you sure the usage of $this->Incidents->getSta...race($incident, $class) targeting Cake\View\Helper::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94 4
            ++$count;
95
        }
96 4
        $html .= '</div>';
97
98 4
        return $html;
99
    }
100
101
    /**
102
     * @param mixed $columns
103
     * @param mixed $relatedEntries
104
     * @return string HTML code
105
     */
106 4
    public function getChartArray(string $arrayName, $columns, $relatedEntries): string
107
    {
108 4
        $html = 'var ' . $arrayName . ' = [], chart = {};';
109 4
        foreach ($columns as $column) {
110 4
            $column = htmlspecialchars($column, ENT_QUOTES | ENT_HTML5);
111 4
            $html .= 'chart = {};';
112 4
            $html .= 'chart.name = "' . $column . '";';
113 4
            $html .= 'chart.title = "' . Inflector::humanize($column) . '";';
114 4
            $html .= 'chart.labels = []; chart.values = [];';
115 4
            foreach ($relatedEntries[$column] as $entry) {
116 4
                $count = $entry['count'];
117 4
                $html .= 'chart.labels.push("' . $entry[$column] . ' (' . $count . ')");';
118 4
                $html .= 'chart.values.push(' . $count . ');';
119
            }
120 4
            $html .= $arrayName . '.push(chart);';
121
        }
122
123 4
        return $html;
124
    }
125
126
    /**
127
     * @param mixed $entries
128
     * @return string HTML
129
     */
130
    public function getLineChartData(string $arrayName, $entries): string
131
    {
132
        $html = 'var $arrayName = [];';
133
        foreach ($entries as $entry) {
134
            $html .= $arrayName . '.push(["' . $entry['date'] . '", '
135
                    . $entry['count'] . ']);';
136
        }
137
138
        return $html;
139
    }
140
}
141