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 |
||
| 10 | class Container_Condition_Provider implements ServiceProviderInterface { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Install dependencies in IoC container |
||
| 14 | * |
||
| 15 | * @param PimpleContainer $ioc |
||
| 16 | */ |
||
| 17 | public function register( PimpleContainer $ioc ) { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Install all codition types and the condition factory |
||
| 26 | * |
||
| 27 | * @param PimpleContainer $ioc |
||
| 28 | */ |
||
| 29 | protected function install_conditions( $ioc ) { |
||
| 30 | $ioc['container_condition_factory'] = function( $ioc ) { |
||
| 31 | return new ConditionFactory( $ioc['container_conditions'] ); |
||
| 32 | }; |
||
| 33 | |||
| 34 | $ioc['container_condition_fulfillable_collection'] = $ioc->factory( function( $ioc ) { |
||
| 35 | return new Fulfillable_Collection( $ioc['container_condition_factory'], $ioc['container_condition_translator_array'] ); |
||
| 36 | } ); |
||
| 37 | |||
| 38 | $ioc['container_conditions'] = function() { |
||
| 39 | return new PimpleContainer(); |
||
| 40 | }; |
||
| 41 | |||
| 42 | $cc_ioc = $ioc['container_conditions']; |
||
| 43 | |||
| 44 | $cc_ioc['boolean'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 45 | $condition = new \Carbon_Fields\Container\Condition\Boolean_Condition(); |
||
| 46 | $condition->set_comparers( array( |
||
| 47 | $ioc['container_condition_comparers']['equality'], |
||
| 48 | ) ); |
||
| 49 | return $condition; |
||
| 50 | } ); |
||
| 51 | |||
| 52 | $cc_ioc['post_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 53 | $condition = new \Carbon_Fields\Container\Condition\Post_ID_Condition(); |
||
| 54 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 55 | return $condition; |
||
| 56 | } ); |
||
| 57 | $cc_ioc['post_parent_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 58 | $condition = new \Carbon_Fields\Container\Condition\Post_Parent_ID_Condition(); |
||
| 59 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 60 | return $condition; |
||
| 61 | } ); |
||
| 62 | $cc_ioc['post_ancestor_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 63 | $condition = new \Carbon_Fields\Container\Condition\Post_Ancestor_ID_Condition(); |
||
| 64 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['array'] ); |
||
| 65 | return $condition; |
||
| 66 | } ); |
||
| 67 | $cc_ioc['post_type'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 68 | $condition = new \Carbon_Fields\Container\Condition\Post_Type_Condition(); |
||
| 69 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['nonscalar'] ); |
||
| 70 | return $condition; |
||
| 71 | } ); |
||
| 72 | $cc_ioc['post_format'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 73 | $condition = new \Carbon_Fields\Container\Condition\Post_Format_Condition(); |
||
| 74 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['nonscalar'] ); |
||
| 75 | return $condition; |
||
| 76 | } ); |
||
| 77 | $cc_ioc['post_level'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 78 | $condition = new \Carbon_Fields\Container\Condition\Post_Level_Condition(); |
||
| 79 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 80 | return $condition; |
||
| 81 | } ); |
||
| 82 | $cc_ioc['post_template'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 83 | $condition = new \Carbon_Fields\Container\Condition\Post_Template_Condition(); |
||
| 84 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['nonscalar'] ); |
||
| 85 | return $condition; |
||
| 86 | } ); |
||
| 87 | View Code Duplication | $cc_ioc['post_term'] = $cc_ioc->factory( function() use ( $ioc ) { |
|
| 88 | $condition = new \Carbon_Fields\Container\Condition\Post_Term_Condition( $ioc['wp_toolset'] ); |
||
| 89 | $condition->set_comparers( array( |
||
| 90 | // Only support the custom comparer as this condition has its own comparison methods |
||
| 91 | $ioc['container_condition_comparers']['custom'], |
||
| 92 | ) ); |
||
| 93 | return $condition; |
||
| 94 | } ); |
||
| 95 | |||
| 96 | View Code Duplication | $cc_ioc['term'] = $cc_ioc->factory( function() use ( $ioc ) { |
|
| 97 | $condition = new \Carbon_Fields\Container\Condition\Term_Condition( $ioc['wp_toolset'] ); |
||
| 98 | $condition->set_comparers( array( |
||
| 99 | // Only support the custom comparer as this condition has its own comparison methods |
||
| 100 | $ioc['container_condition_comparers']['custom'], |
||
| 101 | ) ); |
||
| 102 | return $condition; |
||
| 103 | } ); |
||
| 104 | $cc_ioc['term_taxonomy'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 105 | $condition = new \Carbon_Fields\Container\Condition\Term_Taxonomy_Condition(); |
||
| 106 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['nonscalar'] ); |
||
| 107 | return $condition; |
||
| 108 | } ); |
||
| 109 | $cc_ioc['term_level'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 110 | $condition = new \Carbon_Fields\Container\Condition\Term_Level_Condition(); |
||
| 111 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 112 | return $condition; |
||
| 113 | } ); |
||
| 114 | $cc_ioc['term_parent'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 115 | $condition = new \Carbon_Fields\Container\Condition\Term_Parent_Condition( $ioc['wp_toolset'] ); |
||
| 116 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 117 | return $condition; |
||
| 118 | } ); |
||
| 119 | $cc_ioc['term_ancestor'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 120 | $condition = new \Carbon_Fields\Container\Condition\Term_Ancestor_Condition( $ioc['wp_toolset'] ); |
||
| 121 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['array'] ); |
||
| 122 | return $condition; |
||
| 123 | } ); |
||
| 124 | |||
| 125 | $cc_ioc['user_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 126 | $condition = new \Carbon_Fields\Container\Condition\User_ID_Condition(); |
||
| 127 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 128 | return $condition; |
||
| 129 | } ); |
||
| 130 | $cc_ioc['user_role'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 131 | $condition = new \Carbon_Fields\Container\Condition\User_Role_Condition(); |
||
| 132 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['array'] ); |
||
| 133 | return $condition; |
||
| 134 | } ); |
||
| 135 | View Code Duplication | $cc_ioc['user_capability'] = $cc_ioc->factory( function() use ( $ioc ) { |
|
| 136 | $condition = new \Carbon_Fields\Container\Condition\User_Capability_Condition(); |
||
| 137 | $condition->set_comparers( array( |
||
| 138 | // Only support the custom comparer as this condition has its own comparison methods |
||
| 139 | $ioc['container_condition_comparers']['custom'], |
||
| 140 | ) ); |
||
| 141 | return $condition; |
||
| 142 | } ); |
||
| 143 | |||
| 144 | $cc_ioc['blog_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 145 | $condition = new \Carbon_Fields\Container\Condition\Blog_ID_Condition(); |
||
| 146 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 147 | return $condition; |
||
| 148 | } ); |
||
| 149 | |||
| 150 | $cc_ioc['current_user_id'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 151 | $condition = new \Carbon_Fields\Container\Condition\Current_User_ID_Condition(); |
||
| 152 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] ); |
||
| 153 | return $condition; |
||
| 154 | } ); |
||
| 155 | $cc_ioc['current_user_role'] = $cc_ioc->factory( function() use ( $ioc ) { |
||
| 156 | $condition = new \Carbon_Fields\Container\Condition\Current_User_Role_Condition(); |
||
| 157 | $condition->set_comparers( $ioc['container_condition_comparer_collections']['array'] ); |
||
| 158 | return $condition; |
||
| 159 | } ); |
||
| 160 | View Code Duplication | $cc_ioc['current_user_capability'] = $cc_ioc->factory( function() use ( $ioc ) { |
|
| 161 | $condition = new \Carbon_Fields\Container\Condition\Current_User_Capability_Condition(); |
||
| 162 | $condition->set_comparers( array( |
||
| 163 | // Only support the custom comparer as this condition has its own comparison methods |
||
| 164 | $ioc['container_condition_comparers']['custom'], |
||
| 165 | ) ); |
||
| 166 | return $condition; |
||
| 167 | } ); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Install all condition comparers |
||
| 172 | * |
||
| 173 | * @param PimpleContainer $ioc |
||
| 174 | */ |
||
| 175 | protected function install_comparers( $ioc ) { |
||
| 176 | $ioc['container_condition_comparers'] = function() { |
||
| 177 | return new PimpleContainer(); |
||
| 178 | }; |
||
| 179 | |||
| 180 | $ioc['container_condition_comparers']['equality'] = function() { |
||
| 181 | return new \Carbon_Fields\Container\Condition\Comparer\Equality_Comparer(); |
||
| 182 | }; |
||
| 183 | |||
| 184 | $ioc['container_condition_comparers']['any_equality'] = function() { |
||
| 185 | return new \Carbon_Fields\Container\Condition\Comparer\Any_Equality_Comparer(); |
||
| 186 | }; |
||
| 187 | |||
| 188 | $ioc['container_condition_comparers']['contain'] = function() { |
||
| 189 | return new \Carbon_Fields\Container\Condition\Comparer\Contain_Comparer(); |
||
| 190 | }; |
||
| 191 | |||
| 192 | $ioc['container_condition_comparers']['any_contain'] = function() { |
||
| 193 | return new \Carbon_Fields\Container\Condition\Comparer\Any_Contain_Comparer(); |
||
| 194 | }; |
||
| 195 | |||
| 196 | $ioc['container_condition_comparers']['scalar'] = function() { |
||
| 197 | return new \Carbon_Fields\Container\Condition\Comparer\Scalar_Comparer(); |
||
| 198 | }; |
||
| 199 | |||
| 200 | $ioc['container_condition_comparers']['custom'] = function() { |
||
| 201 | return new \Carbon_Fields\Container\Condition\Comparer\Custom_Comparer(); |
||
| 202 | }; |
||
| 203 | |||
| 204 | $ioc['container_condition_comparer_collections'] = function() { |
||
| 205 | return new PimpleContainer(); |
||
| 206 | }; |
||
| 207 | |||
| 208 | $ioc['container_condition_comparer_collections']['generic'] = function() use ( $ioc ) { |
||
| 209 | return array( |
||
| 210 | $ioc['container_condition_comparers']['equality'], |
||
| 211 | $ioc['container_condition_comparers']['contain'], |
||
| 212 | $ioc['container_condition_comparers']['scalar'], |
||
| 213 | $ioc['container_condition_comparers']['custom'], |
||
| 214 | ); |
||
| 215 | }; |
||
| 216 | $ioc['container_condition_comparer_collections']['nonscalar'] = function() use ( $ioc ) { |
||
| 217 | return array( |
||
| 218 | $ioc['container_condition_comparers']['equality'], |
||
| 219 | $ioc['container_condition_comparers']['contain'], |
||
| 220 | $ioc['container_condition_comparers']['custom'], |
||
| 221 | ); |
||
| 222 | }; |
||
| 223 | $ioc['container_condition_comparer_collections']['array'] = function() use ( $ioc ) { |
||
| 224 | return array( |
||
| 225 | $ioc['container_condition_comparers']['any_equality'], |
||
| 226 | $ioc['container_condition_comparers']['any_contain'], |
||
| 227 | $ioc['container_condition_comparers']['custom'], |
||
| 228 | ); |
||
| 229 | }; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Install all codition translators |
||
| 234 | * |
||
| 235 | * @param PimpleContainer $ioc |
||
| 236 | */ |
||
| 237 | protected static function install_translators( $ioc ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Install all container coditions |
||
| 249 | * |
||
| 250 | * @param PimpleContainer $ioc |
||
| 251 | */ |
||
| 252 | protected function install_container_conditions( $ioc ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Filter the Post_Meta_Container static condition types |
||
| 276 | * |
||
| 277 | * @param array<string> $condition_types |
||
| 278 | * @param \Carbon_Fields\Container\Container $container |
||
| 279 | * @return array<string> |
||
| 280 | */ |
||
| 281 | public function filter_post_meta_container_static_condition_types( $condition_types, $container_type, $container ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Filter the Post_Meta_Container dynamic condition types |
||
| 290 | * |
||
| 291 | * @param array<string> $condition_types |
||
| 292 | * @param string $container_type |
||
| 293 | * @param \Carbon_Fields\Container\Container $container |
||
| 294 | * @return array<string> |
||
| 295 | */ |
||
| 296 | public function filter_post_meta_container_dynamic_condition_types( $condition_types, $container_type, $container ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Filter the Term_Meta_Container static condition types |
||
| 305 | * |
||
| 306 | * @param array<string> $condition_types |
||
| 307 | * @param string $container_type |
||
| 308 | * @param \Carbon_Fields\Container\Container $container |
||
| 309 | * @return array<string> |
||
| 310 | */ |
||
| 311 | public function filter_term_meta_container_static_condition_types( $condition_types, $container_type, $container ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Filter the Term_Meta_Container dynamic condition types |
||
| 320 | * |
||
| 321 | * @param array<string> $condition_types |
||
| 322 | * @param string $container_type |
||
| 323 | * @param \Carbon_Fields\Container\Container $container |
||
| 324 | * @return array<string> |
||
| 325 | */ |
||
| 326 | public function filter_term_meta_container_dynamic_condition_types( $condition_types, $container_type, $container ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Filter the User_Meta_Container static condition types |
||
| 335 | * |
||
| 336 | * @param array<string> $condition_types |
||
| 337 | * @param string $container_type |
||
| 338 | * @param \Carbon_Fields\Container\Container $container |
||
| 339 | * @return array<string> |
||
| 340 | */ |
||
| 341 | public function filter_user_meta_container_static_condition_types( $condition_types, $container_type, $container ) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Filter the User_Meta_Container dynamic condition types |
||
| 350 | * |
||
| 351 | * @param array<string> $condition_types |
||
| 352 | * @param string $container_type |
||
| 353 | * @param \Carbon_Fields\Container\Container $container |
||
| 354 | * @return array<string> |
||
| 355 | */ |
||
| 356 | public function filter_user_meta_container_dynamic_condition_types( $condition_types, $container_type, $container ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Filter the Theme_Options_Container static condition types |
||
| 365 | * |
||
| 366 | * @param array<string> $condition_types |
||
| 367 | * @param string $container_type |
||
| 368 | * @param \Carbon_Fields\Container\Container $container |
||
| 369 | * @return array<string> |
||
| 370 | */ |
||
| 371 | public function filter_theme_options_container_static_condition_types( $condition_types, $container_type, $container ) { |
||
| 377 | } |
||
| 378 |