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
Pull Request — master (#198)
by Jérémiah
24:03 queued 20:46
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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