Passed
Push — master ( 80ae69...77d195 )
by Alex
01:44
created

SetVariablePass::mergeScopeVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace AlexMasterov\PsyshBundle\DependencyInjection\Compiler;
5
6
use Symfony\Component\DependencyInjection\{
7
    Compiler\CompilerPassInterface,
8
    ContainerBuilder,
9
    Reference
10
};
11
12
class SetVariablePass implements CompilerPassInterface
13
{
14
    /**
15
     * @inheritDoc
16
     */
17 5
    public function process(ContainerBuilder $container)
18
    {
19 5
        if (false === $container->has('psysh.shell')) {
20
            return;
21
        }
22
23 5
        $services = $container->findTaggedServiceIds('psysh.variable', true);
24 5
        if (empty($services)) {
25
            return;
26
        }
27
28 5
        $this->registerScopeVariables($services, $container);
29 5
    }
30
31 5
    private function registerScopeVariables(array $services, ContainerBuilder $container): void
32
    {
33 5
        $definition = $container->getDefinition('psysh.shell');
34
35 5
        if ($definition->hasMethodCall('setScopeVariables')) {
36 1
            $calls = $this->mergeScopeVariables(
37 1
                $definition->getMethodCalls(),
38 1
                $this->scopeVariables($services)
39
            );
40 1
            $definition->setMethodCalls($calls);
41
        } else {
42 4
            $definition->addMethodCall('setScopeVariables', [$this->scopeVariables($services)]);
43
        }
44 5
    }
45
46 1
    private function mergeScopeVariables(array $calls, array $variables): array
47
    {
48 1
        foreach ($calls as $i => [$method, $args]) {
49 1
            if ('setScopeVariables' === $method) {
1 ignored issue
show
Bug introduced by
The variable $method 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...
50 1
                foreach ($args as $arg) {
1 ignored issue
show
Bug introduced by
The variable $args 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 1
                    $variables += $arg;
52
                }
53 1
                unset($calls[$i]);
54
            }
55
        }
56
57
        return $calls + [
58 1
            ['setScopeVariables', [$variables]],
59
        ];
60
    }
61
62
    private function scopeVariables(array $services): array
63
    {
64
        // NameSpace\SomeName -> nameSpaceSomename
65 5
        $classify = static function (string $spec): string {
1 ignored issue
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
66 2
            return \str_replace('\\', '', \lcfirst(\ucwords(\strtolower($spec), '\\')));
67 5
        };
68
69 5
        $scopeVariables = [];
70 5
        foreach ($services as $id => $attributes) {
71 5
            $variable = $attributes[0]['name']
72 5
                ?? (\class_exists($id) ? $classify($id) : $id);
73 5
            $scopeVariables[$variable] = new Reference($id);
74
        }
75
76 5
        return $scopeVariables;
77
    }
78
}
79