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

Completed
Push — master ( 842647...ecb285 )
by Jérémiah
50s
created

Configuration::addBuilderSection()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 23
cts 23
cp 1
rs 8.5806
cc 4
eloc 21
nc 1
nop 1
crap 4
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 GraphQL\Validator\Rules\QueryComplexity;
15
use GraphQL\Validator\Rules\QueryDepth;
16
use Overblog\GraphQLBundle\Error\ErrorHandler;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
/**
21
 * @todo fix xml
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    private $debug;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param bool $debug Whether to use the debug mode
31
     */
32 13
    public function __construct($debug)
33
    {
34 13
        $this->debug = (bool) $debug;
35 13
    }
36
37 13
    public function getConfigTreeBuilder()
38
    {
39 13
        $treeBuilder = new TreeBuilder();
40 13
        $rootNode = $treeBuilder->root('overblog_graphql');
41
42
        $rootNode
43 13
            ->children()
44 13
                ->arrayNode('definitions')
45 13
                    ->addDefaultsIfNotSet()
46 13
                    ->children()
47 13
                        ->scalarNode('internal_error_message')->defaultNull()->end()
48 13
                        ->booleanNode('show_debug_info')->defaultValue(false)->end()
49 13
                        ->booleanNode('config_validation')->defaultValue($this->debug)->end()
50 13
                        ->arrayNode('schema')
51 13
                            ->beforeNormalization()
52
                                ->ifTrue(function ($v) {
53 10
                                    $needNormalization = isset($v['query']) && is_string($v['query']) ||
54
                                        isset($v['mutation']) && is_string($v['mutation']) ||
55 10
                                        isset($v['subscription']) && is_string($v['subscription']);
56
57 10
                                    return $needNormalization;
58 13
                                })
59
                                ->then(function ($v) {
60 10
                                    return ['default' => $v];
61 13
                                })
62 13
                            ->end()
63 13
                            ->useAttributeAsKey('name')
64 13
                            ->prototype('array')
65 13
                                ->addDefaultsIfNotSet()
66 13
                                ->children()
67 13
                                    ->scalarNode('query')->defaultNull()->end()
68 13
                                    ->scalarNode('mutation')->defaultNull()->end()
69 13
                                    ->scalarNode('subscription')->defaultNull()->end()
70 13
                                ->end()
71 13
                            ->end()
72 13
                        ->end()
73 13
                        ->arrayNode('mappings')
74 13
                            ->children()
75 13
                                ->arrayNode('types')
76 13
                                    ->prototype('array')
77 13
                                        ->addDefaultsIfNotSet()
78 13
                                        ->children()
79 13
                                            ->enumNode('type')->isRequired()->values(['yml', 'xml'])->end()
80 13
                                            ->scalarNode('dir')->defaultNull()->end()
81 13
                                        ->end()
82 13
                                    ->end()
83 13
                                ->end()
84 13
                            ->end()
85 13
                        ->end()
86 13
                        ->arrayNode('exceptions')
87 13
                            ->addDefaultsIfNotSet()
88 13
                            ->children()
89 13
                                ->arrayNode('warnings')
90 13
                                    ->treatNullLike([])
91 13
                                    ->prototype('scalar')->end()
92 13
                                ->end()
93 13
                                ->arrayNode('errors')
94 13
                                    ->treatNullLike([])
95 13
                                    ->prototype('scalar')->end()
96 13
                                ->end()
97 13
                                ->arrayNode('types')
98 13
                                    ->addDefaultsIfNotSet()
99 13
                                    ->children()
100 13
                                        ->scalarNode('warnings')
101 13
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS)
102 13
                                        ->end()
103 13
                                        ->scalarNode('errors')
104 13
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS)
105 13
                                        ->end()
106 13
                                    ->end()
107 13
                                ->end()
108 13
                            ->end()
109 13
                        ->end()
110
111 13
                        ->arrayNode('builders')
112 13
                            ->children()
113 13
                                ->append($this->addBuilderSection('field'))
114 13
                                ->append($this->addBuilderSection('args'))
115 13
                            ->end()
116 13
                        ->end()
117
118 13
                    ->end()
119 13
                ->end()
120 13
                ->arrayNode('templates')
121 13
                    ->addDefaultsIfNotSet()
122 13
                    ->children()
123 13
                        ->scalarNode('graphiql')
124 13
                            ->defaultValue('OverblogGraphQLBundle:GraphiQL:index.html.twig')
125 13
                        ->end()
126 13
                    ->end()
127 13
                ->end()
128 13
                ->arrayNode('services')
129 13
                    ->addDefaultsIfNotSet()
130 13
                    ->children()
131 13
                        ->scalarNode('executor')
132 13
                            ->defaultValue('overblog_graphql.executor.default')
133 13
                        ->end()
134 13
                        ->scalarNode('promise_adapter')
135 13
                            ->defaultValue('overblog_graphql.promise_adapter.default')
136 13
                        ->end()
137 13
                        ->scalarNode('expression_language')
138 13
                            ->defaultValue('overblog_graphql.expression_language.default')
139 13
                        ->end()
140 13
                        ->scalarNode('cache_expression_language_parser')
141 13
                            ->defaultValue('overblog_graphql.cache_expression_language_parser.default')
142 13
                        ->end()
143 13
                    ->end()
144 13
                ->end()
145 13
                ->arrayNode('security')
146 13
                    ->addDefaultsIfNotSet()
147 13
                    ->children()
148 13
                        ->append($this->addSecurityQuerySection('query_max_depth', QueryDepth::DISABLED))
149 13
                        ->append($this->addSecurityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
150 13
                    ->end()
151 13
                ->end()
152 13
                ->arrayNode('versions')
153 13
                    ->addDefaultsIfNotSet()
154 13
                    ->children()
155 13
                        ->scalarNode('graphiql')->defaultValue('0.7.8')->end()
156 13
                        ->scalarNode('react')->defaultValue('15.3.2')->end()
157 13
                        ->scalarNode('fetch')->defaultValue('2.0.1')->end()
158 13
                    ->end()
159 13
                ->end()
160 13
            ->end();
161
162 13
        return $treeBuilder;
163
    }
164
165 13
    private function addBuilderSection($name)
166
    {
167 13
        $builder = new TreeBuilder();
168 13
        $node = $builder->root($name);
169 13
        $node->beforeNormalization()
170
            ->ifTrue(function ($v) {
171 1
                return is_array($v) && !empty($v);
172 13
            })
173
            ->then(function ($v) {
174 1
                foreach ($v as $key => &$config) {
175 1
                    if (is_string($config)) {
176
                        $config = [
177 1
                            'alias' => $key,
178 1
                            'class' => $config,
179 1
                        ];
180 1
                    }
181 1
                }
182
183 1
                return $v;
184 13
            })
185 13
        ->end();
186
187 13
        $node->prototype('array')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
188 13
            ->children()
189 13
                ->scalarNode('alias')->isRequired()->end()
190 13
                ->scalarNode('class')->isRequired()->end()
191 13
            ->end()
192 13
        ->end()
193
        ;
194
195 13
        return $node;
196
    }
197
198 13
    private function addSecurityQuerySection($name, $disabledValue)
199
    {
200 13
        $builder = new TreeBuilder();
201 13
        $node = $builder->root($name, 'integer');
202
203
        $node
204 13
            ->info('Disabled if equal to false.')
205 13
            ->beforeNormalization()
206
                ->ifTrue(function ($v) {
207 1
                    return false === $v;
208 13
                })
209
                ->then(function () use ($disabledValue) {
210
                    return $disabledValue;
211 13
                })
212 13
            ->end()
213 13
            ->defaultFalse()
214 13
            ->validate()
215 13
                ->ifTrue(function ($v) {
216 1
                    return $v < 0;
217 13
                })
218 13
                ->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.')
219 13
            ->end()
220
        ;
221
222 13
        return $node;
223
    }
224
}
225