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