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 | /** |
||
| 35 | * Default debug mode value. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private $debug; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param bool $debug |
||
| 43 | */ |
||
| 44 | 65 | public function __construct($debug) |
|
| 45 | { |
||
| 46 | 65 | $this->debug = (bool) $debug; |
|
| 47 | 65 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Generates the configuration tree. |
||
| 51 | * |
||
| 52 | * @return TreeBuilder |
||
| 53 | */ |
||
| 54 | 64 | public function getConfigTreeBuilder() |
|
| 55 | { |
||
| 56 | 64 | $treeBuilder = new TreeBuilder('fos_rest'); |
|
| 57 | |||
| 58 | 64 | $rootNode = $treeBuilder->getRootNode(); |
|
| 59 | |||
| 60 | $rootNode |
||
| 61 | 64 | ->children() |
|
| 62 | 64 | ->scalarNode('disable_csrf_role')->defaultNull()->end() |
|
| 63 | 64 | ->arrayNode('access_denied_listener') |
|
| 64 | 64 | ->canBeEnabled() |
|
| 65 | 64 | ->beforeNormalization() |
|
| 66 | View Code Duplication | ->ifArray()->then(function ($v) { |
|
| 67 | if (!empty($v) && empty($v['formats'])) { |
||
| 68 | unset($v['enabled']); |
||
| 69 | $v = ['enabled' => true, 'formats' => $v]; |
||
| 70 | } |
||
| 71 | |||
| 72 | return $v; |
||
| 73 | 64 | }) |
|
| 74 | 64 | ->end() |
|
| 75 | 64 | ->fixXmlConfig('format', 'formats') |
|
| 76 | 64 | ->children() |
|
| 77 | 64 | ->scalarNode('service')->defaultNull()->end() |
|
| 78 | 64 | ->arrayNode('formats') |
|
| 79 | 64 | ->useAttributeAsKey('name') |
|
| 80 | 64 | ->prototype('boolean')->end() |
|
| 81 | 64 | ->end() |
|
| 82 | 64 | ->end() |
|
| 83 | 64 | ->end() |
|
| 84 | 64 | ->scalarNode('unauthorized_challenge')->defaultNull()->end() |
|
| 85 | 64 | ->arrayNode('param_fetcher_listener') |
|
| 86 | 64 | ->beforeNormalization() |
|
| 87 | 64 | ->ifString() |
|
| 88 | View Code Duplication | ->then(function ($v) { |
|
| 89 | 1 | return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; |
|
| 90 | 64 | }) |
|
| 91 | 64 | ->end() |
|
| 92 | 64 | ->canBeEnabled() |
|
| 93 | 64 | ->children() |
|
| 94 | 64 | ->booleanNode('force')->defaultFalse()->end() |
|
| 95 | 64 | ->scalarNode('service')->defaultNull()->end() |
|
| 96 | 64 | ->end() |
|
| 97 | 64 | ->end() |
|
| 98 | 64 | ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end() |
|
| 99 | 64 | ->arrayNode('allowed_methods_listener') |
|
| 100 | 64 | ->canBeEnabled() |
|
| 101 | 64 | ->children() |
|
| 102 | 64 | ->scalarNode('service')->defaultNull()->end() |
|
| 103 | 64 | ->end() |
|
| 104 | 64 | ->end() |
|
| 105 | 64 | ->arrayNode('routing_loader') |
|
| 106 | 64 | ->addDefaultsIfNotSet() |
|
| 107 | 64 | ->children() |
|
| 108 | 64 | ->booleanNode('parse_controller_name') |
|
| 109 | 64 | ->defaultValue(false) |
|
| 110 | 64 | ->end() |
|
| 111 | 64 | ->scalarNode('default_format')->defaultNull()->end() |
|
| 112 | 64 | ->scalarNode('prefix_methods')->defaultTrue()->end() |
|
| 113 | 64 | ->scalarNode('include_format')->defaultTrue()->end() |
|
| 114 | 64 | ->end() |
|
| 115 | 64 | ->end() |
|
| 116 | 64 | ->arrayNode('body_converter') |
|
| 117 | 64 | ->canBeEnabled() |
|
| 118 | 64 | ->children() |
|
| 119 | 64 | ->scalarNode('validate') |
|
| 120 | 64 | ->defaultFalse() |
|
| 121 | 64 | ->beforeNormalization() |
|
| 122 | 64 | ->ifTrue() |
|
| 123 | ->then(function ($value) { |
||
| 124 | 1 | if (!class_exists(OptionsResolver::class)) { |
|
| 125 | throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )"); |
||
| 126 | } |
||
| 127 | |||
| 128 | 1 | return $value; |
|
| 129 | 64 | }) |
|
| 130 | 64 | ->end() |
|
| 131 | 64 | ->end() |
|
| 132 | 64 | ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end() |
|
| 133 | 64 | ->end() |
|
| 134 | 64 | ->end() |
|
| 135 | 64 | ->arrayNode('service') |
|
| 136 | 64 | ->addDefaultsIfNotSet() |
|
| 137 | 64 | ->children() |
|
| 138 | 64 | ->scalarNode('router')->defaultValue('router')->end() |
|
| 139 | 64 | ->scalarNode('templating') |
|
| 140 | 64 | ->defaultNull() |
|
| 141 | 64 | ->validate() |
|
| 142 | 64 | ->ifString() |
|
| 143 | 64 | ->thenInvalid('only null is supported') |
|
| 144 | 64 | ->end() |
|
| 145 | 64 | ->end() |
|
| 146 | 64 | ->scalarNode('serializer')->defaultNull()->end() |
|
| 147 | 64 | ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end() |
|
| 148 | 64 | ->scalarNode('inflector')->defaultValue('fos_rest.inflector.doctrine')->end() |
|
| 149 | 64 | ->scalarNode('validator')->defaultValue('validator')->end() |
|
| 150 | 64 | ->end() |
|
| 151 | 64 | ->end() |
|
| 152 | 64 | ->arrayNode('serializer') |
|
| 153 | 64 | ->addDefaultsIfNotSet() |
|
| 154 | 64 | ->children() |
|
| 155 | 64 | ->scalarNode('version')->defaultNull()->end() |
|
| 156 | 64 | ->arrayNode('groups') |
|
| 157 | 64 | ->prototype('scalar')->end() |
|
| 158 | 64 | ->end() |
|
| 159 | 64 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
| 160 | 64 | ->end() |
|
| 161 | 64 | ->end() |
|
| 162 | 64 | ->arrayNode('zone') |
|
| 163 | 64 | ->cannotBeOverwritten() |
|
| 164 | 64 | ->prototype('array') |
|
| 165 | 64 | ->fixXmlConfig('ip') |
|
| 166 | 64 | ->children() |
|
| 167 | 64 | ->scalarNode('path') |
|
| 168 | 64 | ->defaultNull() |
|
| 169 | 64 | ->info('use the urldecoded format') |
|
| 170 | 64 | ->example('^/path to resource/') |
|
| 171 | 64 | ->end() |
|
| 172 | 64 | ->scalarNode('host')->defaultNull()->end() |
|
| 173 | 64 | ->arrayNode('methods') |
|
| 174 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
| 175 | return preg_split('/\s*,\s*/', $v); |
||
| 176 | 64 | })->end() |
|
| 177 | 64 | ->prototype('scalar')->end() |
|
| 178 | 64 | ->end() |
|
| 179 | 64 | ->arrayNode('ips') |
|
| 180 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
| 181 | 1 | return array($v); |
|
| 182 | 64 | })->end() |
|
| 183 | 64 | ->prototype('scalar')->end() |
|
| 184 | 64 | ->end() |
|
| 185 | 64 | ->end() |
|
| 186 | 64 | ->end() |
|
| 187 | 64 | ->end() |
|
| 188 | 64 | ->end(); |
|
| 189 | |||
| 190 | 64 | $this->addViewSection($rootNode); |
|
|
|
|||
| 191 | 64 | $this->addExceptionSection($rootNode); |
|
| 192 | 64 | $this->addBodyListenerSection($rootNode); |
|
| 193 | 64 | $this->addFormatListenerSection($rootNode); |
|
| 194 | 64 | $this->addVersioningSection($rootNode); |
|
| 195 | |||
| 196 | 64 | return $treeBuilder; |
|
| 197 | } |
||
| 198 | |||
| 199 | 64 | private function addViewSection(ArrayNodeDefinition $rootNode) |
|
| 284 | |||
| 285 | 64 | private function addBodyListenerSection(ArrayNodeDefinition $rootNode) |
|
| 286 | { |
||
| 325 | |||
| 326 | 64 | private function addFormatListenerSection(ArrayNodeDefinition $rootNode) |
|
| 379 | |||
| 380 | 64 | private function addVersioningSection(ArrayNodeDefinition $rootNode) |
|
| 429 | |||
| 430 | 64 | private function addExceptionSection(ArrayNodeDefinition $rootNode) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Checks if an exception is loadable. |
||
| 499 | * |
||
| 500 | * @param string $exception Class to test |
||
| 501 | * |
||
| 502 | * @throws InvalidConfigurationException if the class was not found |
||
| 503 | */ |
||
| 504 | 8 | private function testExceptionExists($exception) |
|
| 510 | } |
||
| 511 |
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.