Completed
Push — refactor ( 1ac315 )
by Akihito
05:56
created

GraphDumper::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Koriym\Printo\Printo;
8
use Ray\Di\Container;
9
use Ray\Di\Name;
10
11
use function explode;
12
use function file_exists;
13
use function file_put_contents;
14
use function mkdir;
15
use function str_replace;
16
17
use const LOCK_EX;
18
19
final class GraphDumper
20
{
21
    /** @var Container */
22
    private $container;
23
24
    /** @var string */
25
    private $scriptDir;
26
27
    public function __construct(Container $container, string $scriptDir)
28
    {
29
        $this->container = $container;
30
        $this->scriptDir = $scriptDir;
31
    }
32
33
    public function __invoke(): void
34
    {
35
        $container = $this->container->getContainer();
36
        foreach ($container as $dependencyIndex => $dependency) {
37
            $isNotInjector = $dependencyIndex !== 'Ray\Di\InjectorInterface-' . Name::ANY;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $dependencyIndex (integer) and 'Ray\\Di\\InjectorInterface-' . \Ray\Di\Name::ANY (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
38
            if ($isNotInjector) {
39
                $this->write($dependencyIndex);
40
            }
41
        }
42
    }
43
44
    private function write(string $dependencyIndex): void
45
    {
46
        if ($dependencyIndex === 'Ray\Aop\MethodInvocation-') {
47
            return;
48
        }
49
50
        [$interface, $name] = explode('-', $dependencyIndex);
0 ignored issues
show
Bug introduced by
The variable $interface does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
        $instance = (new ScriptInjector($this->scriptDir))->getInstance($interface, $name);
52
        $graph = (string) (new Printo($instance))
53
            ->setRange(Printo::RANGE_ALL)
54
            ->setLinkDistance(130)
55
            ->setCharge(-500);
56
        $graphDir = $this->scriptDir . '/graph/';
57
        if (! file_exists($graphDir)) {
58
            mkdir($graphDir);
59
        }
60
61
        $file = $graphDir . str_replace('\\', '_', $dependencyIndex) . '.html';
62
        file_put_contents($file, $graph, LOCK_EX);
63
    }
64
}
65