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 |
||
26 | class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
||
27 | { |
||
28 | 27 | public function load(array $configs, ContainerBuilder $container) |
|
29 | { |
||
30 | 27 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
31 | 27 | $loader->load('services.yml'); |
|
32 | 27 | $loader->load('graphql_types.yml'); |
|
33 | |||
34 | 27 | $config = $this->treatConfigs($configs, $container); |
|
35 | |||
36 | 27 | $this->setBatchingMethod($config, $container); |
|
|
|||
37 | 27 | $this->setExpressionLanguageDefaultParser($container); |
|
38 | 27 | $this->setServicesAliases($config, $container); |
|
39 | 27 | $this->setSchemaBuilderArguments($config, $container); |
|
40 | 27 | $this->setSchemaArguments($config, $container); |
|
41 | 27 | $this->setErrorHandler($config, $container); |
|
42 | 27 | $this->setSecurity($config, $container); |
|
43 | 27 | $this->setConfigBuilders($config, $container); |
|
44 | 27 | $this->setDebugListener($config, $container); |
|
45 | 27 | $this->setDefinitionParameters($config, $container); |
|
46 | 27 | $this->setClassLoaderListener($config, $container); |
|
47 | 27 | $this->setCompilerCacheWarmer($config, $container); |
|
48 | |||
49 | 27 | $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
50 | 27 | } |
|
51 | |||
52 | 25 | public function prepend(ContainerBuilder $container) |
|
62 | |||
63 | 27 | public function getAlias() |
|
67 | |||
68 | 27 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
75 | |||
76 | 27 | private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
|
77 | { |
||
78 | 27 | if ($config['definitions']['auto_compile']) { |
|
79 | 26 | $definition = $container->setDefinition( |
|
80 | 26 | CompileCacheWarmer::class, |
|
81 | 26 | new Definition(CompileCacheWarmer::class) |
|
82 | ); |
||
83 | 26 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
84 | 26 | $definition->addTag('kernel.cache_warmer', ['priority' => 50]); |
|
85 | } |
||
86 | 27 | } |
|
87 | |||
88 | 27 | private function setClassLoaderListener(array $config, ContainerBuilder $container) |
|
89 | { |
||
90 | 27 | $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
|
91 | 27 | if ($config['definitions']['use_classloader_listener']) { |
|
92 | 26 | $definition = $container->setDefinition( |
|
93 | 26 | $this->getAlias().'.event_listener.classloader_listener', |
|
94 | 26 | new Definition(ClassLoaderListener::class) |
|
95 | ); |
||
96 | 26 | $definition->setPublic(true); |
|
97 | 26 | $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
|
98 | 26 | $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
|
99 | 26 | $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
|
100 | } |
||
101 | 27 | } |
|
102 | |||
103 | 27 | private function setDefinitionParameters(array $config, ContainerBuilder $container) |
|
113 | |||
114 | 27 | private function setBatchingMethod(array $config, ContainerBuilder $container) |
|
115 | { |
||
116 | 27 | $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
|
117 | 27 | } |
|
118 | |||
119 | 27 | private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
120 | { |
||
121 | 27 | $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; |
|
122 | 27 | $definition = new Definition($class); |
|
123 | 27 | $definition->setPublic(false); |
|
124 | 27 | $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
125 | 27 | } |
|
126 | |||
127 | 27 | private function setDebugListener(array $config, ContainerBuilder $container) |
|
138 | |||
139 | 27 | private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
140 | { |
||
141 | 27 | $useObjectToAddResource = method_exists($container, 'addObjectResource'); |
|
142 | 27 | $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
|
143 | |||
144 | 27 | foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
|
145 | 27 | if (!empty($config['definitions']['builders'][$type])) { |
|
146 | 1 | foreach ($config['definitions']['builders'][$type] as $params) { |
|
147 | 1 | $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
|
148 | 1 | $container->$objectToAddResourceMethod($object); |
|
149 | 27 | BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
|
150 | } |
||
151 | } |
||
152 | } |
||
153 | 27 | } |
|
154 | |||
155 | 27 | private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
156 | { |
||
157 | 27 | static $config = null; |
|
158 | |||
159 | 27 | if ($forceReload || null === $config) { |
|
160 | 27 | $configuration = $this->getConfiguration($configs, $container); |
|
161 | 27 | $config = $this->processConfiguration($configuration, $configs); |
|
162 | } |
||
163 | |||
164 | 27 | return $config; |
|
165 | } |
||
166 | |||
167 | 27 | private function setSecurity(array $config, ContainerBuilder $container) |
|
173 | |||
174 | 27 | private function setErrorHandler(array $config, ContainerBuilder $container) |
|
175 | { |
||
176 | 27 | if ($config['errors_handler']['enabled']) { |
|
177 | 27 | $id = $this->getAlias().'.error_handler'; |
|
178 | 27 | $errorHandlerDefinition = $container->setDefinition($id, new Definition(ErrorHandler::class)); |
|
179 | 27 | $errorHandlerDefinition->setPublic(false) |
|
214 | |||
215 | 27 | private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
220 | |||
221 | 27 | private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
238 | |||
239 | 27 | private function setServicesAliases(array $config, ContainerBuilder $container) |
|
248 | |||
249 | /** |
||
250 | * Returns a list of custom exceptions mapped to error/warning classes. |
||
251 | * |
||
252 | * @param array $exceptionConfig |
||
253 | * |
||
254 | * @return array Custom exception map, [exception => UserError/UserWarning] |
||
255 | */ |
||
256 | 27 | private function buildExceptionMap(array $exceptionConfig) |
|
273 | } |
||
274 |
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.