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