Conditions | 17 |
Paths | 3460 |
Total Lines | 108 |
Code Lines | 65 |
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 |
||
218 | public function fixItemProperty( |
||
219 | $tool, |
||
220 | ResourceRepository $repo, |
||
221 | $course, |
||
222 | $admin, |
||
223 | ResourceInterface $resource, |
||
224 | $parentResource, |
||
225 | array $items = [] |
||
226 | ) { |
||
227 | $courseId = $course->getId(); |
||
228 | $id = $resource->getResourceIdentifier(); |
||
229 | |||
230 | if (empty($items)) { |
||
231 | $sql = "SELECT * FROM c_item_property |
||
232 | WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}"; |
||
233 | $result = $this->connection->executeQuery($sql); |
||
234 | $items = $result->fetchAllAssociative(); |
||
235 | } |
||
236 | |||
237 | // For some reason the resource doesn't have a c_item_property value. |
||
238 | if (empty($items)) { |
||
239 | return false; |
||
240 | } |
||
241 | |||
242 | $sessionRepo = $this->container->get(SessionRepository::class); |
||
243 | $groupRepo = $this->container->get(CGroupRepository::class); |
||
244 | $userRepo = $this->container->get(UserRepository::class); |
||
245 | |||
246 | $resource->setParent($parentResource); |
||
247 | $resourceNode = null; |
||
248 | $userList = []; |
||
249 | $groupList = []; |
||
250 | $sessionList = []; |
||
251 | foreach ($items as $item) { |
||
252 | $visibility = (int) $item['visibility']; |
||
253 | $userId = (int) $item['insert_user_id']; |
||
254 | $sessionId = $item['session_id'] ?? 0; |
||
255 | $groupId = $item['to_group_id'] ?? 0; |
||
256 | if (empty($item['lastedit_date'])) { |
||
257 | $lastUpdatedAt = new DateTime('now', new DateTimeZone('UTC')); |
||
258 | } else { |
||
259 | $lastUpdatedAt = new DateTime($item['lastedit_date'], new DateTimeZone('UTC')); |
||
260 | } |
||
261 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
262 | |||
263 | // Old 1.11.x visibility (item property) is based in this switch: |
||
264 | switch ($visibility) { |
||
265 | case 0: |
||
266 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
267 | |||
268 | break; |
||
269 | |||
270 | case 1: |
||
271 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
272 | |||
273 | break; |
||
274 | } |
||
275 | |||
276 | // If c_item_property.insert_user_id doesn't exist we use the first admin id. |
||
277 | $user = null; |
||
278 | if (isset($userList[$userId])) { |
||
279 | $user = $userList[$userId]; |
||
280 | } else { |
||
281 | if (!empty($userId)) { |
||
282 | $userFound = $userRepo->find($userId); |
||
283 | if ($userFound) { |
||
284 | $user = $userList[$userId] = $userRepo->find($userId); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | if (null === $user) { |
||
290 | $user = $admin; |
||
291 | } |
||
292 | |||
293 | $session = null; |
||
294 | if (!empty($sessionId)) { |
||
295 | if (isset($sessionList[$sessionId])) { |
||
296 | $session = $sessionList[$sessionId]; |
||
297 | } else { |
||
298 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | $group = null; |
||
303 | if (!empty($groupId)) { |
||
304 | if (isset($groupList[$groupId])) { |
||
305 | $group = $groupList[$groupId]; |
||
306 | } else { |
||
307 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | if (null === $resourceNode) { |
||
312 | $resourceNode = $repo->addResourceNode($resource, $user, $parentResource); |
||
313 | $this->entityManager->persist($resourceNode); |
||
314 | } |
||
315 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
316 | |||
317 | if (2 === $visibility) { |
||
318 | $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group); |
||
319 | $link->setDeletedAt($lastUpdatedAt); |
||
320 | } |
||
321 | |||
322 | $this->entityManager->persist($resource); |
||
323 | } |
||
324 | |||
325 | return true; |
||
326 | } |
||
375 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.