Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#137)
by Jérémiah
08:18
created

prependExtensionConfigFromFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Resource\FileResource;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
class OverblogGraphQLTypesExtension extends Extension
21
{
22
    private static $configTypes = ['yml', 'xml'];
23
24 78
    public function load(array $configs, ContainerBuilder $container)
25
    {
26 78
        $configuration = $this->getConfiguration($configs, $container);
27 78
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 26 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
28
29 72
        $container->setParameter($this->getAlias().'.config', $config);
30 72
    }
31
32 73
    public function containerPrependExtensionConfig(array $config, ContainerBuilder $container)
33
    {
34 73
        $typesMappings = $this->mappingConfig($config, $container);
35
36
        // treats mappings
37 73
        foreach ($typesMappings as $params) {
38 73
            $this->prependExtensionConfigFromFiles($params['type'], $params['files'], $container);
39 71
        }
40 71
    }
41
42
    /**
43
     * @param $type
44
     * @param SplFileInfo[]    $files
45
     * @param ContainerBuilder $container
46
     */
47 73
    private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder $container)
48
    {
49
        /** @var SplFileInfo $file */
50 73
        foreach ($files as $file) {
51 73
            $parserClass = sprintf('Overblog\\GraphQLBundle\\Config\\Parser\\%sParser', ucfirst($type));
52
53 73
            $typeConfig = call_user_func($parserClass.'::parse', $file, $container);
54 71
            $container->prependExtensionConfig($this->getAlias(), $typeConfig);
55 71
        }
56 71
    }
57
58 73
    private function mappingConfig(array $config, ContainerBuilder $container)
59
    {
60 73
        $typesMappings = empty($config['definitions']['mappings']['types']) ? [] : $config['definitions']['mappings']['types'];
61
62
        // app only config files (yml or xml)
63 73
        if ($container->hasParameter('kernel.root_dir')) {
64 71
            $typesMappings[] = ['dir' => $container->getParameter('kernel.root_dir').'/config/graphql', 'type' => null];
65 71
        }
66
67 73
        $mappingFromBundles = $this->mappingFromBundles($container);
68 73
        $typesMappings = array_merge($typesMappings, $mappingFromBundles);
69
70
        // from config
71 73
        $typesMappings = array_filter(array_map(
72 73
            function (array $typeMapping) use ($container) {
73 73
                $params = $this->detectFilesByType($container, $typeMapping['dir'],  $typeMapping['type']);
74
75 73
                return $params;
76 73
            },
77
            $typesMappings
78 73
        ));
79
80 73
        return $typesMappings;
81
    }
82
83 74
    private function mappingFromBundles(ContainerBuilder $container)
84
    {
85 73
        $typesMappings = [];
86 73
        $bundles = $container->getParameter('kernel.bundles');
87
88
        // auto detect from bundle
89 73
        foreach ($bundles as $name => $class) {
90 71
            $bundle = new \ReflectionClass($class);
91 71
            $bundleDir = dirname($bundle->getFileName());
92
93
            // only config files (yml or xml)
94 71
            $typesMappings[] = ['dir' => $bundleDir.'/Resources/config/graphql', 'type' => null];
95 73
        }
96
97 74
        return $typesMappings;
98
    }
99
100 73
    private function detectFilesByType(ContainerBuilder $container, $path, $type = null)
101 1
    {
102
        // add the closest existing directory as a resource
103 73
        $resource = $path;
104 73
        while (!is_dir($resource)) {
105 71
            $resource = dirname($resource);
106 71
        }
107 73
        $container->addResource(new FileResource($resource));
108
109 73
        $finder = new Finder();
110
111 73
        $types = null === $type ? self::$configTypes : [$type];
112
113 73
        foreach ($types as $type) {
114
            try {
115 73
                $finder->files()->in($path)->name('*.types.'.$type);
116 73
            } catch (\InvalidArgumentException $e) {
117 71
                continue;
118
            }
119 73
            if ($finder->count() > 0) {
120
                return [
121 73
                    'type' => $type,
122 73
                    'files' => $finder,
123 73
                ];
124
            }
125 71
        }
126
127 71
        return;
128
    }
129
130 72
    public function getAliasPrefix()
131
    {
132 72
        return 'overblog_graphql';
133
    }
134
135 72
    public function getAlias()
136
    {
137 72
        return $this->getAliasPrefix().'_types';
138
    }
139
140 78
    public function getConfiguration(array $config, ContainerBuilder $container)
141
    {
142 78
        return new TypesConfiguration();
143
    }
144
}
145