| Conditions | 72 |
| Paths | > 20000 |
| Total Lines | 361 |
| Code Lines | 257 |
| 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 |
||
| 151 | public function displayPost() |
||
| 152 | { |
||
| 153 | $xoops = Xoops::getInstance(); |
||
| 154 | if (Request::getMethod()!=='POST') { |
||
| 155 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 156 | } |
||
| 157 | $id = Request::getInt('com_id'); |
||
| 158 | $modid = Request::getInt('com_modid'); |
||
| 159 | if (empty($modid)) { |
||
| 160 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 161 | } |
||
| 162 | |||
| 163 | /* @var $comment CommentsComment */ |
||
| 164 | $comment = $this->getHandlerComment()->get($id); |
||
| 165 | if (!is_object($comment)) { |
||
| 166 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!$comment->isNew()) { |
||
| 170 | $modid = $comment->getVar('modid'); |
||
| 171 | } else { |
||
| 172 | $comment->setVar('modid', $modid); |
||
| 173 | } |
||
| 174 | |||
| 175 | $module = $xoops->getModuleById($modid); |
||
| 176 | if (!is_object($module)) { |
||
| 177 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 178 | } |
||
| 179 | |||
| 180 | $moddir = $module->getVar('dirname'); |
||
| 181 | |||
| 182 | if ($xoops->isAdminSide) { |
||
| 183 | if (empty($id)) { |
||
| 184 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 185 | } |
||
| 186 | $redirect_page = $this->url('admin/main.php?com_modid=' . $modid . '&com_itemid'); |
||
| 187 | } else { |
||
| 188 | if (static::APPROVE_NONE == $xoops->getModuleConfig('com_rule', $moddir)) { |
||
| 189 | $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION); |
||
| 190 | } |
||
| 191 | $redirect_page = ''; |
||
| 192 | } |
||
| 193 | |||
| 194 | /* @var $plugin CommentsPluginInterface */ |
||
| 195 | if ($plugin = \Xoops\Module\Plugin::getPlugin($moddir, 'comments')) { |
||
| 196 | if (!$xoops->isAdminSide) { |
||
| 197 | $redirect_page = $xoops->url('modules/' . $moddir . '/' . $plugin->pageName() . '?'); |
||
| 198 | if (is_array($extraParams = $plugin->extraParams())) { |
||
| 199 | $extra_params = ''; |
||
| 200 | foreach ($extraParams as $extra_param) { |
||
| 201 | $extra_params .= isset($_POST[$extra_param]) |
||
| 202 | ? $extra_param . '=' . htmlspecialchars($_POST[$extra_param]) . '&' |
||
| 203 | : $extra_param . '=amp;'; |
||
| 204 | } |
||
| 205 | $redirect_page .= $extra_params; |
||
| 206 | } |
||
| 207 | $redirect_page .= $plugin->itemName(); |
||
| 208 | } |
||
| 209 | $comment_url = $redirect_page; |
||
| 210 | |||
| 211 | $op = Request::getBool('com_dopost') ? 'post' : ''; |
||
| 212 | $op = Request::getBool('com_dopreview') ? 'preview' : $op; |
||
| 213 | $op = Request::getBool('com_dodelete') ? 'delete' : $op; |
||
| 214 | |||
| 215 | if ($op === 'preview' || $op === 'post') { |
||
| 216 | if (!$xoops->security()->check()) { |
||
| 217 | $op = ''; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | if ($op === 'post' && !$xoops->isUser()) { |
||
| 221 | $xoopsCaptcha = XoopsCaptcha::getInstance(); |
||
| 222 | if (!$xoopsCaptcha->verify()) { |
||
| 223 | $captcha_message = $xoopsCaptcha->getMessage(); |
||
| 224 | $op = 'preview'; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | $title = XoopsLocale::trim(Request::getString('com_title')); |
||
| 229 | $text = XoopsLocale::trim(Request::getString('com_text')); |
||
| 230 | $mode = XoopsLocale::trim(Request::getString('com_mode', 'flat')); |
||
| 231 | $order = Request::getInt('com_order', static::DISPLAY_OLDEST_FIRST); |
||
| 232 | $itemid = Request::getInt('com_itemid'); |
||
| 233 | $pid = Request::getInt('com_pid'); |
||
| 234 | $rootid = Request::getInt('com_rootid'); |
||
| 235 | $status = Request::getInt('com_status'); |
||
| 236 | $dosmiley = Request::getBool('com_dosmiley'); |
||
| 237 | $doxcode = Request::getBool('com_doxcode'); |
||
| 238 | $dobr = Request::getBool('com_dobr'); |
||
| 239 | $dohtml = Request::getBool('com_html'); |
||
| 240 | $doimage = Request::getBool('com_doimage'); |
||
| 241 | $icon = XoopsLocale::trim(Request::getString('com_icon')); |
||
| 242 | |||
| 243 | $comment->setVar('title', $title); |
||
| 244 | $comment->setVar('text', $text); |
||
| 245 | $comment->setVar('itemid', $itemid); |
||
| 246 | $comment->setVar('pid', $pid); |
||
| 247 | $comment->setVar('rootid', $rootid); |
||
| 248 | $comment->setVar('status', $status); |
||
| 249 | $comment->setVar('dosmiley', $dosmiley); |
||
| 250 | $comment->setVar('doxcode', $doxcode); |
||
| 251 | $comment->setVar('dobr', $dobr); |
||
| 252 | $comment->setVar('dohtml', $dohtml); |
||
| 253 | $comment->setVar('doimage', $doimage); |
||
| 254 | $comment->setVar('icon', $icon); |
||
| 255 | |||
| 256 | switch ($op) { |
||
| 257 | case "delete": |
||
| 258 | $this->displayDelete(); |
||
| 259 | break; |
||
| 260 | |||
| 261 | case "preview": |
||
| 262 | $comment->setVar('doimage', 1); |
||
| 263 | if ($comment->getVar('dohtml') != 0) { |
||
| 264 | if ($xoops->isUser()) { |
||
| 265 | if (!$xoops->user->isAdmin($comment->getVar('modid'))) { |
||
| 266 | $comment->setVar('dohtml', 0); |
||
| 267 | } |
||
| 268 | } else { |
||
| 269 | $comment->setVar('dohtml', 0); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | $xoops->header(); |
||
| 274 | if (!$xoops->isAdminSide && !empty($captcha_message)) { |
||
| 275 | echo $xoops->alert('error', $captcha_message); |
||
| 276 | } |
||
| 277 | echo $this->renderHeader($comment->getVar('title', 'p'), $comment->getVar('text', 'p'), false, time()); |
||
| 278 | $this->displayCommentForm($comment); |
||
| 279 | $xoops->footer(); |
||
| 280 | break; |
||
| 281 | |||
| 282 | case "post": |
||
| 283 | $comment->setVar('doimage', 1); |
||
| 284 | $comment_handler = $this->getHandlerComment(); |
||
| 285 | $add_userpost = false; |
||
| 286 | $call_approvefunc = false; |
||
| 287 | $call_updatefunc = false; |
||
| 288 | // RMV-NOTIFY - this can be set to 'comment' or 'comment_submit' |
||
| 289 | $notify_event = false; |
||
| 290 | if (!empty($id)) { |
||
| 291 | $accesserror = false; |
||
| 292 | |||
| 293 | if ($xoops->isUser()) { |
||
| 294 | if ($xoops->user->isAdmin($comment->getVar('modid'))) { |
||
| 295 | if (!empty($status) && $status != static::STATUS_PENDING) { |
||
| 296 | $old_status = $comment->getVar('status'); |
||
| 297 | $comment->setVar('status', $status); |
||
| 298 | // if changing status from pending state, increment user post |
||
| 299 | if (static::STATUS_PENDING == $old_status) { |
||
| 300 | $add_userpost = true; |
||
| 301 | if (static::STATUS_ACTIVE == $status) { |
||
| 302 | $call_updatefunc = true; |
||
| 303 | $call_approvefunc = true; |
||
| 304 | // RMV-NOTIFY |
||
| 305 | $notify_event = 'comment'; |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | if (static::STATUS_HIDDEN == $old_status && static::STATUS_ACTIVE == $status) { |
||
| 309 | $call_updatefunc = true; |
||
| 310 | // Comments can not be directly posted hidden, |
||
| 311 | // no need to send notification here |
||
| 312 | } else { |
||
| 313 | if (static::STATUS_ACTIVE == $old_status && static::STATUS_HIDDEN == $status) { |
||
| 314 | $call_updatefunc = true; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } else { |
||
| 320 | $comment->setVar('dohtml', 0); |
||
| 321 | if ($comment->getVar('uid') != $xoops->user->getVar('uid')) { |
||
| 322 | $accesserror = true; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } else { |
||
| 326 | $comment->setVar('dohtml', 0); |
||
| 327 | $accesserror = true; |
||
| 328 | } |
||
| 329 | if (false != $accesserror) { |
||
| 330 | $xoops->redirect( |
||
| 331 | $redirect_page . '=' . $comment->getVar('itemid') |
||
| 332 | . '&com_id=' . $comment->getVar('id') |
||
| 333 | . '&com_mode=' . $mode . '&com_order=' . $order, |
||
| 334 | 1, |
||
| 335 | XoopsLocale::E_NO_ACCESS_PERMISSION |
||
| 336 | ); |
||
| 337 | } |
||
| 338 | } else { |
||
| 339 | $comment->setVar('created', time()); |
||
| 340 | $comment->setVar('ip', $xoops->getEnv('REMOTE_ADDR')); |
||
| 341 | if ($xoops->isUser()) { |
||
| 342 | if ($xoops->user->isAdmin($comment->getVar('modid'))) { |
||
| 343 | $comment->setVar('status', static::STATUS_ACTIVE); |
||
| 344 | $add_userpost = true; |
||
| 345 | $call_approvefunc = true; |
||
| 346 | $call_updatefunc = true; |
||
| 347 | // RMV-NOTIFY |
||
| 348 | $notify_event = 'comment'; |
||
| 349 | } else { |
||
| 350 | $comment->setVar('dohtml', 0); |
||
| 351 | switch ($xoops->getModuleConfig('com_rule', $moddir)) { |
||
| 352 | case static::APPROVE_ALL: |
||
| 353 | case static::APPROVE_USER: |
||
| 354 | $comment->setVar('status', static::STATUS_ACTIVE); |
||
| 355 | $add_userpost = true; |
||
| 356 | $call_approvefunc = true; |
||
| 357 | $call_updatefunc = true; |
||
| 358 | // RMV-NOTIFY |
||
| 359 | $notify_event = 'comment'; |
||
| 360 | break; |
||
| 361 | case static::APPROVE_ADMIN: |
||
| 362 | default: |
||
| 363 | $comment->setVar('status', static::STATUS_PENDING); |
||
| 364 | $notify_event = 'comment_submit'; |
||
| 365 | break; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | if ($xoops->getModuleConfig('com_anonpost', $module->getVar('dirname')) |
||
| 369 | && $comment->getVar('noname') |
||
| 370 | ) { |
||
| 371 | $comment->setVar('uid', 0); |
||
| 372 | } else { |
||
| 373 | $comment->setVar('uid', $xoops->user->getVar('uid')); |
||
| 374 | } |
||
| 375 | } else { |
||
| 376 | $comment->setVar('dohtml', 0); |
||
| 377 | $comment->setVar('uid', 0); |
||
| 378 | if ($xoops->getModuleConfig('com_anonpost', $module->getVar('dirname')) != 1) { |
||
| 379 | $xoops->redirect( |
||
| 380 | $redirect_page . '=' . $comment->getVar('itemid') |
||
| 381 | . '&com_id=' . $comment->getVar('id') . '&com_mode=' . $mode |
||
| 382 | . '&com_order=' . $order, |
||
| 383 | 1, |
||
| 384 | XoopsLocale::E_NO_ACCESS_PERMISSION |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | if ($comment->getVar('uid') == 0) { |
||
| 389 | switch ($xoops->getModuleConfig('com_rule', $moddir)) { |
||
| 390 | case static::APPROVE_ALL: |
||
| 391 | $comment->setVar('status', static::STATUS_ACTIVE); |
||
| 392 | $add_userpost = true; |
||
| 393 | $call_approvefunc = true; |
||
| 394 | $call_updatefunc = true; |
||
| 395 | // RMV-NOTIFY |
||
| 396 | $notify_event = 'comment'; |
||
| 397 | break; |
||
| 398 | case static::APPROVE_ADMIN: |
||
| 399 | case static::APPROVE_USER: |
||
| 400 | default: |
||
| 401 | $comment->setVar('status', static::STATUS_PENDING); |
||
| 402 | // RMV-NOTIFY |
||
| 403 | $notify_event = 'comment_submit'; |
||
| 404 | break; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | if ($comment->getVar('title') == '') { |
||
| 409 | $comment->setVar('title', XoopsLocale::NO_TITLE); |
||
| 410 | } |
||
| 411 | $comment->setVar('modified', time()); |
||
| 412 | if (isset($extra_params)) { |
||
| 413 | $comment->setVar('exparams', $extra_params); |
||
| 414 | } |
||
| 415 | |||
| 416 | if (false != $comment_handler->insert($comment)) { |
||
| 417 | $newcid = $comment->getVar('id'); |
||
| 418 | // set own id as root id if this is a top comment |
||
| 419 | if ($comment->getVar('rootid') == 0) { |
||
| 420 | $comment->setVar('rootid', $newcid); |
||
| 421 | if (!$comment_handler->updateByField($comment, 'rootid', $comment->getVar('rootid'))) { |
||
| 422 | $comment_handler->delete($comment); |
||
| 423 | $xoops->header(); |
||
| 424 | echo $xoops->alert('error', $comment->getHtmlErrors()); |
||
| 425 | $xoops->footer(); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | // call custom approve function if any |
||
| 429 | if (false != $call_approvefunc) { |
||
| 430 | $plugin->approve($comment); |
||
| 431 | } |
||
| 432 | |||
| 433 | if (false != $call_updatefunc) { |
||
| 434 | $criteria = new CriteriaCompo(new Criteria('modid', $comment->getVar('modid'))); |
||
| 435 | $criteria->add(new Criteria('itemid', $comment->getVar('itemid'))); |
||
| 436 | $criteria->add(new Criteria('status', static::STATUS_ACTIVE)); |
||
| 437 | $comment_count = $comment_handler->getCount($criteria); |
||
| 438 | $plugin->update($comment->getVar('itemid'), $comment_count); |
||
| 439 | } |
||
| 440 | |||
| 441 | // increment user post if needed |
||
| 442 | $uid = $comment->getVar('uid'); |
||
| 443 | if ($uid > 0 && false != $add_userpost) { |
||
| 444 | $member_handler = $xoops->getHandlerMember(); |
||
| 445 | $poster = $member_handler->getUser($uid); |
||
| 446 | if ($poster instanceof XoopsUser) { |
||
| 447 | $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') + 1); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | // RMV-NOTIFY |
||
| 452 | // trigger notification event if necessary |
||
| 453 | if ($notify_event && $xoops->isActiveModule('notifications')) { |
||
| 454 | $notifications = Notifications::getInstance(); |
||
| 455 | $not_modid = $comment->getVar('modid'); |
||
| 456 | $not_catinfo = $notifications->getCommentsCategory($module->getVar('dirname')); |
||
| 457 | $not_category = $not_catinfo['name']; |
||
| 458 | $not_itemid = $comment->getVar('itemid'); |
||
| 459 | $not_event = $notify_event; |
||
| 460 | // Build an ABSOLUTE URL to view the comment. Make sure we |
||
| 461 | // point to a viewable page (i.e. not the system administration |
||
| 462 | // module). |
||
| 463 | $comment_tags = array(); |
||
| 464 | $comment_tags['X_COMMENT_URL'] = $comment_url . '=' . $comment->getVar('itemid') |
||
| 465 | . '&com_id=' . $comment->getVar('id') |
||
| 466 | . '&com_rootid=' . $comment->getVar('rootid') |
||
| 467 | . '&com_mode=' . $mode . '&com_order=' . $order |
||
| 468 | . '#comment' . $comment->getVar('id'); |
||
| 469 | |||
| 470 | if ($xoops->isActiveModule('notifications')) { |
||
| 471 | Notifications::getInstance()->getHandlerNotification()->triggerEvent($not_category, $not_itemid, $not_event, $comment_tags, false, $not_modid); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | if (!isset($comment_post_results)) { |
||
| 475 | // if the comment is active, redirect to posted comment |
||
| 476 | if ($comment->getVar('status') == static::STATUS_ACTIVE) { |
||
| 477 | $xoops->redirect( |
||
| 478 | $redirect_page . '=' . $comment->getVar('itemid') |
||
| 479 | . '&com_id=' . $comment->getVar('id') |
||
| 480 | . '&com_rootid=' . $comment->getVar('rootid') . '&com_mode=' . $mode |
||
| 481 | . '&com_order=' . $order . '#comment' . $comment->getVar('id'), |
||
| 482 | 1, |
||
| 483 | _MD_COMMENTS_THANKSPOST |
||
| 484 | ); |
||
| 485 | } else { |
||
| 486 | // not active, so redirect to top comment page |
||
| 487 | $xoops->redirect( |
||
| 488 | $redirect_page . '=' . $comment->getVar('itemid') . '&com_mode=' . $mode |
||
| 489 | . '&com_order=' . $order . '#comment' . $comment->getVar('id'), |
||
| 490 | 1, |
||
| 491 | _MD_COMMENTS_THANKSPOST |
||
| 492 | ); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | } else { |
||
| 496 | if (!isset($purge_comment_post_results)) { |
||
| 497 | $xoops->header(); |
||
| 498 | echo $xoops->alert('error', $comment->getHtmlErrors()); |
||
| 499 | $xoops->footer(); |
||
| 500 | } else { |
||
| 501 | $comment_post_results = $comment->getErrors(); |
||
| 502 | } |
||
| 503 | } |
||
| 504 | break; |
||
| 505 | default: |
||
| 506 | $xoops->redirect( |
||
| 507 | \XoopsBaseConfig::get('url') . '/', |
||
| 508 | 1, |
||
| 509 | implode('<br />', $xoops->security()->getErrors()) |
||
| 510 | ); |
||
| 511 | break; |
||
| 512 | } |
||
| 1116 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: