Completed
Push — master ( f808f4...fba7cb )
by Tomáš
07:11 queued 04:32
created

MultiCodingStandardExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 7
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 66
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 5 1
A beforeCompile() 0 4 1
A loadServicesFromConfig() 0 6 1
A loadCommandsToConsoleApplication() 0 9 2
A getDefinitionByType() 0 7 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 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