AlpixelMenuExtension::prepend()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Yaml\Parser;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
9
use Symfony\Component\DependencyInjection\Loader;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
12
13
/**
14
 * This is the class that loads and manages your bundle configuration.
15
 *
16
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
17
 */
18
class AlpixelMenuExtension extends Extension implements PrependExtensionInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function load(array $configs, ContainerBuilder $container)
24
    {
25
        if (!$container->hasParameter('default_locale')) {
26
            throw new UndefinedOptionsException('The default locale parameter must be defined under parameters in your symfony configuration');
27
        }
28
29
        $configuration = new Configuration();
30
        $this->processConfiguration($configuration, $configs);
31
32
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33
        $loader->load('services.yml');
34
35
        $menuBuilder = $container->getDefinition('alpixel_menu.builder');
36
        $menuBuilder->addMethodCall('setDefaultLocale', [$container->getParameter('default_locale')]);
37
    }
38
39
    public function prepend(ContainerBuilder $container)
40
    {
41
        $parser = new Parser();
42
        $config = $parser->parse(file_get_contents(__DIR__ . '/../Resources/config/config.yml'));
43
44
        foreach ($config as $key => $configuration) {
0 ignored issues
show
Bug introduced by
The expression $config of type null|object<Symfony\Comp...|array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
            $container->prependExtensionConfig($key, $configuration);
46
        }
47
    }
48
}
49