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

setConfigToContainerBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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