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 |
||
| 14 | class net_nehmer_blog_handler_link extends midcom_baseclasses_components_handler |
||
|
1 ignored issue
–
show
|
|||
| 15 | implements midcom_helper_datamanager2_interfaces_create |
||
|
1 ignored issue
–
show
|
|||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The content topic to use |
||
| 19 | * |
||
| 20 | * @var midcom_db_topic |
||
| 21 | */ |
||
| 22 | private $_content_topic = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The article link which has been created |
||
| 26 | * |
||
| 27 | * @var net_nehmer_blog_link_dba |
||
| 28 | */ |
||
| 29 | private $_link = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Maps the content topic from the request data to local member variables. |
||
| 33 | */ |
||
| 34 | public function _on_initialize() |
||
| 38 | |||
| 39 | public function load_schemadb() |
||
| 43 | |||
| 44 | public function get_schema_name() |
||
| 48 | |||
| 49 | View Code Duplication | public function get_schema_defaults() |
|
| 50 | { |
||
| 51 | $defaults = array(); |
||
| 52 | if (isset($_GET['article'])) |
||
| 53 | { |
||
| 54 | $defaults['article'] = $_GET['article']; |
||
| 55 | } |
||
| 56 | else |
||
| 57 | { |
||
| 58 | $defaults['topic'] = $this->_topic->id; |
||
| 59 | } |
||
| 60 | return $defaults; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * DM2 creation callback, binds to the current content topic. |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function &dm2_create_callback (&$controller) |
|
| 67 | { |
||
| 68 | $this->_link = new net_nehmer_blog_link_dba(); |
||
| 69 | $this->_link->topic = $this->_topic->id; |
||
| 70 | |||
| 71 | if (!$this->_link->create()) |
||
| 72 | { |
||
| 73 | debug_print_r('We operated on this object:', $this->_link); |
||
| 74 | throw new midcom_error('Failed to create a new article. Last Midgard error was: '. midcom_connection::get_error_string()); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this->_link; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Displays an article edit view. |
||
| 82 | * |
||
| 83 | * @param mixed $handler_id The ID of the handler. |
||
| 84 | * @param array $args The argument list. |
||
| 85 | * @param array &$data The local request data. |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function _handler_create($handler_id, array $args, array &$data) |
|
| 88 | { |
||
| 89 | $this->_content_topic->require_do('midgard:create'); |
||
| 90 | |||
| 91 | if (!$this->_config->get('enable_article_links')) |
||
| 92 | { |
||
| 93 | throw new midcom_error_notfound('Article linking disabled'); |
||
| 94 | } |
||
| 95 | |||
| 96 | $workflow = $this->get_workflow('datamanager2', array |
||
| 97 | ( |
||
| 98 | 'controller' => $this->get_controller('create'), |
||
| 99 | 'save_callback' => array($this, 'save_callback') |
||
| 100 | )); |
||
| 101 | |||
| 102 | midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('article link'))); |
||
| 103 | |||
| 104 | return $workflow->run(); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function save_callback(midcom_helper_datamanager2_controller $controller) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Displays article link delete confirmation |
||
| 118 | * |
||
| 119 | * @param mixed $handler_id The ID of the handler. |
||
| 120 | * @param array $args The argument list. |
||
| 121 | * @param array &$data The local request data. |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function _handler_delete($handler_id, array $args, array &$data) |
|
| 148 | } |
||
| 149 |
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.