| Conditions | 29 |
| Paths | 544 |
| Total Lines | 94 |
| Code Lines | 68 |
| Lines | 61 |
| Ratio | 64.89 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 115 | public function store() |
||
| 116 | { |
||
| 117 | $myts = MyTextSanitizer::getInstance(); |
||
| 118 | $title = ''; |
||
| 119 | $imgurl = ''; |
||
| 120 | if (isset($this->topic_title) && $this->topic_title != '') { |
||
| 121 | $title = $myts->addSlashes($this->topic_title); |
||
| 122 | } |
||
| 123 | if (isset($this->topic_imgurl) && $this->topic_imgurl != '') { |
||
| 124 | $imgurl = $myts->addSlashes($this->topic_imgurl); |
||
| 125 | } |
||
| 126 | if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) { |
||
| 127 | $this->topic_pid = 0; |
||
| 128 | } |
||
| 129 | if (empty($this->topic_id)) { |
||
| 130 | $this->topic_id = $this->db->genId($this->table . '_topic_id_seq'); |
||
| 131 | $sql = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title); |
||
| 132 | } else { |
||
| 133 | $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id); |
||
| 134 | } |
||
| 135 | if (!$result = $this->db->query($sql)) { |
||
| 136 | ErrorHandler::show('0022'); |
||
| 137 | } |
||
| 138 | if ($this->use_permission == true) { |
||
| 139 | if (empty($this->topic_id)) { |
||
| 140 | $this->topic_id = $this->db->getInsertId(); |
||
| 141 | } |
||
| 142 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
| 143 | $parent_topics = $xt->getAllParentId($this->topic_id); |
||
| 144 | View Code Duplication | if (!empty($this->m_groups) && is_array($this->m_groups)) { |
|
| 145 | foreach ($this->m_groups as $m_g) { |
||
| 146 | $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g); |
||
| 147 | $add = true; |
||
| 148 | // only grant this permission when the group has this permission in all parent topics of the created topic |
||
| 149 | foreach ($parent_topics as $p_topic) { |
||
| 150 | if (!in_array($p_topic, $moderate_topics)) { |
||
| 151 | $add = false; |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | if ($add == true) { |
||
| 156 | $xp = new XoopsPerms(); |
||
| 157 | $xp->setModuleId($this->mid); |
||
| 158 | $xp->setName('ModInTopic'); |
||
| 159 | $xp->setItemId($this->topic_id); |
||
| 160 | $xp->store(); |
||
| 161 | $xp->addGroup($m_g); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | View Code Duplication | if (!empty($this->s_groups) && is_array($this->s_groups)) { |
|
| 166 | foreach ($s_groups as $s_g) { |
||
| 167 | $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g); |
||
| 168 | $add = true; |
||
| 169 | foreach ($parent_topics as $p_topic) { |
||
| 170 | if (!in_array($p_topic, $submit_topics)) { |
||
| 171 | $add = false; |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | if ($add == true) { |
||
| 176 | $xp = new XoopsPerms(); |
||
| 177 | $xp->setModuleId($this->mid); |
||
| 178 | $xp->setName('SubmitInTopic'); |
||
| 179 | $xp->setItemId($this->topic_id); |
||
| 180 | $xp->store(); |
||
| 181 | $xp->addGroup($s_g); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | View Code Duplication | if (!empty($this->r_groups) && is_array($this->r_groups)) { |
|
| 186 | foreach ($r_groups as $r_g) { |
||
| 187 | $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g); |
||
| 188 | $add = true; |
||
| 189 | foreach ($parent_topics as $p_topic) { |
||
| 190 | if (!in_array($p_topic, $read_topics)) { |
||
| 191 | $add = false; |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | if ($add == true) { |
||
| 196 | $xp = new XoopsPerms(); |
||
| 197 | $xp->setModuleId($this->mid); |
||
| 198 | $xp->setName('ReadInTopic'); |
||
| 199 | $xp->setItemId($this->topic_id); |
||
| 200 | $xp->store(); |
||
| 201 | $xp->addGroup($r_g); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return true; |
||
| 208 | } |
||
| 209 | |||
| 408 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.