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
09:13
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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