Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 32 | final class Configuration implements ConfigurationInterface |
||
| 33 | { |
||
| 34 | private $debug; |
||
| 35 | |||
| 36 | 61 | public function __construct(bool $debug) |
|
| 40 | |||
| 41 | 60 | public function getConfigTreeBuilder(): TreeBuilder |
|
| 42 | { |
||
| 43 | 60 | $treeBuilder = new TreeBuilder('fos_rest'); |
|
| 44 | |||
| 45 | 60 | $rootNode = $treeBuilder->getRootNode(); |
|
| 46 | |||
| 47 | $rootNode |
||
| 48 | 60 | ->children() |
|
| 49 | 60 | ->scalarNode('disable_csrf_role')->defaultNull()->end() |
|
| 50 | 60 | ->scalarNode('unauthorized_challenge')->defaultNull()->end() |
|
| 51 | 60 | ->arrayNode('param_fetcher_listener') |
|
| 52 | 60 | ->beforeNormalization() |
|
| 53 | 60 | ->ifString() |
|
| 54 | View Code Duplication | ->then(function ($v) { |
|
| 55 | 1 | return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; |
|
| 56 | 60 | }) |
|
| 57 | 60 | ->end() |
|
| 58 | 60 | ->canBeEnabled() |
|
| 59 | 60 | ->children() |
|
| 60 | 60 | ->booleanNode('force')->defaultFalse()->end() |
|
| 61 | 60 | ->scalarNode('service')->defaultNull()->end() |
|
| 62 | 60 | ->end() |
|
| 63 | 60 | ->end() |
|
| 64 | 60 | ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end() |
|
| 65 | 60 | ->arrayNode('allowed_methods_listener') |
|
| 66 | 60 | ->canBeEnabled() |
|
| 67 | 60 | ->children() |
|
| 68 | 60 | ->scalarNode('service')->defaultNull()->end() |
|
| 69 | 60 | ->end() |
|
| 70 | 60 | ->end() |
|
| 71 | 60 | ->booleanNode('routing_loader') |
|
| 72 | 60 | ->defaultValue(false) |
|
| 73 | 60 | ->validate() |
|
| 74 | 60 | ->ifTrue() |
|
| 75 | 60 | ->thenInvalid('only "false" is supported') |
|
| 76 | 60 | ->end() |
|
| 77 | 60 | ->end() |
|
| 78 | 60 | ->arrayNode('body_converter') |
|
| 79 | 60 | ->canBeEnabled() |
|
| 80 | 60 | ->children() |
|
| 81 | 60 | ->scalarNode('validate') |
|
| 82 | 60 | ->defaultFalse() |
|
| 83 | 60 | ->beforeNormalization() |
|
| 84 | 60 | ->ifTrue() |
|
| 85 | ->then(function ($value) { |
||
| 86 | 1 | if (!class_exists(OptionsResolver::class)) { |
|
| 87 | throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )"); |
||
| 88 | } |
||
| 89 | |||
| 90 | 1 | return $value; |
|
| 91 | 60 | }) |
|
| 92 | 60 | ->end() |
|
| 93 | 60 | ->end() |
|
| 94 | 60 | ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end() |
|
| 95 | 60 | ->end() |
|
| 96 | 60 | ->end() |
|
| 97 | 60 | ->arrayNode('service') |
|
| 98 | 60 | ->addDefaultsIfNotSet() |
|
| 99 | 60 | ->children() |
|
| 100 | 60 | ->scalarNode('serializer')->defaultNull()->end() |
|
| 101 | 60 | ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end() |
|
| 102 | 60 | ->scalarNode('validator')->defaultValue('validator')->end() |
|
| 103 | 60 | ->end() |
|
| 104 | 60 | ->end() |
|
| 105 | 60 | ->arrayNode('serializer') |
|
| 106 | 60 | ->addDefaultsIfNotSet() |
|
| 107 | 60 | ->children() |
|
| 108 | 60 | ->scalarNode('version')->defaultNull()->end() |
|
| 109 | 60 | ->arrayNode('groups') |
|
| 110 | 60 | ->prototype('scalar')->end() |
|
| 111 | 60 | ->end() |
|
| 112 | 60 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
| 113 | 60 | ->booleanNode('disable_custom_jms_registry')->defaultFalse()->end() |
|
| 114 | 60 | ->end() |
|
| 115 | 60 | ->end() |
|
| 116 | 60 | ->arrayNode('zone') |
|
| 117 | 60 | ->cannotBeOverwritten() |
|
| 118 | 60 | ->prototype('array') |
|
| 119 | 60 | ->fixXmlConfig('ip') |
|
| 120 | 60 | ->children() |
|
| 121 | 60 | ->scalarNode('path') |
|
| 122 | 60 | ->defaultNull() |
|
| 123 | 60 | ->info('use the urldecoded format') |
|
| 124 | 60 | ->example('^/path to resource/') |
|
| 125 | 60 | ->end() |
|
| 126 | 60 | ->scalarNode('host')->defaultNull()->end() |
|
| 127 | 60 | ->arrayNode('methods') |
|
| 128 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
| 129 | return preg_split('/\s*,\s*/', $v); |
||
| 130 | 60 | })->end() |
|
| 131 | 60 | ->prototype('scalar')->end() |
|
| 132 | 60 | ->end() |
|
| 133 | 60 | ->arrayNode('ips') |
|
| 134 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
| 135 | 1 | return array($v); |
|
| 136 | 60 | })->end() |
|
| 137 | 60 | ->prototype('scalar')->end() |
|
| 138 | 60 | ->end() |
|
| 139 | 60 | ->end() |
|
| 140 | 60 | ->end() |
|
| 141 | 60 | ->end() |
|
| 142 | 60 | ->end(); |
|
| 143 | |||
| 144 | 60 | $this->addViewSection($rootNode); |
|
|
|
|||
| 145 | 60 | $this->addExceptionSection($rootNode); |
|
| 146 | 60 | $this->addBodyListenerSection($rootNode); |
|
| 147 | 60 | $this->addFormatListenerSection($rootNode); |
|
| 148 | 60 | $this->addVersioningSection($rootNode); |
|
| 149 | |||
| 150 | 60 | return $treeBuilder; |
|
| 151 | } |
||
| 152 | |||
| 153 | 60 | private function addViewSection(ArrayNodeDefinition $rootNode): void |
|
| 154 | { |
||
| 155 | $rootNode |
||
| 156 | 60 | ->children() |
|
| 157 | 60 | ->arrayNode('view') |
|
| 158 | 60 | ->fixXmlConfig('format', 'formats') |
|
| 159 | 60 | ->fixXmlConfig('mime_type', 'mime_types') |
|
| 160 | 60 | ->addDefaultsIfNotSet() |
|
| 161 | 60 | ->children() |
|
| 162 | 60 | ->arrayNode('mime_types') |
|
| 163 | 60 | ->canBeEnabled() |
|
| 164 | 60 | ->beforeNormalization() |
|
| 165 | ->ifArray()->then(function ($v) { |
||
| 166 | 1 | if (!empty($v) && empty($v['formats'])) { |
|
| 167 | 1 | unset($v['enabled']); |
|
| 168 | 1 | $v = ['enabled' => true, 'formats' => $v]; |
|
| 169 | } |
||
| 170 | |||
| 171 | 1 | return $v; |
|
| 172 | 60 | }) |
|
| 173 | 60 | ->end() |
|
| 174 | 60 | ->fixXmlConfig('format', 'formats') |
|
| 175 | 60 | ->children() |
|
| 176 | 60 | ->scalarNode('service')->defaultNull()->end() |
|
| 177 | 60 | ->arrayNode('formats') |
|
| 178 | 60 | ->useAttributeAsKey('name') |
|
| 179 | 60 | ->prototype('array') |
|
| 180 | 60 | ->beforeNormalization() |
|
| 181 | 60 | ->ifString() |
|
| 182 | ->then(function ($v) { return array($v); }) |
||
| 183 | 60 | ->end() |
|
| 184 | 60 | ->prototype('scalar')->end() |
|
| 185 | 60 | ->end() |
|
| 186 | 60 | ->end() |
|
| 187 | 60 | ->end() |
|
| 188 | 60 | ->end() |
|
| 189 | 60 | ->arrayNode('formats') |
|
| 190 | 60 | ->useAttributeAsKey('name') |
|
| 191 | 60 | ->defaultValue(['json' => true, 'xml' => true]) |
|
| 192 | 60 | ->prototype('boolean')->end() |
|
| 193 | 60 | ->end() |
|
| 194 | 60 | ->arrayNode('view_response_listener') |
|
| 195 | 60 | ->beforeNormalization() |
|
| 196 | 60 | ->ifString() |
|
| 197 | View Code Duplication | ->then(function ($v) { |
|
| 198 | 4 | return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; |
|
| 199 | 60 | }) |
|
| 200 | 60 | ->end() |
|
| 201 | 60 | ->canBeEnabled() |
|
| 202 | 60 | ->children() |
|
| 203 | 60 | ->booleanNode('force')->defaultFalse()->end() |
|
| 204 | 60 | ->scalarNode('service')->defaultNull()->end() |
|
| 205 | 60 | ->end() |
|
| 206 | 60 | ->end() |
|
| 207 | 60 | ->scalarNode('failed_validation')->defaultValue(Response::HTTP_BAD_REQUEST)->end() |
|
| 208 | 60 | ->scalarNode('empty_content')->defaultValue(Response::HTTP_NO_CONTENT)->end() |
|
| 209 | 60 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
| 210 | 60 | ->arrayNode('jsonp_handler') |
|
| 211 | 60 | ->canBeUnset() |
|
| 212 | 60 | ->children() |
|
| 213 | 60 | ->scalarNode('callback_param')->defaultValue('callback')->end() |
|
| 214 | 60 | ->scalarNode('mime_type')->defaultValue('application/javascript+jsonp')->end() |
|
| 215 | 60 | ->end() |
|
| 216 | 60 | ->end() |
|
| 217 | 60 | ->end() |
|
| 218 | 60 | ->end() |
|
| 219 | 60 | ->end(); |
|
| 220 | 60 | } |
|
| 221 | |||
| 222 | 60 | private function addBodyListenerSection(ArrayNodeDefinition $rootNode): void |
|
| 262 | |||
| 263 | 60 | private function addFormatListenerSection(ArrayNodeDefinition $rootNode): void |
|
| 264 | { |
||
| 316 | |||
| 317 | 60 | private function addVersioningSection(ArrayNodeDefinition $rootNode): void |
|
| 366 | |||
| 367 | 60 | private function addExceptionSection(ArrayNodeDefinition $rootNode): void |
|
| 454 | |||
| 455 | 14 | private function testExceptionExists(string $throwable): void |
|
| 461 | } |
||
| 462 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.