Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Configuration implements ConfigurationInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Whether to use the debug mode. |
||
| 39 | * |
||
| 40 | * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
||
| 41 | * |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $debug; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param bool $debug |
||
| 48 | */ |
||
| 49 | 51 | public function __construct($debug) |
|
| 50 | { |
||
| 51 | 51 | $this->debug = (bool) $debug; |
|
| 52 | 51 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 51 | public function getConfigTreeBuilder() |
|
| 58 | { |
||
| 59 | 51 | $treeBuilder = new TreeBuilder('httplug'); |
|
| 60 | // Keep compatibility with symfony/config < 4.2 |
||
| 61 | 51 | if (!method_exists($treeBuilder, 'getRootNode')) { |
|
| 62 | $rootNode = $treeBuilder->root('httplug'); |
||
|
|
|||
| 63 | } else { |
||
| 64 | 51 | $rootNode = $treeBuilder->getRootNode(); |
|
| 65 | } |
||
| 66 | |||
| 67 | 51 | $this->configureClients($rootNode); |
|
| 68 | 51 | $this->configureSharedPlugins($rootNode); |
|
| 69 | |||
| 70 | $rootNode |
||
| 71 | 51 | ->validate() |
|
| 72 | ->ifTrue(function ($v) { |
||
| 73 | 44 | return !empty($v['classes']['client']) |
|
| 74 | 41 | || !empty($v['classes']['message_factory']) |
|
| 75 | 41 | || !empty($v['classes']['uri_factory']) |
|
| 76 | 44 | || !empty($v['classes']['stream_factory']); |
|
| 77 | 51 | }) |
|
| 78 | ->then(function ($v) { |
||
| 79 | 3 | foreach ($v['classes'] as $key => $class) { |
|
| 80 | 3 | if (null !== $class && !class_exists($class)) { |
|
| 81 | 1 | throw new InvalidConfigurationException(sprintf( |
|
| 82 | 1 | 'Class %s specified for httplug.classes.%s does not exist.', |
|
| 83 | $class, |
||
| 84 | $key |
||
| 85 | )); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | 2 | return $v; |
|
| 90 | 51 | }) |
|
| 91 | 51 | ->end() |
|
| 92 | 51 | ->beforeNormalization() |
|
| 93 | ->ifTrue(function ($v) { |
||
| 94 | 51 | return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
|
| 95 | 51 | }) |
|
| 96 | ->then(function ($v) { |
||
| 97 | 4 | if (array_key_exists('profiling', $v)) { |
|
| 98 | 1 | throw new InvalidConfigurationException('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); |
|
| 99 | } |
||
| 100 | |||
| 101 | 3 | @trigger_error('"httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use "httplug.profiling" instead.', E_USER_DEPRECATED); |
|
| 102 | |||
| 103 | 3 | if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
|
| 104 | 1 | @trigger_error('"auto" value in "httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use a boolean value instead.', E_USER_DEPRECATED); |
|
| 105 | 1 | $v['toolbar']['enabled'] = $this->debug; |
|
| 106 | } |
||
| 107 | |||
| 108 | 3 | $v['profiling'] = $v['toolbar']; |
|
| 109 | |||
| 110 | 3 | unset($v['toolbar']); |
|
| 111 | |||
| 112 | 3 | return $v; |
|
| 113 | 51 | }) |
|
| 114 | 51 | ->end() |
|
| 115 | 51 | ->fixXmlConfig('client') |
|
| 116 | 51 | ->children() |
|
| 117 | 51 | ->booleanNode('default_client_autowiring') |
|
| 118 | 51 | ->defaultTrue() |
|
| 119 | 51 | ->info('Set to false to not autowire HttpClient and HttpAsyncClient.') |
|
| 120 | 51 | ->end() |
|
| 121 | 51 | ->arrayNode('main_alias') |
|
| 122 | 51 | ->addDefaultsIfNotSet() |
|
| 123 | 51 | ->info('Configure which service the main alias point to.') |
|
| 124 | 51 | ->children() |
|
| 125 | 51 | ->scalarNode('client')->defaultValue('httplug.client.default')->end() |
|
| 126 | 51 | ->scalarNode('psr18_client')->defaultValue('httplug.psr18_client.default')->end() |
|
| 127 | 51 | ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
|
| 128 | 51 | ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
|
| 129 | 51 | ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
|
| 130 | 51 | ->scalarNode('psr17_request_factory')->defaultValue('httplug.psr17_request_factory.default')->end() |
|
| 131 | 51 | ->scalarNode('psr17_response_factory')->defaultValue('httplug.psr17_response_factory.default')->end() |
|
| 132 | 51 | ->scalarNode('psr17_stream_factory')->defaultValue('httplug.psr17_stream_factory.default')->end() |
|
| 133 | 51 | ->scalarNode('psr17_uri_factory')->defaultValue('httplug.psr17_uri_factory.default')->end() |
|
| 134 | 51 | ->scalarNode('psr17_uploaded_file_factory')->defaultValue('httplug.psr17_uploaded_file_factory.default')->end() |
|
| 135 | 51 | ->scalarNode('psr17_server_request_factory')->defaultValue('httplug.psr17_server_request_factory.default')->end() |
|
| 136 | 51 | ->end() |
|
| 137 | 51 | ->end() |
|
| 138 | 51 | ->arrayNode('classes') |
|
| 139 | 51 | ->addDefaultsIfNotSet() |
|
| 140 | 51 | ->info('Overwrite a service class instead of using the discovery mechanism.') |
|
| 141 | 51 | ->children() |
|
| 142 | 51 | ->scalarNode('client')->defaultNull()->end() |
|
| 143 | 51 | ->scalarNode('psr18_client')->defaultNull()->end() |
|
| 144 | 51 | ->scalarNode('message_factory')->defaultNull()->end() |
|
| 145 | 51 | ->scalarNode('uri_factory')->defaultNull()->end() |
|
| 146 | 51 | ->scalarNode('stream_factory')->defaultNull()->end() |
|
| 147 | 51 | ->scalarNode('psr17_request_factory')->defaultNull()->end() |
|
| 148 | 51 | ->scalarNode('psr17_response_factory')->defaultNull()->end() |
|
| 149 | 51 | ->scalarNode('psr17_stream_factory')->defaultNull()->end() |
|
| 150 | 51 | ->scalarNode('psr17_uri_factory')->defaultNull()->end() |
|
| 151 | 51 | ->scalarNode('psr17_uploaded_file_factory')->defaultNull()->end() |
|
| 152 | 51 | ->scalarNode('psr17_server_request_factory')->defaultNull()->end() |
|
| 153 | 51 | ->end() |
|
| 154 | 51 | ->end() |
|
| 155 | 51 | ->arrayNode('profiling') |
|
| 156 | 51 | ->addDefaultsIfNotSet() |
|
| 157 | 51 | ->treatFalseLike(['enabled' => false]) |
|
| 158 | 51 | ->treatTrueLike(['enabled' => true]) |
|
| 159 | 51 | ->treatNullLike(['enabled' => $this->debug]) |
|
| 160 | 51 | ->info('Extend the debug profiler with information about requests.') |
|
| 161 | 51 | ->children() |
|
| 162 | 51 | ->booleanNode('enabled') |
|
| 163 | 51 | ->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
|
| 164 | 51 | ->defaultValue($this->debug) |
|
| 165 | 51 | ->end() |
|
| 166 | 51 | ->scalarNode('formatter')->defaultNull()->end() |
|
| 167 | 51 | ->scalarNode('captured_body_length') |
|
| 168 | 51 | ->validate() |
|
| 169 | ->ifTrue(function ($v) { |
||
| 170 | 4 | return null !== $v && !is_int($v); |
|
| 171 | 51 | }) |
|
| 172 | 51 | ->thenInvalid('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null ("%s" given).') |
|
| 173 | 51 | ->end() |
|
| 174 | 51 | ->defaultValue(0) |
|
| 175 | 51 | ->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. If null the body will not be truncated. Only available with the default formatter (FullHttpMessageFormatter).') |
|
| 176 | 51 | ->end() |
|
| 177 | 51 | ->end() |
|
| 178 | 51 | ->end() |
|
| 179 | 51 | ->arrayNode('discovery') |
|
| 180 | 51 | ->addDefaultsIfNotSet() |
|
| 181 | 51 | ->info('Control what clients should be found by the discovery.') |
|
| 182 | 51 | ->children() |
|
| 183 | 51 | ->scalarNode('client') |
|
| 184 | 51 | ->defaultValue('auto') |
|
| 185 | 51 | ->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
|
| 186 | 51 | ->end() |
|
| 187 | 51 | ->scalarNode('async_client') |
|
| 188 | 51 | ->defaultNull() |
|
| 189 | 51 | ->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
|
| 190 | 51 | ->end() |
|
| 191 | 51 | ->end() |
|
| 192 | 51 | ->end() |
|
| 193 | 51 | ->end(); |
|
| 194 | |||
| 195 | 51 | return $treeBuilder; |
|
| 196 | } |
||
| 197 | |||
| 198 | 51 | private function configureClients(ArrayNodeDefinition $root) |
|
| 199 | { |
||
| 200 | 51 | $root->children() |
|
| 201 | 51 | ->arrayNode('clients') |
|
| 202 | 51 | ->useAttributeAsKey('name') |
|
| 203 | 51 | ->prototype('array') |
|
| 204 | 51 | ->fixXmlConfig('plugin') |
|
| 205 | 51 | ->validate() |
|
| 206 | ->ifTrue(function ($config) { |
||
| 207 | // Make sure we only allow one of these to be true |
||
| 208 | 27 | return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
|
| 209 | 51 | }) |
|
| 210 | 51 | ->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")') |
|
| 211 | 51 | ->end() |
|
| 212 | 51 | ->validate() |
|
| 213 | ->ifTrue(function ($config) { |
||
| 214 | 27 | return 'httplug.factory.auto' === $config['factory'] && !empty($config['config']); |
|
| 215 | 51 | }) |
|
| 216 | 51 | ->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".') |
|
| 217 | 51 | ->end() |
|
| 218 | 51 | ->validate() |
|
| 219 | ->ifTrue(function ($config) { |
||
| 220 | 27 | return !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config'])); |
|
| 221 | 51 | }) |
|
| 222 | 51 | ->thenInvalid('If you want to use the "service" key you cannot specify "factory" or "config".') |
|
| 223 | 51 | ->end() |
|
| 224 | 51 | ->children() |
|
| 225 | 51 | ->scalarNode('factory') |
|
| 226 | 51 | ->defaultValue('httplug.factory.auto') |
|
| 227 | 51 | ->cannotBeEmpty() |
|
| 228 | 51 | ->info('The service id of a factory to use when creating the adapter.') |
|
| 229 | 51 | ->end() |
|
| 230 | 51 | ->scalarNode('service') |
|
| 231 | 51 | ->defaultNull() |
|
| 232 | 51 | ->info('The service id of the client to use.') |
|
| 233 | 51 | ->end() |
|
| 234 | 51 | ->booleanNode('public') |
|
| 235 | 51 | ->defaultNull() |
|
| 236 | 51 | ->info('Set to true if you really cannot use dependency injection and need to make the client service public.') |
|
| 237 | 51 | ->end() |
|
| 238 | 51 | ->booleanNode('flexible_client') |
|
| 239 | 51 | ->defaultFalse() |
|
| 240 | 51 | ->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
|
| 241 | 51 | ->end() |
|
| 242 | 51 | ->booleanNode('http_methods_client') |
|
| 243 | 51 | ->defaultFalse() |
|
| 244 | 51 | ->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
|
| 245 | 51 | ->end() |
|
| 246 | 51 | ->booleanNode('batch_client') |
|
| 247 | 51 | ->defaultFalse() |
|
| 248 | 51 | ->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.') |
|
| 249 | 51 | ->end() |
|
| 250 | 51 | ->variableNode('config')->defaultValue([])->end() |
|
| 251 | 51 | ->append($this->createClientPluginNode()) |
|
| 252 | 51 | ->end() |
|
| 253 | 51 | ->end() |
|
| 254 | 51 | ->end(); |
|
| 255 | 51 | } |
|
| 256 | |||
| 257 | 51 | private function configureSharedPlugins(ArrayNodeDefinition $root) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Createplugins node of a client. |
||
| 271 | * |
||
| 272 | * @return ArrayNodeDefinition The plugin node |
||
| 273 | */ |
||
| 274 | 51 | private function createClientPluginNode() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Add the definitions for shared plugin configurations. |
||
| 509 | * |
||
| 510 | * @param ArrayNodeDefinition $pluginNode the node to add to |
||
| 511 | * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
||
| 512 | */ |
||
| 513 | 51 | private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Create configuration for authentication plugin. |
||
| 607 | * |
||
| 608 | * @return NodeDefinition definition for the authentication node in the plugins list |
||
| 609 | */ |
||
| 610 | 51 | private function createAuthenticationPluginNode() |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Validate that the configuration fragment has the specified keys and none other. |
||
| 672 | * |
||
| 673 | * @param array $expected Fields that must exist |
||
| 674 | * @param array $actual Actual configuration hashmap |
||
| 675 | * @param string $authName Name of authentication method for error messages |
||
| 676 | * |
||
| 677 | * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
||
| 678 | */ |
||
| 679 | 8 | private function validateAuthenticationType(array $expected, array $actual, $authName) |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Create configuration for cache plugin. |
||
| 704 | * |
||
| 705 | * @return NodeDefinition definition for the cache node in the plugins list |
||
| 706 | */ |
||
| 707 | 51 | private function createCachePluginNode() |
|
| 842 | } |
||
| 843 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.