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 |
||
| 22 | class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
||
| 23 | { |
||
| 24 | 27 | public function load(array $configs, ContainerBuilder $container) |
|
| 25 | { |
||
| 26 | 27 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 27 | 27 | $loader->load('services.yml'); |
|
| 28 | 27 | $loader->load('graphql_types.yml'); |
|
| 29 | |||
| 30 | 27 | $config = $this->treatConfigs($configs, $container); |
|
| 31 | |||
| 32 | 27 | $this->setBatchingMethod($config, $container); |
|
|
|
|||
| 33 | 27 | $this->setExpressionLanguageDefaultParser($container); |
|
| 34 | 27 | $this->setServicesAliases($config, $container); |
|
| 35 | 27 | $this->setSchemaBuilderArguments($config, $container); |
|
| 36 | 27 | $this->setSchemaArguments($config, $container); |
|
| 37 | 27 | $this->setErrorHandlerArguments($config, $container); |
|
| 38 | 27 | $this->setSecurity($config, $container); |
|
| 39 | 27 | $this->setConfigBuilders($config, $container); |
|
| 40 | 27 | $this->setDebugListener($config, $container); |
|
| 41 | 27 | $this->setDefinitionParameters($config, $container); |
|
| 42 | 27 | $this->setClassLoaderListener($config, $container); |
|
| 43 | 27 | $this->setCompilerCacheWarmer($config, $container); |
|
| 44 | |||
| 45 | 27 | $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
| 46 | 27 | } |
|
| 47 | |||
| 48 | 25 | public function prepend(ContainerBuilder $container) |
|
| 58 | |||
| 59 | 27 | public function getAlias() |
|
| 63 | |||
| 64 | 27 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 71 | |||
| 72 | 27 | private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
|
| 83 | |||
| 84 | 27 | private function setClassLoaderListener(array $config, ContainerBuilder $container) |
|
| 85 | { |
||
| 86 | 27 | $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
|
| 87 | 27 | if ($config['definitions']['use_classloader_listener']) { |
|
| 88 | 26 | $definition = $container->setDefinition( |
|
| 89 | 26 | $this->getAlias().'.event_listener.classloader_listener', |
|
| 90 | 26 | new Definition(ClassLoaderListener::class) |
|
| 91 | ); |
||
| 92 | 26 | $definition->setPublic(true); |
|
| 93 | 26 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
| 94 | 26 | $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
|
| 95 | 26 | $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
|
| 96 | } |
||
| 97 | 27 | } |
|
| 98 | |||
| 99 | 27 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
| 100 | { |
||
| 101 | // auto mapping |
||
| 102 | 27 | $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']); |
|
| 103 | 27 | $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']); |
|
| 104 | // generator and config |
||
| 105 | 27 | $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']); |
|
| 106 | 27 | $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']); |
|
| 107 | 27 | $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']); |
|
| 108 | 27 | } |
|
| 109 | |||
| 110 | 27 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
| 111 | { |
||
| 112 | 27 | $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
|
| 113 | 27 | } |
|
| 114 | |||
| 115 | 27 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
| 116 | { |
||
| 117 | 27 | $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; |
|
| 118 | 27 | $definition = new Definition($class); |
|
| 119 | 27 | $definition->setPublic(false); |
|
| 120 | 27 | $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
| 121 | 27 | } |
|
| 122 | |||
| 123 | 27 | private function setDebugListener(array $config, ContainerBuilder $container) |
|
| 124 | { |
||
| 125 | 27 | if ($config['definitions']['show_debug_info']) { |
|
| 126 | 1 | $definition = $container->setDefinition( |
|
| 127 | 1 | DebugListener::class, |
|
| 128 | 1 | new Definition(DebugListener::class) |
|
| 129 | ); |
||
| 130 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
|
| 131 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
|
| 132 | } |
||
| 133 | 27 | } |
|
| 134 | |||
| 135 | 27 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
| 136 | { |
||
| 137 | 27 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
| 138 | 27 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
| 139 | |||
| 140 | 27 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
| 141 | 27 | if (!empty($config['definitions']['builders'][$type])) { |
|
| 142 | 1 | foreach ($config['definitions']['builders'][$type] as $params) { |
|
| 143 | 1 | $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
|
| 144 | 1 | $container->$objectToAddResourceMethod($object); |
|
| 145 | 27 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | 27 | } |
|
| 150 | |||
| 151 | 27 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
| 162 | |||
| 163 | 27 | private function setSecurity(array $config, ContainerBuilder $container) |
|
| 169 | |||
| 170 | 27 | private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
|
| 171 | { |
||
| 172 | 27 | $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); |
|
| 173 | |||
| 174 | 27 | if (isset($config['definitions']['internal_error_message'])) { |
|
| 175 | 27 | $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); |
|
| 176 | } |
||
| 194 | |||
| 195 | 27 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
| 200 | |||
| 201 | 27 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
| 218 | |||
| 219 | 27 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
| 231 | * |
||
| 232 | * @param array $exceptionConfig |
||
| 233 | * |
||
| 234 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
| 235 | */ |
||
| 236 | 27 | private function buildExceptionMap(array $exceptionConfig) |
|
| 253 | } |
||
| 254 |
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.