We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Complex classes like OverblogGraphQLExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OverblogGraphQLExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
||
21 | { |
||
22 | 25 | public function load(array $configs, ContainerBuilder $container) |
|
23 | { |
||
24 | 25 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
25 | 25 | $loader->load('services.yml'); |
|
26 | 25 | $loader->load('graphql_types.yml'); |
|
27 | |||
28 | 25 | $config = $this->treatConfigs($configs, $container); |
|
29 | |||
30 | 25 | $this->setBatchingMethod($config, $container); |
|
|
|||
31 | 25 | $this->setExpressionLanguageDefaultParser($container); |
|
32 | 25 | $this->setServicesAliases($config, $container); |
|
33 | 25 | $this->setSchemaBuilderArguments($config, $container); |
|
34 | 25 | $this->setSchemaArguments($config, $container); |
|
35 | 25 | $this->setErrorHandlerArguments($config, $container); |
|
36 | 25 | $this->setSecurity($config, $container); |
|
37 | 25 | $this->setConfigBuilders($config, $container); |
|
38 | 25 | $this->setShowDebug($config, $container); |
|
39 | 25 | $this->setDefinitionParameters($config, $container); |
|
40 | 25 | $this->setClassLoaderListener($config, $container); |
|
41 | 25 | $this->setCompilerCacheWarmer($config, $container); |
|
42 | |||
43 | 25 | $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
44 | 25 | } |
|
45 | |||
46 | 23 | public function prepend(ContainerBuilder $container) |
|
56 | |||
57 | 25 | public function getAlias() |
|
58 | { |
||
59 | 25 | return 'overblog_graphql'; |
|
60 | } |
||
61 | |||
62 | 25 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
63 | { |
||
64 | 25 | return new Configuration( |
|
65 | 25 | $container->getParameter('kernel.debug'), |
|
66 | 25 | $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null |
|
67 | ); |
||
68 | } |
||
69 | |||
70 | 25 | private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
|
71 | { |
||
72 | 25 | if ($config['definitions']['auto_compile']) { |
|
73 | 24 | $definition = $container->setDefinition( |
|
74 | 24 | CompileCacheWarmer::class, |
|
75 | 24 | new Definition(CompileCacheWarmer::class) |
|
76 | ); |
||
77 | 24 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
78 | 24 | $definition->addTag('kernel.cache_warmer', ['priority' => 50]); |
|
79 | } |
||
80 | 25 | } |
|
81 | |||
82 | 25 | private function setClassLoaderListener(array $config, ContainerBuilder $container) |
|
83 | { |
||
84 | 25 | $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
|
85 | 25 | if ($config['definitions']['use_classloader_listener']) { |
|
86 | 24 | $definition = $container->setDefinition( |
|
87 | 24 | $this->getAlias().'.event_listener.classloader_listener', |
|
88 | 24 | new Definition(ClassLoaderListener::class) |
|
89 | ); |
||
90 | 24 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
91 | 24 | $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
|
92 | 24 | $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
|
93 | } |
||
94 | 25 | } |
|
95 | |||
96 | 25 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
97 | { |
||
98 | // auto mapping |
||
99 | 25 | $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']); |
|
100 | 25 | $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']); |
|
101 | // generator and config |
||
102 | 25 | $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']); |
|
103 | 25 | $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']); |
|
104 | 25 | $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']); |
|
105 | 25 | } |
|
106 | |||
107 | 25 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
108 | { |
||
109 | 25 | $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
|
110 | 25 | } |
|
111 | |||
112 | 25 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
113 | { |
||
114 | 25 | $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; |
|
115 | 25 | $definition = new Definition($class); |
|
116 | 25 | $definition->setPublic(false); |
|
117 | 25 | $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
118 | 25 | } |
|
119 | |||
120 | 25 | private function setShowDebug(array $config, ContainerBuilder $container) |
|
121 | { |
||
122 | 25 | $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']); |
|
123 | 25 | } |
|
124 | |||
125 | 25 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
126 | { |
||
127 | 25 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
128 | 25 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
129 | |||
130 | 25 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
131 | 25 | if (!empty($config['definitions']['builders'][$type])) { |
|
132 | 1 | foreach ($config['definitions']['builders'][$type] as $params) { |
|
133 | 1 | $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
|
134 | 1 | $container->$objectToAddResourceMethod($object); |
|
135 | 25 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
136 | } |
||
137 | } |
||
138 | } |
||
139 | 25 | } |
|
140 | |||
141 | 25 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
142 | { |
||
143 | 25 | static $config = null; |
|
144 | |||
145 | 25 | if ($forceReload || null === $config) { |
|
146 | 25 | $configuration = $this->getConfiguration($configs, $container); |
|
147 | 25 | $config = $this->processConfiguration($configuration, $configs); |
|
148 | } |
||
149 | |||
150 | 25 | return $config; |
|
151 | } |
||
152 | |||
153 | 25 | private function setSecurity(array $config, ContainerBuilder $container) |
|
154 | { |
||
155 | 25 | foreach ($config['security'] as $key => $value) { |
|
156 | 25 | $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value); |
|
157 | } |
||
158 | 25 | } |
|
159 | |||
160 | 25 | private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
|
161 | { |
||
162 | 25 | $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); |
|
163 | |||
164 | 25 | if (isset($config['definitions']['internal_error_message'])) { |
|
165 | 2 | $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); |
|
166 | } |
||
167 | |||
168 | 25 | if (isset($config['definitions']['map_exceptions_to_parent'])) { |
|
169 | 25 | $errorHandlerDefinition->replaceArgument( |
|
170 | 25 | 3, |
|
171 | 25 | $config['definitions']['map_exceptions_to_parent'] |
|
172 | ); |
||
173 | } |
||
174 | |||
175 | 25 | if (isset($config['definitions']['exceptions'])) { |
|
176 | $errorHandlerDefinition |
||
177 | 25 | ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions'])) |
|
178 | 25 | ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']]) |
|
179 | 25 | ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']]) |
|
180 | ; |
||
181 | } |
||
182 | 25 | } |
|
183 | |||
184 | 25 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
189 | |||
190 | 25 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
191 | { |
||
192 | 25 | if (isset($config['definitions']['schema'])) { |
|
193 | 25 | $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor'); |
|
194 | |||
195 | 25 | foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
|
196 | 21 | $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
|
197 | 21 | $definition = new Definition(Schema::class); |
|
198 | 21 | $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']); |
|
199 | 21 | $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]); |
|
200 | 21 | $definition->setPublic(false); |
|
201 | 21 | $container->setDefinition($schemaID, $definition); |
|
202 | |||
203 | 21 | $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]); |
|
204 | } |
||
205 | } |
||
206 | 25 | } |
|
207 | |||
208 | 25 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
217 | |||
218 | /** |
||
219 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
220 | * |
||
221 | * @param array $exceptionConfig |
||
222 | * |
||
223 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
224 | */ |
||
225 | 25 | private function buildExceptionMap(array $exceptionConfig) |
|
242 | } |
||
243 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.