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 — 0.8 (#176)
by Evgenij
08:19
created

Configuration   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.8%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 210
ccs 165
cts 167
cp 0.988
rs 10

4 Methods

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