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 |
||
28 | class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
||
29 | { |
||
30 | 28 | public function load(array $configs, ContainerBuilder $container) |
|
31 | { |
||
32 | 28 | $this->loadConfigFiles($container); |
|
33 | 28 | $config = $this->treatConfigs($configs, $container); |
|
34 | |||
35 | 28 | $this->setBatchingMethod($config, $container); |
|
|
|||
36 | 28 | $this->setExpressionLanguageDefaultParser($container); |
|
37 | 28 | $this->setServicesAliases($config, $container); |
|
38 | 28 | $this->setSchemaBuilderArguments($config, $container); |
|
39 | 28 | $this->setSchemaArguments($config, $container); |
|
40 | 28 | $this->setErrorHandler($config, $container); |
|
41 | 28 | $this->setSecurity($config, $container); |
|
42 | 28 | $this->setConfigBuilders($config, $container); |
|
43 | 28 | $this->setDebugListener($config, $container); |
|
44 | 28 | $this->setDefinitionParameters($config, $container); |
|
45 | 28 | $this->setClassLoaderListener($config, $container); |
|
46 | 28 | $this->setCompilerCacheWarmer($config, $container); |
|
47 | |||
48 | 28 | $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
49 | 28 | } |
|
50 | |||
51 | 26 | public function prepend(ContainerBuilder $container) |
|
61 | |||
62 | 28 | public function getAlias() |
|
66 | |||
67 | 28 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
74 | |||
75 | 28 | private function loadConfigFiles(ContainerBuilder $container) |
|
76 | { |
||
77 | 28 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
78 | 28 | $loader->load('services.yml'); |
|
79 | 28 | $loader->load('graphql_types.yml'); |
|
80 | 28 | $loader->load('expression_language_functions.yml'); |
|
81 | 28 | $loader->load('definition_config_processors.yml'); |
|
82 | 28 | } |
|
83 | |||
84 | 28 | private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
|
95 | |||
96 | 28 | private function setClassLoaderListener(array $config, ContainerBuilder $container) |
|
97 | { |
||
98 | 28 | $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
|
99 | 28 | if ($config['definitions']['use_classloader_listener']) { |
|
100 | 27 | $definition = $container->setDefinition( |
|
101 | 27 | $this->getAlias().'.event_listener.classloader_listener', |
|
102 | 27 | new Definition(ClassLoaderListener::class) |
|
103 | ); |
||
104 | 27 | $definition->setPublic(true); |
|
105 | 27 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
106 | 27 | $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
|
107 | 27 | $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
|
108 | } |
||
109 | 28 | } |
|
110 | |||
111 | 28 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
121 | |||
122 | 28 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
123 | { |
||
124 | 28 | $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
|
125 | 28 | } |
|
126 | |||
127 | 28 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
128 | { |
||
129 | 28 | $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; |
|
130 | 28 | $definition = new Definition($class); |
|
131 | 28 | $definition->setPublic(false); |
|
132 | 28 | $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
133 | 28 | } |
|
134 | |||
135 | 28 | private function setDebugListener(array $config, ContainerBuilder $container) |
|
136 | { |
||
137 | 28 | if ($config['definitions']['show_debug_info']) { |
|
138 | 1 | $definition = $container->setDefinition( |
|
139 | 1 | DebugListener::class, |
|
140 | 1 | new Definition(DebugListener::class) |
|
141 | ); |
||
142 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
|
143 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
|
144 | } |
||
145 | 28 | } |
|
146 | |||
147 | 28 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
148 | { |
||
149 | 28 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
150 | 28 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
151 | |||
152 | 28 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
153 | 28 | if (!empty($config['definitions']['builders'][$type])) { |
|
154 | 1 | foreach ($config['definitions']['builders'][$type] as $params) { |
|
155 | 1 | $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
|
156 | 1 | $container->$objectToAddResourceMethod($object); |
|
157 | 28 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
158 | } |
||
159 | } |
||
160 | } |
||
161 | 28 | } |
|
162 | |||
163 | 28 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
164 | { |
||
165 | 28 | static $config = null; |
|
166 | |||
167 | 28 | if ($forceReload || null === $config) { |
|
168 | 28 | $configuration = $this->getConfiguration($configs, $container); |
|
169 | 28 | $config = $this->processConfiguration($configuration, $configs); |
|
170 | } |
||
171 | |||
172 | 28 | return $config; |
|
173 | } |
||
174 | |||
175 | 28 | private function setSecurity(array $config, ContainerBuilder $container) |
|
181 | |||
182 | 28 | private function setErrorHandler(array $config, ContainerBuilder $container) |
|
215 | |||
216 | 28 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
217 | { |
||
218 | 28 | $container->getDefinition($this->getAlias().'.schema_builder') |
|
219 | 28 | ->replaceArgument(2, $config['definitions']['config_validation']); |
|
220 | 28 | } |
|
221 | |||
222 | 28 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
223 | { |
||
224 | 28 | if (isset($config['definitions']['schema'])) { |
|
246 | |||
247 | 28 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
256 | |||
257 | /** |
||
258 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
259 | * |
||
260 | * @param array $exceptionConfig |
||
261 | * |
||
262 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
263 | */ |
||
264 | 28 | private function buildExceptionMap(array $exceptionConfig) |
|
280 | } |
||
281 |
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.