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_static_viewer extends midcom_baseclasses_components_request |
||
|
1 ignored issue
–
show
|
|||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The topic in which to look for articles. This defaults to the current content topic |
||
| 18 | * unless overridden by the symlink topic feature. |
||
| 19 | * |
||
| 20 | * @var midcom_db_topic |
||
| 21 | */ |
||
| 22 | private $_content_topic = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Initialize the request switch and the content topic. |
||
| 26 | */ |
||
| 27 | public function _on_initialize() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set the content topic to use. This will check against the configuration setting 'symlink_topic'. |
||
| 53 | */ |
||
| 54 | View Code Duplication | private function _determine_content_topic() |
|
| 55 | { |
||
| 56 | $guid = $this->_config->get('symlink_topic'); |
||
| 57 | if (is_null($guid)) |
||
| 58 | { |
||
| 59 | // No symlink topic |
||
| 60 | // Workaround, we should talk to a DBA object automatically here in fact. |
||
| 61 | $this->_content_topic = midcom_db_topic::get_cached($this->_topic->id); |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->_content_topic = midcom_db_topic::get_cached($guid); |
||
| 66 | // Validate topic. |
||
| 67 | if ($this->_content_topic->component != 'net.nehmer.static') |
||
| 68 | { |
||
| 69 | debug_print_r('Retrieved topic was:', $this->_content_topic); |
||
| 70 | throw new midcom_error('Symlink content topic is invalid, see the debug level log for details.'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Indexes an article. |
||
| 76 | * |
||
| 77 | * @param midcom_helper_datamanager2_datamanager $dm The Datamanager encapsulating the event. |
||
| 78 | * @param midcom_services_indexer $indexer The indexer instance to use. |
||
| 79 | * @param midcom_db_topic $topic The topic which we are bound to. If this is not an object, the code |
||
| 80 | * tries to load a new topic instance from the database identified by this parameter. |
||
| 81 | */ |
||
| 82 | View Code Duplication | public static function index($dm, $indexer, $topic) |
|
| 83 | { |
||
| 84 | if (!is_object($topic)) |
||
| 85 | { |
||
| 86 | $topic = new midcom_db_topic($topic); |
||
| 87 | } |
||
| 88 | |||
| 89 | // Don't index directly, that would lose a reference due to limitations |
||
| 90 | // of the index() method. Needs fixes there. |
||
| 91 | |||
| 92 | $nav = new midcom_helper_nav(); |
||
| 93 | $node = $nav->get_node($topic->id); |
||
| 94 | |||
| 95 | $document = $indexer->new_document($dm); |
||
| 96 | $document->topic_guid = $topic->guid; |
||
| 97 | $document->topic_url = $node[MIDCOM_NAV_FULLURL]; |
||
| 98 | $document->read_metadata_from_object($dm->storage->object); |
||
| 99 | $document->component = $topic->component; |
||
| 100 | $indexer->index($document); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Populates the node toolbar depending on the user's rights. |
||
| 105 | */ |
||
| 106 | private function _populate_node_toolbar() |
||
| 107 | { |
||
| 108 | $buttons = array(); |
||
| 109 | $workflow = $this->get_workflow('datamanager2'); |
||
| 110 | View Code Duplication | if ($this->_content_topic->can_do('midgard:create')) |
|
| 111 | { |
||
| 112 | foreach (array_keys($this->_request_data['schemadb']) as $name) |
||
| 113 | { |
||
| 114 | $buttons[] = $workflow->get_button("create/{$name}/", array |
||
| 115 | ( |
||
| 116 | MIDCOM_TOOLBAR_LABEL => sprintf |
||
| 117 | ( |
||
| 118 | $this->_l10n_midcom->get('create %s'), |
||
| 119 | $this->_l10n->get($this->_request_data['schemadb'][$name]->description) |
||
| 120 | ), |
||
| 121 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png', |
||
| 122 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/new-text.png', |
||
| 123 | MIDCOM_TOOLBAR_ACCESSKEY => 'n', |
||
| 124 | )); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( $this->_config->get('enable_article_links') |
||
| 129 | && $this->_content_topic->can_do('midgard:create')) |
||
| 130 | { |
||
| 131 | $buttons[] = $workflow->get_button("create/link/", array |
||
| 132 | ( |
||
| 133 | MIDCOM_TOOLBAR_LABEL => sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('article link')), |
||
| 134 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png', |
||
| 135 | )); |
||
| 136 | } |
||
| 137 | |||
| 138 | if ( $this->_topic->can_do('midgard:update') |
||
| 139 | && $this->_topic->can_do('midcom:component_config')) |
||
| 140 | { |
||
| 141 | $buttons[] = $workflow->get_button('config/', array |
||
| 142 | ( |
||
| 143 | MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'), |
||
| 144 | MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'), |
||
| 145 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png', |
||
| 146 | )); |
||
| 147 | } |
||
| 148 | $this->_node_toolbar->add_items($buttons); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The handle callback populates the toolbars. |
||
| 153 | */ |
||
| 154 | public function _on_handle($handler, array $args) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * |
||
| 163 | * @param midcom_helper_configuration $config |
||
| 164 | * @param integer $id The topic ID |
||
| 165 | * @return midcom_core_querybuilder The querybuilder instance |
||
| 166 | */ |
||
| 167 | public static function get_topic_qb(midcom_helper_configuration $config, $id) |
||
| 187 | } |
||
| 188 |
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.