| Conditions | 19 |
| Paths | 37 |
| Total Lines | 98 |
| Code Lines | 50 |
| 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 |
||
| 28 | public static function setLinks(AbstractResource $resource, $em): void |
||
| 29 | { |
||
| 30 | $resourceNode = $resource->getResourceNode(); |
||
| 31 | $links = $resource->getResourceLinkArray(); |
||
| 32 | if ($links) { |
||
|
|
|||
| 33 | $groupRepo = $em->getRepository(CGroup::class); |
||
| 34 | $courseRepo = $em->getRepository(Course::class); |
||
| 35 | $sessionRepo = $em->getRepository(Session::class); |
||
| 36 | $userRepo = $em->getRepository(User::class); |
||
| 37 | |||
| 38 | foreach ($links as $link) { |
||
| 39 | $resourceLink = new ResourceLink(); |
||
| 40 | $linkSet = false; |
||
| 41 | if (isset($link['cid']) && !empty($link['cid'])) { |
||
| 42 | $course = $courseRepo->find($link['cid']); |
||
| 43 | if (null !== $course) { |
||
| 44 | $linkSet = true; |
||
| 45 | $resourceLink->setCourse($course); |
||
| 46 | } else { |
||
| 47 | throw new InvalidArgumentException(sprintf('Course #%s does not exists', $link['cid'])); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if (isset($link['sid']) && !empty($link['sid'])) { |
||
| 52 | $session = $sessionRepo->find($link['sid']); |
||
| 53 | if (null !== $session) { |
||
| 54 | $linkSet = true; |
||
| 55 | $resourceLink->setSession($session); |
||
| 56 | } else { |
||
| 57 | throw new InvalidArgumentException(sprintf('Session #%s does not exists', $link['sid'])); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if (isset($link['gid']) && !empty($link['gid'])) { |
||
| 62 | $group = $groupRepo->find($link['gid']); |
||
| 63 | if (null !== $group) { |
||
| 64 | $linkSet = true; |
||
| 65 | $resourceLink->setGroup($group); |
||
| 66 | } else { |
||
| 67 | throw new InvalidArgumentException(sprintf('Group #%s does not exists', $link['gid'])); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if (isset($link['uid']) && !empty($link['uid'])) { |
||
| 72 | $user = $userRepo->find($link['uid']); |
||
| 73 | if (null !== $user) { |
||
| 74 | $linkSet = true; |
||
| 75 | $resourceLink->setUser($user); |
||
| 76 | } else { |
||
| 77 | throw new InvalidArgumentException(sprintf('User #%s does not exists', $link['uid'])); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($link['visibility'])) { |
||
| 82 | $resourceLink->setVisibility((int) $link['visibility']); |
||
| 83 | } else { |
||
| 84 | throw new InvalidArgumentException('Link needs a visibility key'); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($linkSet) { |
||
| 88 | $em->persist($resourceLink); |
||
| 89 | $resourceNode->addResourceLink($resourceLink); |
||
| 90 | //$em->persist($resourceNode); |
||
| 91 | //$em->persist($resource->getResourceNode()); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // Use by Chamilo not api platform. |
||
| 97 | $links = $resource->getResourceLinkEntityList(); |
||
| 98 | if ($links) { |
||
| 99 | //error_log('$resource->getResourceLinkEntityList()'); |
||
| 100 | foreach ($links as $link) { |
||
| 101 | /*$rights = []; |
||
| 102 | switch ($link->getVisibility()) { |
||
| 103 | case ResourceLink::VISIBILITY_PENDING: |
||
| 104 | case ResourceLink::VISIBILITY_DRAFT: |
||
| 105 | $editorMask = ResourceNodeVoter::getEditorMask(); |
||
| 106 | $resourceRight = new ResourceRight(); |
||
| 107 | $resourceRight |
||
| 108 | ->setMask($editorMask) |
||
| 109 | ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER) |
||
| 110 | ; |
||
| 111 | $rights[] = $resourceRight; |
||
| 112 | |||
| 113 | break; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!empty($rights)) { |
||
| 117 | foreach ($rights as $right) { |
||
| 118 | $link->addResourceRight($right); |
||
| 119 | } |
||
| 120 | }*/ |
||
| 121 | //error_log('link adding to node: '.$resource->getResourceNode()->getId()); |
||
| 122 | //error_log('link with user : '.$link->getUser()->getUsername()); |
||
| 123 | $resource->getResourceNode()->addResourceLink($link); |
||
| 124 | |||
| 125 | $em->persist($link); |
||
| 126 | } |
||
| 286 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.