DumpRecorder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 9
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 22 2
A record() 0 10 1
A getDumps() 0 4 1
A reset() 0 4 1
A toArray() 0 10 2
A getDefaultHandler() 0 9 2
1
<?php
2
3
namespace Facade\Ignition\DumpRecorder;
4
5
use Illuminate\Foundation\Application;
6
use Illuminate\Support\Arr;
7
use Symfony\Component\VarDumper\Cloner\Data;
8
use Symfony\Component\VarDumper\Cloner\VarCloner;
9
use Symfony\Component\VarDumper\Dumper\CliDumper;
10
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
11
use Symfony\Component\VarDumper\VarDumper;
12
13
class DumpRecorder
14
{
15
    protected $dumps = [];
16
17
    /** @var \Illuminate\Foundation\Application */
18
    protected $app;
19
20
    public function __construct(Application $app)
21
    {
22
        $this->app = $app;
23
    }
24
25
    public function register(): self
26
    {
27
        $multiDumpHandler = new MultiDumpHandler();
28
29
        $this->app->singleton(MultiDumpHandler::class, $multiDumpHandler);
0 ignored issues
show
Documentation introduced by
$multiDumpHandler is of type object<Facade\Ignition\D...order\MultiDumpHandler>, but the function expects a object<Closure>|string|null.

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...
30
31
        $previousHandler = VarDumper::setHandler(function ($var) use ($multiDumpHandler) {
32
            $multiDumpHandler->dump($var);
33
        });
34
35
        if ($previousHandler) {
36
            $multiDumpHandler->addHandler($previousHandler);
37
        } else {
38
            $multiDumpHandler->addHandler($this->getDefaultHandler());
39
        }
40
41
        $multiDumpHandler->addHandler(function ($var) {
42
            $this->app->make(DumpHandler::class)->dump($var);
43
        });
44
45
        return $this;
46
    }
47
48
    public function record(Data $data)
49
    {
50
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
51
        $file = Arr::get($backtrace, '6.file');
52
        $lineNumber = Arr::get($backtrace, '6.line');
53
54
        $htmlDump = (new HtmlDumper())->dump($data);
55
56
        $this->dumps[] = new Dump($htmlDump, $file, $lineNumber);
57
    }
58
59
    public function getDumps(): array
60
    {
61
        return $this->toArray();
62
    }
63
64
    public function reset()
65
    {
66
        $this->dumps = [];
67
    }
68
69
    public function toArray(): array
70
    {
71
        $dumps = [];
72
73
        foreach ($this->dumps as $dump) {
74
            $dumps[] = $dump->toArray();
75
        }
76
77
        return $dumps;
78
    }
79
80
    protected function getDefaultHandler()
81
    {
82
        return function ($value) {
83
            $data = (new VarCloner)->cloneVar($value);
84
85
            $dumper = in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new BaseHtmlDumper;
86
            $dumper->dump($data);
87
        };
88
    }
89
}
90