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 | 19 | public function load(array $configs, ContainerBuilder $container) |
|
| 31 | { |
||
| 32 | 19 | $this->loadConfigFiles($container); |
|
| 33 | 19 | $config = $this->treatConfigs($configs, $container); |
|
| 34 | |||
| 35 | 19 | $this->setBatchingMethod($config, $container); |
|
| 36 | 19 | $this->setExpressionLanguageDefaultParser($container); |
|
| 37 | 19 | $this->setServicesAliases($config, $container); |
|
| 38 | 19 | $this->setSchemaBuilderArguments($config, $container); |
|
| 39 | 19 | $this->setSchemaArguments($config, $container); |
|
| 40 | 19 | $this->setErrorHandler($config, $container); |
|
| 41 | 19 | $this->setSecurity($config, $container); |
|
| 42 | 19 | $this->setConfigBuilders($config, $container); |
|
| 43 | 19 | $this->setDebugListener($config, $container); |
|
| 44 | 19 | $this->setDefinitionParameters($config, $container); |
|
| 45 | 19 | $this->setClassLoaderListener($config, $container); |
|
| 46 | 19 | $this->setCompilerCacheWarmer($config, $container); |
|
| 47 | |||
| 48 | 19 | $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
| 49 | 19 | } |
|
| 50 | |||
| 51 | 17 | public function prepend(ContainerBuilder $container) |
|
| 61 | |||
| 62 | 19 | public function getAlias() |
|
| 66 | |||
| 67 | 19 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 74 | |||
| 75 | 19 | private function loadConfigFiles(ContainerBuilder $container) |
|
| 76 | { |
||
| 77 | 19 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 78 | 19 | $loader->load('services.yml'); |
|
| 79 | 19 | $loader->load('graphql_types.yml'); |
|
| 80 | 19 | $loader->load('expression_language_functions.yml'); |
|
| 81 | 19 | $loader->load('definition_config_processors.yml'); |
|
| 82 | 19 | } |
|
| 83 | |||
| 84 | 19 | private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
|
| 95 | |||
| 96 | 19 | private function setClassLoaderListener(array $config, ContainerBuilder $container) |
|
| 97 | { |
||
| 98 | 19 | $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
|
| 99 | 19 | if ($config['definitions']['use_classloader_listener']) { |
|
| 100 | 18 | $definition = $container->setDefinition( |
|
| 101 | 18 | $this->getAlias().'.event_listener.classloader_listener', |
|
| 102 | 18 | new Definition(ClassLoaderListener::class) |
|
| 103 | ); |
||
| 104 | 18 | $definition->setPublic(true); |
|
| 105 | 18 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
| 106 | 18 | $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
|
| 107 | 18 | $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
|
| 108 | } |
||
| 109 | 19 | } |
|
| 110 | |||
| 111 | 19 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
| 121 | |||
| 122 | 19 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
| 123 | { |
||
| 124 | 19 | $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
|
| 125 | 19 | } |
|
| 126 | |||
| 127 | 19 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
| 128 | { |
||
| 129 | 19 | $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; |
|
| 130 | 19 | $definition = new Definition($class); |
|
| 131 | 19 | $definition->setPublic(false); |
|
| 132 | 19 | $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
| 133 | 19 | } |
|
| 134 | |||
| 135 | 19 | private function setDebugListener(array $config, ContainerBuilder $container) |
|
| 136 | { |
||
| 137 | 19 | if ($config['definitions']['show_debug_info']) { |
|
| 138 | $definition = $container->setDefinition( |
||
| 139 | DebugListener::class, |
||
| 140 | new Definition(DebugListener::class) |
||
| 141 | ); |
||
| 142 | $definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
||
| 143 | $definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
||
| 144 | } |
||
| 145 | 19 | } |
|
| 146 | |||
| 147 | 19 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
| 148 | { |
||
| 149 | 19 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
| 150 | 19 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
| 151 | |||
| 152 | 19 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
| 153 | 19 | 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 | 19 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | 19 | } |
|
| 162 | |||
| 163 | 19 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
| 164 | { |
||
| 165 | 19 | static $config = null; |
|
| 166 | |||
| 167 | 19 | if ($forceReload || null === $config) { |
|
| 168 | 19 | $configuration = $this->getConfiguration($configs, $container); |
|
| 169 | 19 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 170 | } |
||
| 171 | |||
| 172 | 19 | return $config; |
|
| 173 | } |
||
| 174 | |||
| 175 | 19 | private function setSecurity(array $config, ContainerBuilder $container) |
|
| 181 | |||
| 182 | 19 | private function setErrorHandler(array $config, ContainerBuilder $container) |
|
| 215 | |||
| 216 | 19 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
| 217 | { |
||
| 218 | 19 | $container->getDefinition($this->getAlias().'.schema_builder') |
|
| 219 | 19 | ->replaceArgument(1, $config['definitions']['config_validation']); |
|
| 220 | 19 | } |
|
| 221 | |||
| 222 | 19 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
| 223 | { |
||
| 224 | 19 | if (isset($config['definitions']['schema'])) { |
|
| 225 | 19 | $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor'); |
|
| 226 | |||
| 227 | 19 | foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
|
| 228 | 15 | $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
|
| 229 | 15 | $definition = new Definition(Schema::class); |
|
| 230 | 15 | $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']); |
|
| 231 | 15 | $definition->setArguments([ |
|
| 232 | 15 | $schemaConfig['query'], |
|
| 233 | 15 | $schemaConfig['mutation'], |
|
| 234 | 15 | $schemaConfig['subscription'], |
|
| 235 | 15 | array_map(function ($id) { |
|
| 236 | return new Reference($id); |
||
| 237 | 15 | }, $schemaConfig['resolver_maps']), |
|
| 238 | 15 | $schemaConfig['types'], |
|
| 239 | ]); |
||
| 240 | 15 | $definition->setPublic(false); |
|
| 241 | 15 | $container->setDefinition($schemaID, $definition); |
|
| 242 | |||
| 243 | 15 | $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]); |
|
| 244 | } |
||
| 245 | } |
||
| 246 | 19 | } |
|
| 247 | |||
| 248 | 19 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
| 260 | * |
||
| 261 | * @param array $exceptionConfig |
||
| 262 | * |
||
| 263 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
| 264 | */ |
||
| 265 | 19 | private function buildExceptionMap(array $exceptionConfig) |
|
| 281 | } |
||
| 282 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: