Completed
Push — master ( f808f4...fba7cb )
by Tomáš
07:11 queued 04:32
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 Symplify\MultiCodingStandard\Console\Application;
15
16
final class MultiCodingStandardExtension extends CompilerExtension
17
{
18
    /**
19
     * @var string[]
20
     */
21
    private $defaults = [
22
        'configPath' => '%appDir%/../multi-cs.json'
23
    ];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function loadConfiguration()
29
    {
30 4
        $this->setConfigToContainerBuilder($this->defaults);
31 4
        $this->loadServicesFromConfig();
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 4
    public function beforeCompile()
38
    {
39 4
        $this->loadCommandsToConsoleApplication();
40 4
    }
41
42 4
    private function loadServicesFromConfig()
43
    {
44 4
        $containerBuilder = $this->getContainerBuilder();
45 4
        $config = $this->loadFromFile(__DIR__.'/../config/services.neon');
46 4
        $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 45 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...
47 4
    }
48
49 4
    private function loadCommandsToConsoleApplication()
50
    {
51 4
        $consoleApplication = $this->getDefinitionByType(Application::class);
52
53 4
        $containerBuilder = $this->getContainerBuilder();
54 4
        foreach ($containerBuilder->findByType(Command::class) as $definition) {
55 4
            $consoleApplication->addSetup('add', ['@'.$definition->getClass()]);
56
        }
57 4
    }
58
59
    /**
60
     * @param string $type
61
     *
62
     * @return ServiceDefinition
63
     */
64 4
    private function getDefinitionByType($type)
65
    {
66 4
        $containerBuilder = $this->getContainerBuilder();
67 4
        $definitionName = $containerBuilder->getByType($type);
68
69 4
        return $containerBuilder->getDefinition($definitionName);
70
    }
71
72
    /**
73
     * @param string[] $defaults
74
     */
75 4
    private function setConfigToContainerBuilder(array $defaults)
0 ignored issues
show
Unused Code introduced by
The parameter $defaults is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77 4
        $config = $this->validateConfig($this->defaults);
78 4
        $config['configPath'] = Helpers::expand($config['configPath'], $this->getContainerBuilder()->parameters);
79 4
        $this->getContainerBuilder()->parameters += $config;
80 4
    }
81
}
82