Conditions | 65 |
Paths | > 20000 |
Total Lines | 386 |
Code Lines | 262 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | 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 |
||
596 | function setupMenuContext() |
||
597 | { |
||
598 | global $context, $modSettings, $user_info, $txt, $scripturl, $settings; |
||
599 | |||
600 | // Set up the menu privileges. |
||
601 | $context['allow_search'] = !empty($modSettings['allow_guestAccess']) ? allowedTo('search_posts') : (!$user_info['is_guest'] && allowedTo('search_posts')); |
||
602 | $context['allow_admin'] = allowedTo(array('admin_forum', 'manage_boards', 'manage_permissions', 'moderate_forum', 'manage_membergroups', 'manage_bans', 'send_mail', 'edit_news', 'manage_attachments', 'manage_smileys')); |
||
603 | $context['allow_edit_profile'] = !$user_info['is_guest'] && allowedTo(array('profile_view_own', 'profile_view_any', 'profile_identity_own', 'profile_identity_any', 'profile_extra_own', 'profile_extra_any', 'profile_remove_own', 'profile_remove_any', 'moderate_forum', 'manage_membergroups', 'profile_title_own', 'profile_title_any')); |
||
604 | $context['allow_memberlist'] = allowedTo('view_mlist'); |
||
605 | $context['allow_calendar'] = allowedTo('calendar_view') && !empty($modSettings['cal_enabled']); |
||
606 | $context['allow_moderation_center'] = $context['user']['can_mod']; |
||
607 | $context['allow_pm'] = allowedTo('pm_read'); |
||
608 | |||
609 | if ($context['allow_search']) |
||
610 | $context['theme_header_callbacks'] = elk_array_insert($context['theme_header_callbacks'], 'login_bar', array('search_bar'), 'after'); |
||
611 | |||
612 | $cacheTime = $modSettings['lastActive'] * 60; |
||
613 | |||
614 | // Update the Moderation menu items with action item totals |
||
615 | if ($context['allow_moderation_center']) |
||
616 | { |
||
617 | // Get the numbers for the menu ... |
||
618 | require_once(SUBSDIR . '/Moderation.subs.php'); |
||
619 | $menu_count = loadModeratorMenuCounts(); |
||
620 | } |
||
621 | |||
622 | $menu_count['unread_messages'] = $context['user']['unread_messages']; |
||
623 | $menu_count['mentions'] = $context['user']['mentions']; |
||
624 | |||
625 | // All the buttons we can possible want and then some, try pulling the final list of buttons from cache first. |
||
626 | if (($menu_buttons = cache_get_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $cacheTime)) === null || time() - $cacheTime <= $modSettings['settings_updated']) |
||
627 | { |
||
628 | // Start things up: this is what we know by default |
||
629 | require_once(SUBSDIR . '/Menu.subs.php'); |
||
630 | $buttons = array( |
||
631 | 'home' => array( |
||
632 | 'title' => $txt['community'], |
||
633 | 'href' => $scripturl, |
||
634 | 'data-icon' => '', |
||
635 | 'show' => true, |
||
636 | 'sub_buttons' => array( |
||
637 | 'help' => array( |
||
638 | 'title' => $txt['help'], |
||
639 | 'href' => $scripturl . '?action=help', |
||
640 | 'show' => true, |
||
641 | ), |
||
642 | 'search' => array( |
||
643 | 'title' => $txt['search'], |
||
644 | 'href' => $scripturl . '?action=search', |
||
645 | 'show' => $context['allow_search'], |
||
646 | ), |
||
647 | 'calendar' => array( |
||
648 | 'title' => $txt['calendar'], |
||
649 | 'href' => $scripturl . '?action=calendar', |
||
650 | 'show' => $context['allow_calendar'], |
||
651 | ), |
||
652 | 'memberlist' => array( |
||
653 | 'title' => $txt['members_title'], |
||
654 | 'href' => $scripturl . '?action=memberlist', |
||
655 | 'show' => $context['allow_memberlist'], |
||
656 | ), |
||
657 | 'recent' => array( |
||
658 | 'title' => $txt['recent_posts'], |
||
659 | 'href' => $scripturl . '?action=recent', |
||
660 | 'show' => true, |
||
661 | ), |
||
662 | ), |
||
663 | ) |
||
664 | ); |
||
665 | |||
666 | // Will change title correctly if user is either a mod or an admin. |
||
667 | // Button highlighting works properly too (see current action stuffz). |
||
668 | if ($context['allow_admin']) |
||
669 | { |
||
670 | $buttons['admin'] = array( |
||
671 | 'title' => $context['current_action'] !== 'moderate' ? $txt['admin'] : $txt['moderate'], |
||
672 | 'counter' => 'grand_total', |
||
673 | 'href' => $scripturl . '?action=admin', |
||
674 | 'data-icon' => '', |
||
675 | 'show' => true, |
||
676 | 'sub_buttons' => array( |
||
677 | 'admin_center' => array( |
||
678 | 'title' => $txt['admin_center'], |
||
679 | 'href' => $scripturl . '?action=admin', |
||
680 | 'show' => $context['allow_admin'], |
||
681 | ), |
||
682 | 'featuresettings' => array( |
||
683 | 'title' => $txt['modSettings_title'], |
||
684 | 'href' => $scripturl . '?action=admin;area=featuresettings', |
||
685 | 'show' => allowedTo('admin_forum'), |
||
686 | ), |
||
687 | 'packages' => array( |
||
688 | 'title' => $txt['package'], |
||
689 | 'href' => $scripturl . '?action=admin;area=packages', |
||
690 | 'show' => allowedTo('admin_forum'), |
||
691 | ), |
||
692 | 'permissions' => array( |
||
693 | 'title' => $txt['edit_permissions'], |
||
694 | 'href' => $scripturl . '?action=admin;area=permissions', |
||
695 | 'show' => allowedTo('manage_permissions'), |
||
696 | ), |
||
697 | 'errorlog' => array( |
||
698 | 'title' => $txt['errlog'], |
||
699 | 'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;desc', |
||
700 | 'show' => allowedTo('admin_forum') && !empty($modSettings['enableErrorLogging']), |
||
701 | ), |
||
702 | 'moderate_sub' => array( |
||
703 | 'title' => $txt['moderate'], |
||
704 | 'counter' => 'grand_total', |
||
705 | 'href' => $scripturl . '?action=moderate', |
||
706 | 'show' => $context['allow_moderation_center'], |
||
707 | 'sub_buttons' => array( |
||
708 | 'reports' => array( |
||
709 | 'title' => $txt['mc_reported_posts'], |
||
710 | 'counter' => 'reports', |
||
711 | 'href' => $scripturl . '?action=moderate;area=reports', |
||
712 | 'show' => !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1', |
||
713 | ), |
||
714 | 'modlog' => array( |
||
715 | 'title' => $txt['modlog_view'], |
||
716 | 'href' => $scripturl . '?action=moderate;area=modlog', |
||
717 | 'show' => !empty($modSettings['modlog_enabled']) && !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1', |
||
718 | ), |
||
719 | 'attachments' => array( |
||
720 | 'title' => $txt['mc_unapproved_attachments'], |
||
721 | 'counter' => 'attachments', |
||
722 | 'href' => $scripturl . '?action=moderate;area=attachmod;sa=attachments', |
||
723 | 'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']), |
||
724 | ), |
||
725 | 'poststopics' => array( |
||
726 | 'title' => $txt['mc_unapproved_poststopics'], |
||
727 | 'counter' => 'postmod', |
||
728 | 'href' => $scripturl . '?action=moderate;area=postmod;sa=posts', |
||
729 | 'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']), |
||
730 | ), |
||
731 | 'postbyemail' => array( |
||
732 | 'title' => $txt['mc_emailerror'], |
||
733 | 'counter' => 'emailmod', |
||
734 | 'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist', |
||
735 | 'show' => !empty($modSettings['maillist_enabled']) && allowedTo('approve_emails'), |
||
736 | ), |
||
737 | ), |
||
738 | ), |
||
739 | ), |
||
740 | ); |
||
741 | } |
||
742 | else |
||
743 | { |
||
744 | $buttons['admin'] = array( |
||
745 | 'title' => $txt['moderate'], |
||
746 | 'counter' => 'grand_total', |
||
747 | 'href' => $scripturl . '?action=moderate', |
||
748 | 'data-icon' => '', |
||
749 | 'show' => $context['allow_moderation_center'], |
||
750 | 'sub_buttons' => array( |
||
751 | 'reports' => array( |
||
752 | 'title' => $txt['mc_reported_posts'], |
||
753 | 'counter' => 'reports', |
||
754 | 'href' => $scripturl . '?action=moderate;area=reports', |
||
755 | 'show' => !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1', |
||
756 | ), |
||
757 | 'modlog' => array( |
||
758 | 'title' => $txt['modlog_view'], |
||
759 | 'href' => $scripturl . '?action=moderate;area=modlog', |
||
760 | 'show' => !empty($modSettings['modlog_enabled']) && !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1', |
||
761 | ), |
||
762 | 'attachments' => array( |
||
763 | 'title' => $txt['mc_unapproved_attachments'], |
||
764 | 'counter' => 'attachments', |
||
765 | 'href' => $scripturl . '?action=moderate;area=attachmod;sa=attachments', |
||
766 | 'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']), |
||
767 | ), |
||
768 | 'poststopics' => array( |
||
769 | 'title' => $txt['mc_unapproved_poststopics'], |
||
770 | 'counter' => 'postmod', |
||
771 | 'href' => $scripturl . '?action=moderate;area=postmod;sa=posts', |
||
772 | 'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']), |
||
773 | ), |
||
774 | 'postbyemail' => array( |
||
775 | 'title' => $txt['mc_emailerror'], |
||
776 | 'counter' => 'emailmod', |
||
777 | 'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist', |
||
778 | 'show' => !empty($modSettings['maillist_enabled']) && allowedTo('approve_emails'), |
||
779 | ), |
||
780 | ), |
||
781 | ); |
||
782 | } |
||
783 | |||
784 | $buttons += array( |
||
785 | 'profile' => array( |
||
786 | 'title' => (!empty($user_info['avatar']['href']) ? '<img class="avatar" src="' . $user_info['avatar']['href'] . '" alt="" /> ' : '') . (!empty($modSettings['displayMemberNames']) ? $user_info['name'] : $txt['account_short']), |
||
787 | 'href' => $scripturl . '?action=profile', |
||
788 | 'data-icon' => '', |
||
789 | 'show' => $context['allow_edit_profile'], |
||
790 | 'sub_buttons' => array( |
||
791 | 'account' => array( |
||
792 | 'title' => $txt['account'], |
||
793 | 'href' => $scripturl . '?action=profile;area=account', |
||
794 | 'show' => allowedTo(array('profile_identity_any', 'profile_identity_own', 'manage_membergroups')), |
||
795 | ), |
||
796 | 'forumprofile' => array( |
||
797 | 'title' => $txt['forumprofile'], |
||
798 | 'href' => $scripturl . '?action=profile;area=forumprofile', |
||
799 | 'show' => allowedTo(array('profile_extra_any', 'profile_extra_own')), |
||
800 | ), |
||
801 | 'theme' => array( |
||
802 | 'title' => $txt['theme'], |
||
803 | 'href' => $scripturl . '?action=profile;area=theme', |
||
804 | 'show' => allowedTo(array('profile_extra_any', 'profile_extra_own', 'profile_extra_any')), |
||
805 | ), |
||
806 | 'logout' => array( |
||
807 | 'title' => $txt['logout'], |
||
808 | 'href' => $scripturl . '?action=logout', |
||
809 | 'show' => !$user_info['is_guest'], |
||
810 | ), |
||
811 | ), |
||
812 | ), |
||
813 | // @todo Look at doing something here, to provide instant access to inbox when using click menus. |
||
814 | // @todo A small pop-up anchor seems like the obvious way to handle it. ;) |
||
815 | 'pm' => array( |
||
816 | 'title' => $txt['pm_short'], |
||
817 | 'counter' => 'unread_messages', |
||
818 | 'href' => $scripturl . '?action=pm', |
||
819 | 'data-icon' => '', |
||
820 | 'show' => $context['allow_pm'], |
||
821 | 'sub_buttons' => array( |
||
822 | 'pm_read' => array( |
||
823 | 'title' => $txt['pm_menu_read'], |
||
824 | 'href' => $scripturl . '?action=pm', |
||
825 | 'show' => allowedTo('pm_read'), |
||
826 | ), |
||
827 | 'pm_send' => array( |
||
828 | 'title' => $txt['pm_menu_send'], |
||
829 | 'href' => $scripturl . '?action=pm;sa=send', |
||
830 | 'show' => allowedTo('pm_send'), |
||
831 | ), |
||
832 | ), |
||
833 | ), |
||
834 | 'mentions' => array( |
||
835 | 'title' => $txt['mention'], |
||
836 | 'counter' => 'mentions', |
||
837 | 'href' => $scripturl . '?action=mentions', |
||
838 | 'data-icon' => '', |
||
839 | 'show' => !$user_info['is_guest'] && !empty($modSettings['mentions_enabled']), |
||
840 | ), |
||
841 | // The old language string made no sense, and was too long. |
||
842 | // "New posts" is better, because there are probably a pile |
||
843 | // of old unread posts, and they wont be reached from this button. |
||
844 | 'unread' => array( |
||
845 | 'title' => $txt['view_unread_category'], |
||
846 | 'href' => $scripturl . '?action=unread', |
||
847 | 'data-icon' => '', |
||
848 | 'show' => !$user_info['is_guest'], |
||
849 | ), |
||
850 | // The old language string made no sense, and was too long. |
||
851 | // "New replies" is better, because there are "updated topics" |
||
852 | // that the user has never posted in and doesn't care about. |
||
853 | 'unreadreplies' => array( |
||
854 | 'title' => $txt['view_replies_category'], |
||
855 | 'href' => $scripturl . '?action=unreadreplies', |
||
856 | 'data-icon' => '', |
||
857 | 'show' => !$user_info['is_guest'], |
||
858 | ), |
||
859 | 'login' => array( |
||
860 | 'title' => $txt['login'], |
||
861 | 'href' => $scripturl . '?action=login', |
||
862 | 'data-icon' => '', |
||
863 | 'show' => $user_info['is_guest'], |
||
864 | ), |
||
865 | |||
866 | 'register' => array( |
||
867 | 'title' => $txt['register'], |
||
868 | 'href' => $scripturl . '?action=register', |
||
869 | 'data-icon' => '', |
||
870 | 'show' => $user_info['is_guest'] && $context['can_register'], |
||
871 | ), |
||
872 | 'like_stats' => array( |
||
873 | 'title' => $txt['like_post_stats'], |
||
874 | 'href' => $scripturl . '?action=likes;sa=likestats', |
||
875 | // 'data-icon' => '', |
||
876 | 'show' => allowedTo('like_posts_stats'), |
||
877 | ), |
||
878 | 'contact' => array( |
||
879 | 'title' => $txt['contact'], |
||
880 | 'href' => $scripturl . '?action=register;sa=contact', |
||
881 | 'data-icon' => '', |
||
882 | 'show' => $user_info['is_guest'] && !empty($modSettings['enable_contactform']) && $modSettings['enable_contactform'] == 'menu', |
||
883 | ), |
||
884 | ); |
||
885 | |||
886 | // Allow editing menu buttons easily. |
||
887 | call_integration_hook('integrate_menu_buttons', array(&$buttons, &$menu_count)); |
||
888 | |||
889 | // Now we put the buttons in the context so the theme can use them. |
||
890 | $menu_buttons = array(); |
||
891 | foreach ($buttons as $act => $button) |
||
892 | { |
||
893 | if (!empty($button['show'])) |
||
894 | { |
||
895 | $button['active_button'] = false; |
||
896 | |||
897 | // This button needs some action. |
||
898 | if (isset($button['action_hook'])) |
||
899 | $needs_action_hook = true; |
||
900 | |||
901 | if (isset($button['counter']) && !empty($menu_count[$button['counter']])) |
||
902 | { |
||
903 | $button['alttitle'] = $button['title'] . ' [' . $menu_count[$button['counter']] . ']'; |
||
904 | if (!empty($settings['menu_numeric_notice'][0])) |
||
905 | { |
||
906 | $button['title'] .= sprintf($settings['menu_numeric_notice'][0], $menu_count[$button['counter']]); |
||
907 | $button['indicator'] = true; |
||
908 | } |
||
909 | } |
||
910 | |||
911 | // Go through the sub buttons if there are any. |
||
912 | if (isset($button['sub_buttons'])) |
||
913 | { |
||
914 | foreach ($button['sub_buttons'] as $key => $subbutton) |
||
915 | { |
||
916 | if (empty($subbutton['show'])) |
||
917 | unset($button['sub_buttons'][$key]); |
||
918 | elseif (isset($subbutton['counter']) && !empty($menu_count[$subbutton['counter']])) |
||
919 | { |
||
920 | $button['sub_buttons'][$key]['alttitle'] = $subbutton['title'] . ' [' . $menu_count[$subbutton['counter']] . ']'; |
||
921 | if (!empty($settings['menu_numeric_notice'][1])) |
||
922 | $button['sub_buttons'][$key]['title'] .= sprintf($settings['menu_numeric_notice'][1], $menu_count[$subbutton['counter']]); |
||
923 | |||
924 | // 2nd level sub buttons next... |
||
925 | if (isset($subbutton['sub_buttons'])) |
||
926 | { |
||
927 | foreach ($subbutton['sub_buttons'] as $key2 => $subbutton2) |
||
928 | { |
||
929 | $button['sub_buttons'][$key]['sub_buttons'][$key2] = $subbutton2; |
||
930 | if (empty($subbutton2['show'])) |
||
931 | unset($button['sub_buttons'][$key]['sub_buttons'][$key2]); |
||
932 | elseif (isset($subbutton2['counter']) && !empty($menu_count[$subbutton2['counter']])) |
||
933 | { |
||
934 | $button['sub_buttons'][$key]['sub_buttons'][$key2]['alttitle'] = $subbutton2['title'] . ' [' . $menu_count[$subbutton2['counter']] . ']'; |
||
935 | if (!empty($settings['menu_numeric_notice'][2])) |
||
936 | $button['sub_buttons'][$key]['sub_buttons'][$key2]['title'] .= sprintf($settings['menu_numeric_notice'][2], $menu_count[$subbutton2['counter']]); |
||
937 | unset($menu_count[$subbutton2['counter']]); |
||
938 | } |
||
939 | } |
||
940 | } |
||
941 | } |
||
942 | } |
||
943 | } |
||
944 | |||
945 | $menu_buttons[$act] = $button; |
||
946 | } |
||
947 | } |
||
948 | |||
949 | if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) |
||
950 | cache_put_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $menu_buttons, $cacheTime); |
||
951 | } |
||
952 | |||
953 | if (!empty($menu_buttons['profile']['sub_buttons']['logout'])) |
||
954 | $menu_buttons['profile']['sub_buttons']['logout']['href'] .= ';' . $context['session_var'] . '=' . $context['session_id']; |
||
955 | |||
956 | $context['menu_buttons'] = $menu_buttons; |
||
957 | |||
958 | // Figure out which action we are doing so we can set the active tab. |
||
959 | // Default to home. |
||
960 | $current_action = 'home'; |
||
961 | |||
962 | if (isset($context['menu_buttons'][$context['current_action']])) |
||
963 | $current_action = $context['current_action']; |
||
964 | elseif ($context['current_action'] == 'profile') |
||
965 | $current_action = 'pm'; |
||
966 | elseif ($context['current_action'] == 'theme') |
||
967 | $current_action = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' ? 'profile' : 'admin'; |
||
968 | elseif ($context['current_action'] == 'login2' || ($user_info['is_guest'] && $context['current_action'] == 'reminder')) |
||
969 | $current_action = 'login'; |
||
970 | elseif ($context['current_action'] == 'groups' && $context['allow_moderation_center']) |
||
971 | $current_action = 'moderate'; |
||
972 | elseif ($context['current_action'] == 'moderate' && $context['allow_admin']) |
||
973 | $current_action = 'admin'; |
||
974 | |||
975 | // Not all actions are simple. |
||
976 | if (!empty($needs_action_hook)) |
||
977 | call_integration_hook('integrate_current_action', array(&$current_action)); |
||
978 | |||
979 | if (isset($context['menu_buttons'][$current_action])) |
||
980 | $context['menu_buttons'][$current_action]['active_button'] = true; |
||
981 | } |
||
982 | |||
1111 | } |
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.