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