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 ( 4e5a29...e6532b )
by Jérémiah
15:06
created

Configuration   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 229
ccs 184
cts 186
cp 0.9892
rs 10

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