Completed
Pull Request — master (#79)
by
unknown
05:51
created

getSourceContextProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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 Ekino\Drupal\Debug\Action\DisplayDumpLocation\SourceContextProvider as BackPortedSourceContextProvider;
19
use Symfony\Component\VarDumper\Cloner\VarCloner;
20
use Symfony\Component\VarDumper\Dumper\CliDumper;
21
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ekino\Drupal\Debug\Actio...n\SourceContextProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
23
use Symfony\Component\VarDumper\VarDumper;
24
25
class DisplayDumpLocationAction implements EventSubscriberActionInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public static function getSubscribedEvents(): array
31
    {
32
        return array(
33 1
            DebugKernelEvents::ON_KERNEL_INSTANTIATION => 'process',
34
        );
35
    }
36
37 1
    public function process(): void
38
    {
39 1
        $cloner = new VarCloner();
40 1
        $dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
41 1
        $sourceContextProvider = $this->getSourceContextProvider();
42
43
        VarDumper::setHandler(function ($var) use ($cloner, $dumper, $sourceContextProvider ): void {
44
            (function () use ($sourceContextProvider) : void {
45
                list('name' => $name, 'file' => $file, 'line' => $line) = $sourceContextProvider->getContext();
46
47
                $attr = array();
48
                if ($this instanceof HtmlDumper) {
0 ignored issues
show
introduced by
$this is never a sub-type of Symfony\Component\VarDumper\Dumper\HtmlDumper.
Loading history...
49
                    if (\is_string($file)) {
50
                        $attr = array(
51
                            'file' => $file,
52
                            'line' => $line,
53
                            'title' => $file,
54
                        );
55
                    }
56
                } else {
57
                    $name = $file;
58
                }
59
60
                $this->line = \sprintf('%s on line %s:', $this->style('meta', $name, $attr), $this->style('meta', $line));
0 ignored issues
show
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...
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

60
                $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...
61
                $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

61
                $this->/** @scrutinizer ignore-call */ 
62
                       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...
62
            })->bindTo($dumper, $dumper)();
63
64
            $dumper->dump($cloner->cloneVar($var));
65 1
        });
66 1
    }
67
68
    /**
69
     * Get the Source Context Provider.
70
     * It will return an instance of the SourceContextProvider if existing.
71
     * Otherwise, it will return an instance of
72
     * the BackPortedSourceContextProvider.
73
     */
74 2
    private function getSourceContextProvider()
75
    {
76 2
        if (!\class_exists(SourceContextProvider::class)) {
77
            return new BackPortedSourceContextProvider();
78
        }
79
80 2
        return new SourceContextProvider();
81
    }
82
83
}
84