Passed
Pull Request — master (#9)
by ANTHONIUS
03:11
created

DefaultPass::process()   C

Complexity

Conditions 12
Paths 56

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.1474
c 0
b 0
f 0
cc 12
eloc 32
nc 56
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the dotfiles project.
5
 *
6
 *     (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Dotfiles\Core\DI\Compiler;
13
14
use Dotfiles\Core\Command\CommandInterface;
15
use Dotfiles\Core\Console\Application;
16
use Dotfiles\Core\Event\Dispatcher;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
22
class DefaultPass implements CompilerPassInterface
23
{
24
    public function process(ContainerBuilder $container)
25
    {
26
        $ids = $container->getServiceIds();
27
        $commands = array();
28
        $subscribers = array();
29
        foreach ($ids as $id) {
30
            $definition = $container->findDefinition($id);
31
            $definition
32
                ->setPublic(true)
33
                ->setAutoconfigured(true)
34
                ->setAutowired(true)
35
            ;
36
            $class = $definition->getClass();
37
            if (!class_exists($class)) {
38
                continue;
39
            }
40
            if (!$container->hasDefinition($class) && class_exists($class)) {
41
                if ($class !== $id) {
42
                    $container
43
                        ->setAlias($class, $id)
44
                        ->setPublic(true)
45
                    ;
46
                }
47
            }
48
49
            $r = new \ReflectionClass($class);
50
            if (
51
                $r->implementsInterface(CommandInterface::class)
52
                && !in_array($class, $commands)
53
            ) {
54
                $commands[] = $class;
55
            }
56
57
            if (
58
                $r->implementsInterface(EventSubscriberInterface::class)
59
                && !in_array($class, $subscribers)
60
            ) {
61
                $subscribers[] = $class;
62
            }
63
        }
64
65
        $application = $container->findDefinition(Application::class);
66
        $dispatcher = $container->findDefinition(Dispatcher::class);
67
        foreach ($commands as $class) {
68
            $application->addMethodCall('add', array(new Reference($class)));
69
        }
70
71
        foreach ($subscribers as $class) {
72
            $dispatcher->addMethodCall('addSubscriber', array(new Reference($class)));
73
        }
74
    }
75
}
76