HtmlVarWriter   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Test Coverage

Coverage 85.6%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 102
dl 0
loc 294
ccs 107
cts 125
cp 0.856
rs 10
c 1
b 0
f 0
wmc 30

18 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 3 1
A writeArrayOpen() 0 12 2
A writeObjectReference() 0 15 1
A writeObjectClose() 0 7 2
A stop() 0 3 1
A writeString() 0 6 2
A __construct() 0 3 1
A writeResource() 0 3 1
A writeBool() 0 3 2
A writeScalar() 0 18 2
B writeName() 0 41 6
A writeInt() 0 3 1
A writeArrayReference() 0 15 1
A writeObjectOpen() 0 12 2
A writeUninitialized() 0 3 1
A writeArrayClose() 0 7 2
A writeNull() 0 3 1
A writeFloat() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\ErrorLogger;
5
6
use Plaisio\Debug\VarWriter;
7
use Plaisio\Helper\Html;
8
9
/**
10
 * Writes a var dump in HTML to a stream.
11
 */
12
class HtmlVarWriter implements VarWriter
13
{
14
  //--------------------------------------------------------------------------------------------------------------------
15
  /**
16
   * The output handle.
17
   *
18
   * @var resource
19
   */
20
  protected $handle;
21
22
  //--------------------------------------------------------------------------------------------------------------------
23
  /**
24
   * Object constructor.
25
   *
26
   * @param resource $handle The handle to write the var dump to.
27
   */
28
  public function __construct($handle)
29 2
  {
30
    $this->handle = $handle;
31 2
  }
32 2
33
  //--------------------------------------------------------------------------------------------------------------------
34
  /**
35
   * Does nothing.
36
   */
37
  public function start(): void
38 2
  {
39
    fwrite($this->handle, '<table class="var-dump">');
40 2
  }
41 2
42
  //--------------------------------------------------------------------------------------------------------------------
43
  /**
44
   * Does nothing.
45
   */
46
  public function stop(): void
47 2
  {
48
    fwrite($this->handle, '</table>');
49 2
  }
50 2
51
  //--------------------------------------------------------------------------------------------------------------------
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function writeArrayClose(int $id, $name): void
56 2
  {
57
    if ($name!==null)
58 2
    {
59
      fwrite($this->handle, '</table>');
60 2
      fwrite($this->handle, '</td>');
61 2
      fwrite($this->handle, '</tr>');
62 2
    }
63
  }
64 2
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function writeArrayOpen(int $id, $name): void
70 2
  {
71
    if ($name!==null)
72 2
    {
73
      fwrite($this->handle, '<tr>');
74 2
      $this->writeName($name, $id);
75 2
      fwrite($this->handle, '<td>');
76 2
      fwrite($this->handle, Html::htmlNested(['tag'  => 'div',
77 2
                                              'attr' => ['class' => 'array'],
78
                                              'text' => 'array']));
79
      fwrite($this->handle, '<br/>');
80 2
      fwrite($this->handle, '<table>');
81 2
    }
82
  }
83 2
84
  //--------------------------------------------------------------------------------------------------------------------
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public function writeArrayReference(int $ref, $name): void
89 1
  {
90
    $html = Html::htmlNested(['tag'  => 'span',
91 1
                              'attr' => ['class' => 'array'],
92
                              'text' => 'array']);
93
    $html .= ', ';
94 1
    $html .= Html::htmlNested(['tag'  => 'a',
95 1
                               'attr' => ['href' => '#'.$ref],
96 1
                               'text' => 'see '.$ref]);
97 1
98
    fwrite($this->handle, '<tr>');
99 1
    $this->writeName($name);
100 1
    fwrite($this->handle, Html::htmlNested(['tag'  => 'td',
101 1
                                            'html' => $html]));
102 1
    fwrite($this->handle, '</tr>');
103 1
  }
104 1
105
  //--------------------------------------------------------------------------------------------------------------------
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function writeBool(?int $id, ?int $ref, bool &$value, $name): void
110 1
  {
111
    $this->writeScalar($id, $ref, $name, ($value) ? 'true' : 'false', 'keyword');
112 1
  }
113 1
114
  //--------------------------------------------------------------------------------------------------------------------
115
  /**
116
   * {@inheritdoc}
117
   */
118
  public function writeFloat(?int $id, ?int $ref, float &$value, $name): void
119 1
  {
120
    $this->writeScalar($id, $ref, $name, (string)$value, 'number');
121 1
  }
122 1
123
  //--------------------------------------------------------------------------------------------------------------------
124
  /**
125
   * {@inheritdoc}
126
   */
127
  public function writeInt(?int $id, ?int $ref, int &$value, $name): void
128 1
  {
129
    $this->writeScalar($id, $ref, $name, (string)$value, 'number');
130 1
  }
131 1
132
  //--------------------------------------------------------------------------------------------------------------------
133
  /**
134
   * {@inheritdoc}
135
   */
136
  public function writeNull(?int $id, ?int $ref, $name): void
137 1
  {
138
    $this->writeScalar($id, $ref, $name, 'null', 'keyword');
139 1
  }
140 1
141
  //--------------------------------------------------------------------------------------------------------------------
142
  /**
143
   * {@inheritdoc}
144
   */
145
  public function writeObjectClose(int $id, mixed $name, string $class): void
146 2
  {
147
    if ($name!==null)
148 2
    {
149
      fwrite($this->handle, '</table>');
150 2
      fwrite($this->handle, '</td>');
151 2
      fwrite($this->handle, '</tr>');
152 2
    }
153
  }
154 2
155
  //--------------------------------------------------------------------------------------------------------------------
156
  /**
157
   * {@inheritdoc}
158
   */
159
  public function writeObjectOpen(int $id, mixed $name, string $class): void
160 2
  {
161
    if ($name!==null)
162 2
    {
163
      fwrite($this->handle, '<tr>');
164 2
      $this->writeName($name, $id);
165 2
      fwrite($this->handle, '<td>');
166 2
      fwrite($this->handle, Html::htmlNested(['tag'  => 'div',
167 2
                                              'attr' => ['class' => 'class'],
168
                                              'text' => $class]));
169 2
      fwrite($this->handle, '<br/>');
170 2
      fwrite($this->handle, '<table>');
171 2
    }
172
  }
173 2
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * {@inheritdoc}
177
   */
178
  public function writeObjectReference(int $ref, mixed $name, string $class): void
179
  {
180
    $html = Html::htmlNested(['tag'  => 'span',
181
                              'attr' => ['class' => 'class'],
182
                              'text' => $class]);
183
    $html .= ', ';
184
    $html .= Html::htmlNested(['tag'  => 'a',
185
                               'attr' => ['href' => '#'.$ref],
186
                               'text' => 'see '.$ref]);
187
188
    fwrite($this->handle, '<tr>');
189
    $this->writeName($name);
190
    fwrite($this->handle, Html::htmlNested(['tag'  => 'td',
191
                                            'html' => $html]));
192
    fwrite($this->handle, '</tr>');
193
  }
194
195
  //--------------------------------------------------------------------------------------------------------------------
196
  /**
197
   * {@inheritdoc}
198
   */
199
  public function writeResource(?int $id, ?int $ref, mixed $name, string $type): void
200 1
  {
201
    $this->writeScalar($id, $ref, $name, $type, 'keyword');
202 1
  }
203 1
204
  //--------------------------------------------------------------------------------------------------------------------
205
  /**
206
   * {@inheritdoc}
207
   */
208
  public function writeString(?int $id, ?int $ref, string &$value, mixed $name): void
209 2
  {
210
    $text  = mb_strimwidth($value, 0, 80, '...');
211 2
    $title = ($text!=$value) ? mb_strimwidth($value, 0, 512, '...') : null;
212 2
213
    $this->writeScalar($id, $ref, $name, $text, 'string', $title);
214 2
  }
215 2
216
  //--------------------------------------------------------------------------------------------------------------------
217
  /**
218
   * {@inheritdoc}
219
   */
220
  public function writeUninitialized(mixed $name): void
221 1
  {
222
    $this->writeScalar(null, null, $name, 'uninitialized', 'uninitialized');
223 1
  }
224 1
225
  //--------------------------------------------------------------------------------------------------------------------
226
  /**
227
   * Writes the name of a variable.
228
   *
229
   * @param string|int|null $name The name of the variable.
230
   * @param int|null        $id   The ID of the value.
231
   */
232
  private function writeName(mixed $name, ?int $id = null): void
233 2
  {
234
    if ($name===null || $name==='')
235 2
    {
236
      fwrite($this->handle, Html::htmlNested(['tag'  => 'th',
237 2
                                              'attr' => ['class' => 'id',
238 2
                                                         'id'    => $id],
239 2
                                              'text' => $id]));
240 2
    }
241
    else
242
    {
243
      $title = null;
244 2
245
      if (is_int($name))
246 2
      {
247
        $text  = (string)$name;
248 1
        $class = 'number';
249 1
      }
250
      elseif (is_string($name))
251 2
      {
252
        $class = 'string';
253 2
        $text  = mb_strimwidth((string)$name, 0, 20, '...');
254 2
        if ($text!=$name)
255 2
        {
256
          $title = mb_strimwidth((string)$name, 0, 512, '...');
257 2
        }
258
      }
259
      else
260
      {
261
        throw new \InvalidArgumentException(sprintf('$name has unexpected type %s', gettype($name)));
262
      }
263
264
      fwrite($this->handle, Html::htmlNested(['tag'  => 'th',
265 2
                                              'attr' => ['class' => 'id'],
266
                                              'text' => $id]));
267 2
268
      fwrite($this->handle, Html::htmlNested(['tag'  => 'th',
269 2
                                              'attr' => ['class' => $class,
270 2
                                                         'id'    => $id,
271 2
                                                         'title' => $title],
272 2
                                              'text' => $text]));
273 2
    }
274
  }
275 2
276
  //--------------------------------------------------------------------------------------------------------------------
277
  /**
278
   * Dumps a scalar value.
279
   *
280
   * @param int|null        $id    The ID of the value.
281
   * @param int|null        $ref   The ID of the value if the variable is a reference to a value that has been dumped
282
   *                               already.
283
   * @param string|int|null $name  The name of the variable.
284
   * @param string          $text  The text for displaying the value.
285
   * @param string          $class The class of the value.
286
   * @param string|null     $title The title for the value.
287
   */
288
  private function writeScalar(?int $id, ?int $ref, mixed $name, string $text, string $class, ?string $title = null)
289 2
  {
290
    $html = Html::htmlNested(['tag'  => 'span',
291 2
                              'attr' => ['class' => $class, 'title' => $title],
292 2
                              'text' => $text]);
293 2
    if ($ref!==null)
294 2
    {
295
      $html .= ', ';
296
      $html .= Html::htmlNested(['tag'  => 'a',
297
                                 'attr' => ['href' => '#'.$ref],
298
                                 'text' => 'see '.$ref]);
299
    }
300
301
    fwrite($this->handle, '<tr>');
302 2
    $this->writeName($name, $id);
303 2
    fwrite($this->handle, Html::htmlNested(['tag'  => 'td',
304 2
                                            'html' => $html]));
305 2
    fwrite($this->handle, '</tr>');
306 2
  }
307 2
}
308
309
//----------------------------------------------------------------------------------------------------------------------
310