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:
Complex classes like Objects 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 Objects, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Objects { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor. |
||
| 26 | * |
||
| 27 | * Add default objects. |
||
| 28 | * |
||
| 29 | * @since 3.0.0 |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get feed types. |
||
| 75 | * |
||
| 76 | * @since 3.0.0 |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function get_feed_types() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get calendar types. |
||
| 88 | * |
||
| 89 | * @since 3.0.0 |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function get_calendar_types() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get admin pages. |
||
| 101 | * |
||
| 102 | * @since 3.0.0 |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function get_admin_pages() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get a calendar. |
||
| 112 | * |
||
| 113 | * Returns the right type of calendar. |
||
| 114 | * |
||
| 115 | * @since 3.0.0 |
||
| 116 | * |
||
| 117 | * @param int|string|object|\WP_Post|Object\Calendar $object |
||
| 118 | * |
||
| 119 | * @return null|Object\Calendar |
||
|
|
|||
| 120 | */ |
||
| 121 | public function get_calendar( $object ) { |
||
| 122 | |||
| 123 | View Code Duplication | if ( is_string( $object ) ) { |
|
| 124 | return ! empty( $object ) ? $this->get_object( $object, 'calendar', '' ) : null; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ( is_object( $object ) ) { |
||
| 128 | if ( $object instanceof Object\Calendar ) { |
||
| 129 | return $this->get_object( $object->type, 'feed', $object ); |
||
| 130 | View Code Duplication | } elseif ( $object instanceof \WP_Post ) { |
|
| 131 | if ( $type = wp_get_object_terms( $object->ID, 'calendar_type' ) ) { |
||
| 132 | $name = sanitize_title( current( $type )->name ); |
||
| 133 | return $this->get_object( $name, 'calendar', $object ); |
||
| 134 | } |
||
| 135 | } elseif ( isset( $object->type ) && isset( $object->id ) ) { |
||
| 136 | return $this->get_object( $object->type, 'calendar', $object->id ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( is_int( $object ) ) { |
||
| 141 | $post = get_post( $object ); |
||
| 142 | View Code Duplication | if ( $post && ( $type = wp_get_object_terms( $post->ID, 'calendar_type' ) ) ) { |
|
| 143 | $name = sanitize_title( current( $type )->name ); |
||
| 144 | return $this->get_object( $name, 'calendar', $post ); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return null; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get a calendar view. |
||
| 153 | * |
||
| 154 | * @since 3.0.0 |
||
| 155 | * |
||
| 156 | * @param int $id Feed post id. |
||
| 157 | * @param string $name (optional) Name of calendar view. |
||
| 158 | * |
||
| 159 | * @return null|Object\Calendar_View |
||
| 160 | */ |
||
| 161 | public function get_calendar_view( $id = 0, $name = '' ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get a feed. |
||
| 179 | * |
||
| 180 | * Returns the right type of feed. |
||
| 181 | * |
||
| 182 | * @since 3.0.0 |
||
| 183 | * |
||
| 184 | * @param int|string|object|\WP_Post|Object\Calendar $object |
||
| 185 | * |
||
| 186 | * @return null|Object\Feed |
||
| 187 | */ |
||
| 188 | public function get_feed( $object ) { |
||
| 189 | |||
| 190 | View Code Duplication | if ( is_string( $object ) ) { |
|
| 191 | return ! empty( $object ) ? $this->get_object( $object, 'feed', '' ) : null; |
||
| 192 | } |
||
| 193 | |||
| 194 | if ( is_object( $object ) ) { |
||
| 195 | if ( $object instanceof Object\Calendar ) { |
||
| 196 | $feed_name = ''; |
||
| 197 | if ( empty( $object->feed ) ) { |
||
| 198 | if ( $feed_type = wp_get_object_terms( $object->id, 'feed_type' ) ) { |
||
| 199 | $feed_name = sanitize_title( current( $feed_type )->name ); |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | $feed_name = $object->feed; |
||
| 203 | } |
||
| 204 | return $this->get_object( $feed_name, 'feed', $object ); |
||
| 205 | View Code Duplication | } elseif ( $object instanceof \WP_Post ) { |
|
| 206 | $calendar = $this->get_calendar( $object ); |
||
| 207 | |||
| 208 | if ( isset( $calendar->feed ) ) { |
||
| 209 | return $this->get_object( $calendar->feed, 'feed', $calendar ); |
||
| 210 | } else { |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | } elseif ( isset( $object->feed ) && isset( $object->id ) ) { |
||
| 216 | return $this->get_object( $object->feed, 'feed', $object ); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | View Code Duplication | if ( is_int( $object ) ) { |
|
| 221 | $calendar = $this->get_calendar( $object ); |
||
| 222 | return isset( $calendar->feed ) ? $this->get_object( $calendar->feed, 'feed', $calendar ) : null; |
||
| 223 | } |
||
| 224 | |||
| 225 | return null; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get a field. |
||
| 230 | * |
||
| 231 | * @since 3.0.0 |
||
| 232 | * |
||
| 233 | * @param array $args Field args. |
||
| 234 | * @param string $name Field type. |
||
| 235 | * |
||
| 236 | * @return null|Object\Field |
||
| 237 | */ |
||
| 238 | public function get_field( $args, $name = '' ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get a settings page. |
||
| 249 | * |
||
| 250 | * @since 3.0.0 |
||
| 251 | * |
||
| 252 | * @param string $name |
||
| 253 | * |
||
| 254 | * @return null|Object\Admin_Page |
||
| 255 | */ |
||
| 256 | public function get_admin_page( $name ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get a plugin object. |
||
| 262 | * |
||
| 263 | * @since 3.0.0 |
||
| 264 | * @access private |
||
| 265 | * |
||
| 266 | * @param string $name Object name. |
||
| 267 | * @param string $type Object type. |
||
| 268 | * @param mixed $args (optional) arguments for the class constructor. |
||
| 269 | * |
||
| 270 | * @return null|Object |
||
| 271 | */ |
||
| 272 | private function get_object( $name, $type, $args = '' ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Make class name from slug. |
||
| 296 | * |
||
| 297 | * Standardizes object naming and class names: <object-name> becomes <Class_Name>. |
||
| 298 | * The plugin autoloader uses a similar pattern. |
||
| 299 | * |
||
| 300 | * @since 3.0.0 |
||
| 301 | * @access private |
||
| 302 | * |
||
| 303 | * @param string $name Object name. |
||
| 304 | * @param string $type Object type. |
||
| 305 | * |
||
| 306 | * @return string The class name complete with its full namespace. |
||
| 307 | */ |
||
| 308 | private function make_class_name( $name, $type ) { |
||
| 328 | |||
| 329 | } |
||
| 330 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.