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 net_nehmer_blog_handler_archive 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 net_nehmer_blog_handler_archive, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class net_nehmer_blog_handler_archive extends midcom_baseclasses_components_handler |
||
|
1 ignored issue
–
show
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The content topic to use |
||
| 20 | * |
||
| 21 | * @var midcom_db_topic |
||
| 22 | */ |
||
| 23 | private $_content_topic = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The articles to display |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $_articles = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The datamanager for the currently displayed article. |
||
| 34 | * |
||
| 35 | * @var midcom_helper_datamanager2_datamanager |
||
| 36 | */ |
||
| 37 | private $_datamanager = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The start date of the Archive listing. |
||
| 41 | * |
||
| 42 | * @var DateTime |
||
| 43 | */ |
||
| 44 | private $_start = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The end date of the Archive listing. |
||
| 48 | * |
||
| 49 | * @var DateTime |
||
| 50 | */ |
||
| 51 | private $_end = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Maps the content topic from the request data to local member variables. |
||
| 55 | */ |
||
| 56 | public function _on_initialize() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Simple helper which references all important members to the request data listing |
||
| 63 | * for usage within the style listing. |
||
| 64 | */ |
||
| 65 | private function _prepare_request_data() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Shows the archive welcome page: A listing of years/months along with total post counts |
||
| 74 | * and similar stuff. |
||
| 75 | * |
||
| 76 | * The handler computes all necessary data and populates the request array accordingly. |
||
| 77 | * |
||
| 78 | * @param mixed $handler_id The ID of the handler. |
||
| 79 | * @param array $args The argument list. |
||
| 80 | * @param array &$data The local request data. |
||
| 81 | */ |
||
| 82 | public function _handler_welcome ($handler_id, array $args, array &$data) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Loads the first posting time from the DB. This is the base for all operations on the |
||
| 99 | * resultset. |
||
| 100 | * |
||
| 101 | * This is done under sudo if possible, to avoid problems arising if the first posting |
||
| 102 | * is hidden. This keeps up performance, as an execute_unchecked() can be made in this case. |
||
| 103 | * If sudo cannot be acquired, the system falls back to excute(). |
||
| 104 | * |
||
| 105 | * @return DateTime The time of the first posting or null on failure. |
||
| 106 | */ |
||
| 107 | private function _compute_welcome_first_post() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Computes the number of postings in a given timeframe. |
||
| 136 | * |
||
| 137 | * @param DateTime $start Start of the timeframe (inclusive) |
||
| 138 | * @param DateTime $end End of the timeframe (exclusive) |
||
| 139 | * @return int Posting count |
||
| 140 | */ |
||
| 141 | private function _compute_welcome_posting_count($start, $end) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Computes the data nececssary for the welcome screen. Automatically put into the request |
||
| 155 | * data array. |
||
| 156 | */ |
||
| 157 | private function _compute_welcome_data() |
||
| 237 | |||
| 238 | private function _get_month_names() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Displays the welcome page. |
||
| 251 | * |
||
| 252 | * Element sequence: |
||
| 253 | * |
||
| 254 | * - archive-welcome-start (Start of the archive welcome page) |
||
| 255 | * - archive-welcome-year (Display of a single year, may not be called when there are no postings) |
||
| 256 | * - archive-welcome-end (End of the archive welcome page) |
||
| 257 | * |
||
| 258 | * Context data for all elements: |
||
| 259 | * |
||
| 260 | * - int total_count (total number of postings w/o ACL restrictions) |
||
| 261 | * - DateTime first_post (the first posting date, may be null) |
||
| 262 | * - Array year_data (the year data, contains the year context info as outlined below) |
||
| 263 | * |
||
| 264 | * Context data for year elements: |
||
| 265 | * |
||
| 266 | * - int year (the year displayed) |
||
| 267 | * - string url (url to display the complete year) |
||
| 268 | * - int count (Number of postings in that year) |
||
| 269 | * - array month_data (the monthly data) |
||
| 270 | * |
||
| 271 | * month_data will contain an associative array containing the following array of data |
||
| 272 | * indexed by month number (1-12): |
||
| 273 | * |
||
| 274 | * - string 'url' => The URL to the month. |
||
| 275 | * - string 'name' => The localized name of the month. |
||
| 276 | * - int 'count' => The number of postings in that month. |
||
| 277 | * |
||
| 278 | * @param mixed $handler_id The ID of the handler. |
||
| 279 | * @param array &$data The local request data. |
||
| 280 | */ |
||
| 281 | public function _show_welcome($handler_id, array &$data) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Shows the archive. Depending on the selected handler various constraints are added to |
||
| 299 | * the QB. See the add_*_constraint methods for details. |
||
| 300 | * |
||
| 301 | * @param mixed $handler_id The ID of the handler. |
||
| 302 | * @param array $args The argument list. |
||
| 303 | * @param array &$data The local request data. |
||
| 304 | */ |
||
| 305 | public function _handler_list ($handler_id, array $args, array &$data) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Computes the start/end dates to only query a given year. It will do validation |
||
| 383 | * before processing, throwing 404 in case of incorrectly formatted dates. |
||
| 384 | * |
||
| 385 | * This is used by the archive-year handler, which expects the year to be in $args[0]. |
||
| 386 | * |
||
| 387 | * @param int $year The year to query. |
||
| 388 | */ |
||
| 389 | private function _set_startend_from_year($year) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Computes the start/end dates to only query a given month. It will do validation |
||
| 410 | * before processing, throwing 404 in case of incorrectly formatted dates. |
||
| 411 | * |
||
| 412 | * This is used by the archive-month handler, which expects the year to be in $args[0] |
||
| 413 | * and the month to be in $args[1]. |
||
| 414 | * |
||
| 415 | * @param int $year The year to query. |
||
| 416 | * @param int $month The month to query. |
||
| 417 | */ |
||
| 418 | private function _set_startend_from_month($year, $month) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Displays the archive. |
||
| 456 | * |
||
| 457 | * @param mixed $handler_id The ID of the handler. |
||
| 458 | * @param array &$data The local request data. |
||
| 459 | */ |
||
| 460 | public function _show_list($handler_id, array &$data) |
||
| 511 | } |
||
| 512 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.