Completed
Pull Request — master (#10)
by Tomáš
24:14
created

MultiCodingStandardExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 6
c 6
b 1
f 0
lcom 2
cbo 5
dl 0
loc 57
ccs 24
cts 24
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 5 1
A beforeCompile() 0 5 1
A loadServicesFromConfig() 0 6 1
A loadCommandsToConsoleApplication() 0 4 1
A loadEventSubscribersToEventDispatcher() 0 4 1
A setConfigToContainerBuilder() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\MultiCodingStandard\DI;
9
10
use Nette\DI\CompilerExtension;
11
use Nette\DI\Helpers;
12
use Nette\DI\ServiceDefinition;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symplify\MultiCodingStandard\Console\MultiCodingStandardApplication;
17
use Symplify\PHP7_CodeSniffer\DI\ExtensionHelperTrait;
18
19
final class MultiCodingStandardExtension extends CompilerExtension
20
{
21
    use ExtensionHelperTrait;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $defaults = [
27
        'configPath' => '%appDir%/../multi-cs.json'
28
    ];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function loadConfiguration()
34
    {
35 6
        $this->setConfigToContainerBuilder($this->defaults);
36 6
        $this->loadServicesFromConfig();
37 6
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 6
    public function beforeCompile()
43
    {
44 6
        $this->loadCommandsToConsoleApplication();
45 6
        $this->loadEventSubscribersToEventDispatcher();
46 6
    }
47
48 6
    private function loadServicesFromConfig()
49
    {
50 6
        $containerBuilder = $this->getContainerBuilder();
51 6
        $config = $this->loadFromFile(__DIR__.'/../config/services.neon');
52 6
        $this->compiler->parseServices($containerBuilder, $config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->loadFromFile(__DI.../config/services.neon') on line 51 can also be of type string; however, Nette\DI\Compiler::parseServices() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The method Nette\DI\Compiler::parseServices() has been deprecated.

This method has been deprecated.

Loading history...
53 6
    }
54
55 6
    private function loadCommandsToConsoleApplication()
56
    {
57 6
        $this->addServicesToCollector(MultiCodingStandardApplication::class, Command::class, 'add');
58 6
    }
59
60 6
    private function loadEventSubscribersToEventDispatcher()
61
    {
62 6
        $this->addServicesToCollector(EventDispatcherInterface::class, EventSubscriberInterface::class, 'addSubscriber');
63 6
    }
64
65
66
    /**
67
     * @param string[] $defaults
68
     */
69 6
    private function setConfigToContainerBuilder(array $defaults)
70
    {
71 6
        $config = $this->validateConfig($defaults);
72 6
        $config['configPath'] = Helpers::expand($config['configPath'], $this->getContainerBuilder()->parameters);
73 6
        $this->getContainerBuilder()->parameters += $config;
74 6
    }
75
}
76