| Conditions | 19 |
| Paths | 160 |
| Total Lines | 122 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 160 | public function updateChild($parents, $profile = null) |
||
| 161 | { |
||
| 162 | // All the parent groups to sort out. |
||
| 163 | if (!is_array($parents)) |
||
| 164 | { |
||
| 165 | $parents = array($parents); |
||
| 166 | } |
||
| 167 | |||
| 168 | // Find all the children of this group. |
||
| 169 | $request = $this->db->query('', ' |
||
| 170 | SELECT id_parent, id_group |
||
| 171 | FROM {db_prefix}membergroups |
||
| 172 | WHERE id_parent != {int:not_inherited} |
||
| 173 | ' . (empty($parents) ? '' : 'AND id_parent IN ({array_int:parent_list})'), |
||
| 174 | array( |
||
| 175 | 'parent_list' => $parents, |
||
| 176 | 'not_inherited' => -2, |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | $children = array(); |
||
| 180 | $parents = array(); |
||
| 181 | $child_groups = array(); |
||
| 182 | while ($row = $this->db->fetch_assoc($request)) |
||
| 183 | { |
||
| 184 | $children[$row['id_parent']][] = $row['id_group']; |
||
| 185 | $child_groups[] = $row['id_group']; |
||
| 186 | $parents[] = $row['id_parent']; |
||
| 187 | } |
||
| 188 | $this->db->free_result($request); |
||
| 189 | |||
| 190 | $parents = array_unique($parents); |
||
| 191 | |||
| 192 | // Not a sausage, or a child? |
||
| 193 | if (empty($children)) |
||
| 194 | { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | // Need functions that modify permissions... |
||
| 199 | require_once(SUBSDIR . '/ManagePermissions.subs.php'); |
||
| 200 | |||
| 201 | // First off, are we doing general permissions? |
||
| 202 | if ($profile < 1 || $profile === null) |
||
| 203 | { |
||
| 204 | // Fetch all the parent permissions. |
||
| 205 | $request = $this->db->query('', ' |
||
| 206 | SELECT id_group, permission, add_deny |
||
| 207 | FROM {db_prefix}permissions |
||
| 208 | WHERE id_group IN ({array_int:parent_list})', |
||
| 209 | array( |
||
| 210 | 'parent_list' => $parents, |
||
| 211 | ) |
||
| 212 | ); |
||
| 213 | $permissions = array(); |
||
| 214 | while ($row = $this->db->fetch_assoc($request)) |
||
| 215 | { |
||
| 216 | foreach ($children[$row['id_group']] as $child) |
||
| 217 | { |
||
| 218 | $permissions[] = array( |
||
| 219 | 'id_group' => (int) $child, |
||
| 220 | 'permission' => $row['permission'], |
||
| 221 | 'add_deny' => $row['add_deny'], |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | $this->db->free_result($request); |
||
| 226 | |||
| 227 | $this->db->query('', ' |
||
| 228 | DELETE FROM {db_prefix}permissions |
||
| 229 | WHERE id_group IN ({array_int:child_groups})', |
||
| 230 | array( |
||
| 231 | 'child_groups' => $child_groups, |
||
| 232 | ) |
||
| 233 | ); |
||
| 234 | |||
| 235 | // Finally insert. |
||
| 236 | if (!empty($permissions)) |
||
| 237 | { |
||
| 238 | replacePermission($permissions); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | // Then, what about board profiles? |
||
| 243 | if ($profile != -1) |
||
| 244 | { |
||
| 245 | $profileQuery = $profile === null ? '' : ' AND id_profile = {int:current_profile}'; |
||
| 246 | |||
| 247 | // Again, get all the parent permissions. |
||
| 248 | $request = $this->db->query('', ' |
||
| 249 | SELECT id_profile, id_group, permission, add_deny |
||
| 250 | FROM {db_prefix}board_permissions |
||
| 251 | WHERE id_group IN ({array_int:parent_groups}) |
||
| 252 | ' . $profileQuery, |
||
| 253 | array( |
||
| 254 | 'parent_groups' => $parents, |
||
| 255 | 'current_profile' => $profile !== null && $profile ? $profile : 1, |
||
| 256 | ) |
||
| 257 | ); |
||
| 258 | $permissions = array(); |
||
| 259 | while ($row = $this->db->fetch_assoc($request)) |
||
| 260 | { |
||
| 261 | foreach ($children[$row['id_group']] as $child) |
||
| 262 | { |
||
| 263 | $permissions[] = array($row['permission'], $child, $row['add_deny'], $row['id_profile']); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | $this->db->free_result($request); |
||
| 267 | |||
| 268 | $this->db->query('', ' |
||
| 269 | DELETE FROM {db_prefix}board_permissions |
||
| 270 | WHERE id_group IN ({array_int:child_groups}) |
||
| 271 | ' . $profileQuery, |
||
| 272 | array( |
||
| 273 | 'child_groups' => $child_groups, |
||
| 274 | 'current_profile' => $profile !== null && $profile ? $profile : 1, |
||
| 275 | ) |
||
| 276 | ); |
||
| 277 | |||
| 278 | // Do the insert. |
||
| 279 | if (!empty($permissions)) |
||
| 280 | { |
||
| 281 | replaceBoardPermission($permissions); |
||
| 282 | } |
||
| 286 |