DisplayDumpLocationAction::process()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 16.1792

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 3
nop 0
dl 0
loc 31
ccs 4
cts 17
cp 0.2353
crap 16.1792
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Action\DisplayDumpLocation;
15
16
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface;
17
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
18
use Symfony\Component\VarDumper\Cloner\VarCloner;
19
use Symfony\Component\VarDumper\Dumper\CliDumper;
20
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
21
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
22
use Symfony\Component\VarDumper\VarDumper;
23
24
class DisplayDumpLocationAction implements EventSubscriberActionInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public static function getSubscribedEvents(): array
30
    {
31
        return array(
32 1
            DebugKernelEvents::ON_KERNEL_INSTANTIATION => 'process',
33
        );
34
    }
35
36 1
    public function process(): void
37
    {
38 1
        if (!\class_exists(SourceContextProvider::class)) {
39
            return;
40
        }
41
42 1
        $cloner = new VarCloner();
43 1
        $dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
44
45
        VarDumper::setHandler(static function ($var) use ($cloner, $dumper): void {
46
            (function (): void {
47
                list('name' => $name, 'file' => $file, 'line' => $line) = (new SourceContextProvider())->getContext();
48
49
                $attr = array();
50
                if ($this instanceof HtmlDumper) {
0 ignored issues
show
introduced by
$this is never a sub-type of Symfony\Component\VarDumper\Dumper\HtmlDumper.
Loading history...
51
                    if (\is_string($file)) {
52
                        $attr = array(
53
                            'file' => $file,
54
                            'line' => $line,
55
                            'title' => $file,
56
                        );
57
                    }
58
                } else {
59
                    $name = $file;
60
                }
61
62
                $this->line = \sprintf('%s on line %s:', $this->style('meta', $name, $attr), $this->style('meta', $line));
0 ignored issues
show
Bug introduced by
The method style() does not exist on Ekino\Drupal\Debug\Actio...splayDumpLocationAction. ( Ignorable by Annotation )

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

62
                $this->line = \sprintf('%s on line %s:', $this->/** @scrutinizer ignore-call */ style('meta', $name, $attr), $this->style('meta', $line));

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 Best Practice introduced by
The property line does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
                $this->dumpLine(0);
0 ignored issues
show
Bug introduced by
The method dumpLine() does not exist on Ekino\Drupal\Debug\Actio...splayDumpLocationAction. ( Ignorable by Annotation )

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

63
                $this->/** @scrutinizer ignore-call */ 
64
                       dumpLine(0);

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...
64
            })->bindTo($dumper, $dumper)();
65
66
            $dumper->dump($cloner->cloneVar($var));
67 1
        });
68 1
    }
69
}
70