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 (#190)
by Renato
08:10 queued 01:58
created

Configuration   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 229
ccs 186
cts 188
cp 0.9894
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getConfigTreeBuilder() 0 146 6
B addBuilderSection() 0 32 4
B addSecurityQuerySection() 0 34 3
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
class Configuration implements ConfigurationInterface
21
{
22
    private $debug;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param bool $debug Whether to use the debug mode
28
     */
29 18
    public function __construct($debug)
30
    {
31 18
        $this->debug = (bool) $debug;
32 18
    }
33
34 18
    public function getConfigTreeBuilder()
35
    {
36 18
        $treeBuilder = new TreeBuilder();
37 18
        $rootNode = $treeBuilder->root('overblog_graphql');
38
39
        $rootNode
40 18
            ->children()
41 18
                ->enumNode('batching_method')
42 18
                    ->values(['relay', 'apollo'])
43 18
                    ->defaultValue('relay')
44 18
                ->end()
45 18
                ->arrayNode('definitions')
46 18
                    ->addDefaultsIfNotSet()
47 18
                    ->children()
48 18
                        ->scalarNode('internal_error_message')->defaultNull()->end()
49 18
                        ->booleanNode('show_debug_info')->defaultFalse()->end()
50 18
                        ->booleanNode('config_validation')->defaultValue($this->debug)->end()
51 18
                        ->arrayNode('schema')
52 18
                            ->beforeNormalization()
53 18
                                ->ifTrue(function ($v) {
54 16
                                    $needNormalization = isset($v['query']) && is_string($v['query']) ||
55
                                        isset($v['mutation']) && is_string($v['mutation']) ||
56 16
                                        isset($v['subscription']) && is_string($v['subscription']);
57
58 16
                                    return $needNormalization;
59 18
                                })
60 18
                                ->then(function ($v) {
61 16
                                    return ['default' => $v];
62 18
                                })
63 18
                            ->end()
64 18
                            ->useAttributeAsKey('name')
65 18
                            ->prototype('array')
66 18
                                ->addDefaultsIfNotSet()
67 18
                                ->children()
68 18
                                    ->scalarNode('query')->defaultNull()->end()
69 18
                                    ->scalarNode('mutation')->defaultNull()->end()
70 18
                                    ->scalarNode('subscription')->defaultNull()->end()
71 18
                                ->end()
72 18
                            ->end()
73 18
                        ->end()
74 18
                        ->arrayNode('auto_mapping')
75 18
                            ->treatFalseLike(['enabled' => false])
76 18
                            ->treatTrueLike(['enabled' => true])
77 18
                            ->treatNullLike(['enabled' => true])
78 18
                            ->addDefaultsIfNotSet()
79 18
                            ->children()
80 18
                                ->booleanNode('enabled')->defaultTrue()->end()
81 18
                                ->arrayNode('directories')
82 18
                                    ->info('List of directories containing GraphQL classes.')
83 18
                                    ->prototype('scalar')->end()
84 18
                                ->end()
85 18
                            ->end()
86 18
                        ->end()
87 18
                        ->arrayNode('mappings')
88 18
                            ->children()
89 18
                                ->arrayNode('types')
90 18
                                    ->prototype('array')
91 18
                                        ->addDefaultsIfNotSet()
92 18
                                        ->children()
93 18
                                            ->enumNode('type')->isRequired()->values(['yml', 'xml'])->end()
94 18
                                            ->scalarNode('dir')->defaultNull()->end()
95 18
                                        ->end()
96 18
                                    ->end()
97 18
                                ->end()
98 18
                            ->end()
99 18
                        ->end()
100 18
                        ->arrayNode('exceptions')
101 18
                            ->addDefaultsIfNotSet()
102 18
                            ->children()
103 18
                                ->arrayNode('warnings')
104 18
                                    ->treatNullLike([])
105 18
                                    ->prototype('scalar')->end()
106 18
                                ->end()
107 18
                                ->arrayNode('errors')
108 18
                                    ->treatNullLike([])
109 18
                                    ->prototype('scalar')->end()
110 18
                                ->end()
111 18
                                ->arrayNode('types')
112 18
                                    ->addDefaultsIfNotSet()
113 18
                                    ->children()
114 18
                                        ->scalarNode('warnings')
115 18
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS)
116 18
                                        ->end()
117 18
                                        ->scalarNode('errors')
118 18
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS)
119 18
                                        ->end()
120 18
                                    ->end()
121 18
                                ->end()
122 18
                            ->end()
123 18
                        ->end()
124
125 18
                        ->arrayNode('builders')
126 18
                            ->children()
127 18
                                ->append($this->addBuilderSection('field'))
128 18
                                ->append($this->addBuilderSection('args'))
129 18
                            ->end()
130 18
                        ->end()
131
132 18
                    ->end()
133 18
                ->end()
134 18
                ->arrayNode('templates')
135 18
                    ->addDefaultsIfNotSet()
136 18
                    ->children()
137 18
                        ->scalarNode('graphiql')
138 18
                            ->defaultValue('OverblogGraphQLBundle:GraphiQL:index.html.twig')
139 18
                        ->end()
140 18
                    ->end()
141 18
                ->end()
142 18
                ->arrayNode('services')
143 18
                    ->addDefaultsIfNotSet()
144 18
                    ->children()
145 18
                        ->scalarNode('executor')
146 18
                            ->defaultValue('overblog_graphql.executor.default')
147 18
                        ->end()
148 18
                        ->scalarNode('promise_adapter')
149 18
                            ->defaultValue('overblog_graphql.promise_adapter.default')
150 18
                        ->end()
151 18
                        ->scalarNode('expression_language')
152 18
                            ->defaultValue('overblog_graphql.expression_language.default')
153 18
                        ->end()
154 18
                        ->scalarNode('cache_expression_language_parser')
155 18
                            ->defaultValue('overblog_graphql.cache_expression_language_parser.default')
156 18
                        ->end()
157 18
                    ->end()
158 18
                ->end()
159 18
                ->arrayNode('security')
160 18
                    ->addDefaultsIfNotSet()
161 18
                    ->children()
162 18
                        ->append($this->addSecurityQuerySection('query_max_depth', QueryDepth::DISABLED))
163 18
                        ->append($this->addSecurityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
164 18
                        ->booleanNode('handle_cors')->defaultFalse()->end()
165 18
                    ->end()
166 18
                ->end()
167 18
                ->arrayNode('versions')
168 18
                    ->addDefaultsIfNotSet()
169 18
                    ->children()
170 18
                        ->scalarNode('graphiql')->defaultValue('0.9')->end()
171 18
                        ->scalarNode('react')->defaultValue('15.4')->end()
172 18
                        ->scalarNode('fetch')->defaultValue('2.0')->end()
173 18
                        ->enumNode('relay')->values(['modern', 'classic'])->defaultValue('classic')->end()
174 18
                    ->end()
175 18
                ->end()
176 18
            ->end();
177
178 18
        return $treeBuilder;
179
    }
180
181 18
    private function addBuilderSection($name)
182
    {
183 18
        $builder = new TreeBuilder();
184 18
        $node = $builder->root($name);
185 18
        $node->beforeNormalization()
186 18
            ->ifTrue(function ($v) {
187 1
                return is_array($v) && !empty($v);
188 18
            })
189 18
            ->then(function ($v) {
190 1
                foreach ($v as $key => &$config) {
191 1
                    if (is_string($config)) {
192
                        $config = [
193 1
                            'alias' => $key,
194 1
                            'class' => $config,
195
                        ];
196
                    }
197
                }
198
199 1
                return $v;
200 18
            })
201 18
        ->end();
202
203 18
        $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...
204 18
            ->children()
205 18
                ->scalarNode('alias')->isRequired()->end()
206 18
                ->scalarNode('class')->isRequired()->end()
207 18
            ->end()
208 18
        ->end()
209
        ;
210
211 18
        return $node;
212
    }
213
214 18
    private function addSecurityQuerySection($name, $disabledValue)
215
    {
216 18
        $builder = new TreeBuilder();
217 18
        $node = $builder->root($name, 'scalar');
218 18
        $node->beforeNormalization()
219 18
                ->ifTrue(function ($v) {
220 4
                    return is_string($v) && is_numeric($v);
221 18
                })
222 18
                ->then(function ($v) {
223 2
                    return (int) $v;
224 18
                })
225 18
            ->end();
226
227
        $node
228 18
            ->info('Disabled if equal to false.')
229 18
            ->beforeNormalization()
230 18
                ->ifTrue(function ($v) {
231 4
                    return false === $v;
232 18
                })
233 18
                ->then(function () use ($disabledValue) {
234
                    return $disabledValue;
235 18
                })
236 18
            ->end()
237 18
            ->defaultFalse()
238 18
            ->validate()
239 18
                ->ifTrue(function ($v) {
240 4
                    return is_int($v) && $v < 0;
241 18
                })
242
                ->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.')
243
            ->end()
244
        ;
245
246
        return $node;
247
    }
248
}
249