| Conditions | 8 |
| Paths | 6 |
| Total Lines | 85 |
| Code Lines | 42 |
| 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 |
||
| 92 | public function renderRegion($region) |
||
| 93 | { |
||
| 94 | if ( |
||
| 95 | 'main_bottom' !== $region |
||
| 96 | || strpos($_SERVER['SCRIPT_NAME'], 'course_home/course_home.php') === false |
||
| 97 | ) { |
||
| 98 | return ''; |
||
| 99 | } |
||
| 100 | |||
| 101 | $courseId = api_get_course_int_id(); |
||
| 102 | $userId = api_get_user_id(); |
||
| 103 | |||
| 104 | if (empty($courseId) || empty($userId)) { |
||
| 105 | return ''; |
||
| 106 | } |
||
| 107 | |||
| 108 | $course = api_get_course_entity($courseId); |
||
| 109 | $user = api_get_user_entity($userId); |
||
| 110 | |||
| 111 | $em = Database::getManager(); |
||
| 112 | /** @var Notification $notification */ |
||
| 113 | $notification = $em |
||
| 114 | ->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification') |
||
| 115 | ->findOneBy(['course' => $course]); |
||
| 116 | |||
| 117 | if (!$notification) { |
||
| 118 | return ''; |
||
| 119 | } |
||
| 120 | |||
| 121 | $modalFooter = ''; |
||
| 122 | $modalConfig = ['show' => true]; |
||
| 123 | |||
| 124 | if ($notification->getExpirationLink()) { |
||
| 125 | /** @var NotificationRelUser $notificationUser */ |
||
| 126 | $notificationUser = $em |
||
| 127 | ->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser') |
||
| 128 | ->findOneBy(['notification' => $notification, 'user' => $user]); |
||
| 129 | |||
| 130 | if ($notificationUser) { |
||
| 131 | return ''; |
||
| 132 | } |
||
| 133 | |||
| 134 | $contentUrl = api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/content.php?hash='.$notification->getHash(); |
||
| 135 | $link = Display::toolbarButton( |
||
| 136 | $this->get_lang('PleaseFollowThisLink'), |
||
| 137 | $contentUrl, |
||
| 138 | 'external-link', |
||
| 139 | 'link', |
||
| 140 | ['id' => 'course-home-notify-link', 'target' => '_blank'] |
||
| 141 | ); |
||
| 142 | |||
| 143 | $modalConfig['keyboard'] = false; |
||
| 144 | $modalConfig['backdrop'] = 'static'; |
||
| 145 | |||
| 146 | $modalFooter = '<div class="modal-footer">'.$link.'</div>'; |
||
| 147 | } |
||
| 148 | |||
| 149 | $modal = '<div id="course-home-notify-modal" class="modal" tabindex="-1" role="dialog"> |
||
| 150 | <div class="modal-dialog" role="document"> |
||
| 151 | <div class="modal-content"> |
||
| 152 | <div class="modal-header"> |
||
| 153 | <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'"> |
||
| 154 | <span aria-hidden="true">×</span> |
||
| 155 | </button> |
||
| 156 | <h4 class="modal-title">'.$this->get_lang('CourseNotice').'</h4> |
||
| 157 | </div> |
||
| 158 | <div class="modal-body"> |
||
| 159 | '.$notification->getContent().' |
||
| 160 | </div> |
||
| 161 | '.$modalFooter.' |
||
| 162 | </div> |
||
| 163 | </div> |
||
| 164 | </div>'; |
||
| 165 | |||
| 166 | $modal .= "<script> |
||
| 167 | $(document).ready(function () { |
||
| 168 | \$('#course-home-notify-modal').modal(".json_encode($modalConfig)."); |
||
| 169 | |||
| 170 | \$('#course-home-notify-link').on('click', function () { |
||
| 171 | $('#course-home-notify-modal').modal('hide'); |
||
| 172 | }); |
||
| 173 | }); |
||
| 174 | </script>"; |
||
| 175 | |||
| 176 | return $modal; |
||
| 177 | } |
||
| 205 |
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: