Passed
Push — main ( be932e...bfe281 )
by Dimitri
04:17
created

BaseCollector::timelineData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Debug\Toolbar\Collectors;
13
14
/**
15
 * Collecteur de la barre d'outils de base
16
 *
17
 * @credit	<a href="https://codeigniter.com">CodeIgniter 4.2 - CodeIgniter\Debug\Toolbar\Collectors\BaseCollector</a>
18
 */
19
abstract class BaseCollector
20
{
21
    /**
22
     * Si ce collecteur possède des données pouvant
23
     * être affichées dans la chronologie.
24
     */
25
    protected bool $hasTimeline = false;
26
27
    /**
28
     * Indique si ce collecteur doit afficher
29
     * du contenu dans un onglet ou non.
30
     */
31
    protected bool $hasTabContent = false;
32
33
    /**
34
     * Si ce collecteur doit afficher
35
     * une étiquette ou non.
36
     */
37
    protected bool $hasLabel = false;
38
39
    /**
40
     * Indique si ce collecteur contient des données qui
41
     * doivent être affichées dans l'onglet Vars.
42
     */
43
    protected bool $hasVarData = false;
44
45
    /**
46
     * Le 'titre' de ce Collector.
47
     * Utilisé pour nommer les choses dans la barre d'outils HTML.
48
     */
49
    protected string $title = '';
50
 
51
    /**
52
     * La 'cle' de ce Collector.
53
     * Utilisé comme id.
54
     */
55
    protected string $key = '';
56
57
    /**
58
     * Obtient le titre du collecteur
59
     */
60
    public function getTitle(bool $safe = false): string
61
    {
62
        if ($safe) {
63
            return str_replace(' ', '-', strtolower($this->title));
64
        }
65
66
        return $this->title;
67
    }
68
69
    /**
70
     * Obtient la cle du collecteur
71
     */
72
    public function getKey(): string
73
    {
74
        if (empty($this->key)) {
75
            $this->key = str_replace('Collector', '', basename(static::class));
76
        }
77
78
        return str_replace(' ', '-', strtolower($this->key));
79
    }
80
81
    /**
82
     * Renvoie toute information devant être affichée à côté du titre.
83
     */
84
    public function getTitleDetails(): string
85
    {
86
        return '';
87
    }
88
89
    /**
90
     * Ce collecteur a-t-il besoin de son propre onglet ?
91
     */
92
    public function hasTabContent(): bool
93
    {
94
        return $this->hasTabContent;
95
    }
96
97
    /**
98
     * Ce collecteur a-t-il une étiquette ?
99
     */
100
    public function hasLabel(): bool
101
    {
102
        return $this->hasLabel;
103
    }
104
105
    /**
106
     * Ce collecteur a-t-il des informations pour la chronologie ?
107
     */
108
    public function hasTimelineData(): bool
109
    {
110
        return $this->hasTimeline;
111
    }
112
113
    /**
114
     * Saisit les données pour la chronologie, correctement formatées,
115
     * ou renvoie un tableau vide.
116
     */
117
    public function timelineData(): array
118
    {
119
        if (! $this->hasTimeline) {
120
            return [];
121
        }
122
123
        return $this->formatTimelineData();
124
    }
125
126
    /**
127
     * Ce collecteur contient-il des données
128
     * qui doivent être affichées dans l'onglet "Vars" ?
129
     */
130
    public function hasVarData(): bool
131
    {
132
        return $this->hasVarData;
133
    }
134
135
    /**
136
     * Obtient une collection de données qui doivent être affichées dans l'onglet "Vars".
137
     * Le format est un tableau de sections, chacune avec son propre tableau de paires clé/valeur :
138
     *
139
     *  $data = [
140
     *      'section 1' => [
141
     *          'foo' => 'bar,
142
     *          'bar' => 'baz'
143
     *      ],
144
     *      'section 2' => [
145
     *          'foo' => 'bar,
146
     *          'bar' => 'baz'
147
     *      ],
148
     *  ];
149
     */
150
    public function getVarData()
151
    {
152
        return null;
153
    }
154
155
    /**
156
     * Les classes enfants doivent l'implémenter
157
     * pour renvoyer les données de la chronologie formatées pour une utilisation correcte.
158
     *
159
     * Les données de la chronologie doivent être formatées dans des tableaux qui ressemblent à :
160
     *
161
     *  [
162
     *      'name'      => 'Database::Query',
163
     *      'component' => 'Database',
164
     *      'start'     => 10       // milliseconds
165
     *      'duration'  => 15       // milliseconds
166
     *  ]
167
     */
168
    protected function formatTimelineData(): array
169
    {
170
        return [];
171
    }
172
173
    /**
174
     * Renvoie les données de ce collecteur à formater dans la barre d'outils
175
     *
176
     * @return array|string
177
     */
178
    public function display()
179
    {
180
        return [];
181
    }
182
183
    /**
184
     * Obtient la valeur "badge" pour le bouton.
185
     */
186
    public function getBadgeValue()
187
    {
188
        return null;
189
    }
190
191
    /**
192
     * Ce collecteur a-t-il collecté des données ?
193
     *
194
     * Si ce n'est pas le cas, le bouton de la barre d'outils ne s'affichera pas.
195
     */
196
    public function isEmpty(): bool
197
    {
198
        return false;
199
    }
200
201
    /**
202
     * Renvoie le code HTML pour afficher l'icône. Devrait soit
203
     * être SVG, ou encodé en base 64.
204
     *
205
     * Les dimensions recommandées sont 24px x 24px
206
     */
207
    public function icon(): string
208
    {
209
        return '';
210
    }
211
212
    /**
213
     * Renvoie les paramètres sous forme de tableau.
214
     */
215
    public function getAsArray(): array
216
    {
217
        return [
218
            'title'           => $this->getTitle(),
219
            'titleSafe'       => $this->getTitle(true),
220
            'key'             => $this->getKey(),
221
            'titleDetails'    => $this->getTitleDetails(),
222
            'display'         => $this->display(),
223
            'badgeValue'      => $this->getBadgeValue(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getBadgeValue() targeting BlitzPHP\Debug\Toolbar\C...lector::getBadgeValue() 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...
224
            'isEmpty'         => $this->isEmpty(),
225
            'hasTabContent'   => $this->hasTabContent(),
226
            'hasLabel'        => $this->hasLabel(),
227
            'icon'            => $this->icon(),
228
            'hasTimelineData' => $this->hasTimelineData(),
229
            'timelineData'    => $this->timelineData(),
230
        ];
231
    }
232
}
233