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 |
||
| 23 | class Feeds extends Admin_Page { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Feed types. |
||
| 27 | * |
||
| 28 | * @access private |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $feed_types = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor. |
||
| 35 | * |
||
| 36 | * @since 3.0.0 |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function __construct() { |
|
|
|
|||
| 39 | |||
| 40 | $this->id = 'feeds'; |
||
| 41 | $this->option_group = 'settings'; |
||
| 42 | $this->label = __( 'Event Sources', 'google-calendar-events' ); |
||
| 43 | //$this->description = __( 'Manage calendar event sources settings.', 'google-calendar-events' ); |
||
| 44 | |||
| 45 | $feeds_settings = array(); |
||
| 46 | $feeds = simcal_get_feed_types(); |
||
| 47 | if ( ! empty( $feeds ) && is_array( $feeds ) ) { |
||
| 48 | foreach ( $feeds as $feed ) { |
||
| 49 | |||
| 50 | $feed_type = simcal_get_feed( $feed ); |
||
| 51 | |||
| 52 | if ( $feed_type instanceof Feed ) { |
||
| 53 | $settings = $feed_type->settings_fields(); |
||
| 54 | if ( ! empty( $settings ) ) { |
||
| 55 | $feeds_settings[ $feed ] = $settings; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->feed_types = $feeds_settings; |
||
| 62 | $this->sections = $this->add_sections(); |
||
| 63 | $this->fields = $this->add_fields(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add sections. |
||
| 68 | * |
||
| 69 | * @since 3.0.0 |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function add_sections() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add fields. |
||
| 93 | * |
||
| 94 | * @since 3.0.0 |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function add_fields() { |
||
| 122 | |||
| 123 | } |
||
| 124 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.