loadInternal()   B
last analyzed

Complexity

Conditions 8
Paths 34

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 29
c 2
b 0
f 0
nc 34
nop 2
dl 0
loc 51
rs 8.2114
ccs 25
cts 25
cp 1
crap 8

How to fix   Long Method   

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
namespace Incenteev\TranslationCheckerBundle\DependencyInjection;
4
5
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
6
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
7
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11
/**
12 8
 * @internal
13
 */
14 8
class IncenteevTranslationCheckerExtension extends ConfigurableExtension
15 8
{
16
    /**
17
     * @param array<string, mixed> $config
18 8
     */
19 8
    public function loadInternal(array $config, ContainerBuilder $container): void
20 8
    {
21
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22
        $loader->load('services.xml');
23 8
24 8
        $container->registerForAutoconfiguration('Incenteev\TranslationCheckerBundle\Translator\Extractor\ExtractorInterface')
25
            ->addTag('incenteev_translation_checker.extractor');
26 8
27 2
        $dirs = array();
28
        $overridePathPatterns = array();
29 8
30
        if ($container->hasParameter('kernel.root_dir')) {
31 8
            $overridePathPatterns[] = '%%kernel.root_dir%%/Resources/%s/views';
32
        }
33 8
        $overridePathPatterns[] = '%%kernel.project_dir%%/templates/bundles/%s';
34 3
35 1
        /** @var array<string, class-string<BundleInterface>> $registeredBundles */
36
        $registeredBundles = $container->getParameter('kernel.bundles');
37
38 2
        foreach ($config['extraction']['bundles'] as $bundle) {
39 2
            if (!isset($registeredBundles[$bundle])) {
40
                throw new \InvalidArgumentException(sprintf('The bundle %s is not registered in the kernel.', $bundle));
41 2
            }
42 2
43
            $reflection = new \ReflectionClass($registeredBundles[$bundle]);
44
45
            if (false === $reflection->getFilename()) {
46 7
                continue;
47 2
            }
48
49
            $dirs[] = dirname($reflection->getFilename()).'/Resources/views';
50 7
51
            foreach ($overridePathPatterns as $overridePath) {
52 7
                $dirs[] = sprintf($overridePath, $bundle);
53
            }
54 7
        }
55 5
56
        if ($container->hasParameter('kernel.root_dir')) {
57 2
            $dirs[] = '%kernel.root_dir%/Resources/views';
58 2
        }
59 2
60
        $dirs[] = '%kernel.project_dir%/templates';
61 7
62
        $container->setParameter('incenteev_translation_checker.extractor.symfony.paths', $dirs);
63
64
        if (empty($config['extraction']['js']['paths'])) {
65
            $container->removeDefinition('incenteev_translation_checker.extractor.js');
66
        } else {
67
            $container->getDefinition('incenteev_translation_checker.extractor.js')
68
                ->replaceArgument(0, $config['extraction']['js']['paths'])
69
                ->replaceArgument(1, $config['extraction']['js']['default_domain']);
70
        }
71
    }
72
}
73