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) |
|
| 97 | |||
| 98 | 27 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
| 108 | |||
| 109 | 27 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
| 113 | |||
| 114 | 27 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
| 121 | |||
| 122 | 27 | private function setDebugListener(array $config, ContainerBuilder $container) |
|
| 123 | { |
||
| 124 | 27 | if ($config['definitions']['show_debug_info']) { |
|
| 125 | 1 | $definition = $container->setDefinition( |
|
| 126 | 1 | DebugListener::class, |
|
| 127 | 1 | new Definition(DebugListener::class) |
|
| 128 | ); |
||
| 129 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
|
| 130 | 1 | $definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
|
| 131 | } |
||
| 132 | 27 | } |
|
| 133 | |||
| 134 | 27 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
| 135 | { |
||
| 136 | 27 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
| 137 | 27 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
| 138 | |||
| 139 | 27 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
| 140 | 27 | if (!empty($config['definitions']['builders'][$type])) { |
|
| 141 | 1 | foreach ($config['definitions']['builders'][$type] as $params) { |
|
| 142 | 1 | $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
|
| 143 | 1 | $container->$objectToAddResourceMethod($object); |
|
| 144 | 27 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | 27 | } |
|
| 149 | |||
| 150 | 27 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
| 151 | { |
||
| 152 | 27 | static $config = null; |
|
| 153 | |||
| 154 | 27 | if ($forceReload || null === $config) { |
|
| 155 | 27 | $configuration = $this->getConfiguration($configs, $container); |
|
| 156 | 27 | $config = $this->processConfiguration($configuration, $configs); |
|
| 157 | } |
||
| 158 | |||
| 159 | 27 | return $config; |
|
| 160 | } |
||
| 161 | |||
| 162 | 27 | private function setSecurity(array $config, ContainerBuilder $container) |
|
| 163 | { |
||
| 164 | 27 | foreach ($config['security'] as $key => $value) { |
|
| 165 | 27 | $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value); |
|
| 166 | } |
||
| 167 | 27 | } |
|
| 168 | |||
| 169 | 27 | private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
|
| 170 | { |
||
| 171 | 27 | $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); |
|
| 172 | |||
| 173 | 27 | if (isset($config['definitions']['internal_error_message'])) { |
|
| 174 | 2 | $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); |
|
| 175 | } |
||
| 176 | |||
| 177 | 27 | if (isset($config['definitions']['map_exceptions_to_parent'])) { |
|
| 178 | 27 | $errorHandlerDefinition->replaceArgument( |
|
| 179 | 27 | 3, |
|
| 180 | 27 | $config['definitions']['map_exceptions_to_parent'] |
|
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | 27 | if (isset($config['definitions']['exceptions'])) { |
|
| 185 | $errorHandlerDefinition |
||
| 186 | 27 | ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions'])) |
|
| 187 | 27 | ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']]) |
|
| 188 | 27 | ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']]) |
|
| 189 | ; |
||
| 190 | } |
||
| 191 | 27 | } |
|
| 192 | |||
| 193 | 27 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
| 198 | |||
| 199 | 27 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
| 216 | |||
| 217 | 27 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
| 229 | * |
||
| 230 | * @param array $exceptionConfig |
||
| 231 | * |
||
| 232 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
| 233 | */ |
||
| 234 | 27 | private function buildExceptionMap(array $exceptionConfig) |
|
| 251 | } |
||
| 252 |
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.