| Conditions | 25 |
| Paths | 7296 |
| Total Lines | 101 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 49 | public function getData(int $glossaryId, int $sectionId, ?int $moduleId = null): array |
||
| 50 | { |
||
| 51 | if ($moduleId === null) { |
||
| 52 | $moduleId = $glossaryId; |
||
| 53 | } |
||
| 54 | |||
| 55 | $adminData = MoodleExport::getAdminUserData(); |
||
| 56 | $adminId = (int) ($adminData['id'] ?? 0); |
||
| 57 | |||
| 58 | $res = \is_array($this->course->resources ?? null) ? $this->course->resources : []; |
||
| 59 | $bags = []; |
||
| 60 | if (\defined('RESOURCE_GLOSSARY') && !empty($res[RESOURCE_GLOSSARY]) && \is_array($res[RESOURCE_GLOSSARY])) { |
||
| 61 | $bags[] = $res[RESOURCE_GLOSSARY]; |
||
| 62 | } |
||
| 63 | foreach (['glossary', 'glossary_definition', 'glossary_terms'] as $k) { |
||
| 64 | if (!empty($res[$k]) && \is_array($res[$k])) { |
||
| 65 | $bags[] = $res[$k]; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | $entries = []; |
||
| 70 | $seen = []; |
||
| 71 | $nextId = 1; |
||
| 72 | $userIds = []; |
||
| 73 | |||
| 74 | $norm = static function (string $s): string { |
||
| 75 | $s = trim($s); |
||
| 76 | $s = mb_strtolower($s, 'UTF-8'); |
||
| 77 | return $s; |
||
| 78 | }; |
||
| 79 | |||
| 80 | foreach ($bags as $bag) { |
||
| 81 | foreach ($bag as $g) { |
||
| 82 | $o = (\is_object($g) && isset($g->obj) && \is_object($g->obj)) ? $g->obj : $g; |
||
| 83 | if (!\is_object($o)) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | $concept = ''; |
||
| 88 | foreach (['name', 'term', 'title'] as $k) { |
||
| 89 | if (!empty($o->{$k}) && \is_string($o->{$k})) { |
||
| 90 | $concept = trim((string) $o->{$k}); |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | if ($concept === '') { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $key = $norm($concept); |
||
| 99 | if (isset($seen[$key])) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | $seen[$key] = true; |
||
| 103 | |||
| 104 | $definition = ''; |
||
| 105 | foreach (['description','definition','comment','text'] as $k) { |
||
| 106 | if (isset($o->{$k}) && \is_string($o->{$k})) { |
||
| 107 | $definition = (string) $o->{$k}; |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | $aliases = []; |
||
| 113 | $lc = mb_strtolower($concept, 'UTF-8'); |
||
| 114 | if ($lc !== $concept) { |
||
| 115 | $aliases[] = $lc; |
||
| 116 | } |
||
| 117 | |||
| 118 | $entries[] = [ |
||
| 119 | 'id' => $nextId++, |
||
| 120 | 'userid' => $adminId, |
||
| 121 | 'concept' => $concept, |
||
| 122 | 'definition' => $definition, |
||
| 123 | 'timecreated' => time(), |
||
| 124 | 'timemodified' => time(), |
||
| 125 | 'aliases' => $aliases, |
||
| 126 | ]; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($adminId > 0) { |
||
| 131 | $userIds[$adminId] = true; |
||
| 132 | } |
||
| 133 | |||
| 134 | return [ |
||
| 135 | 'id' => $glossaryId, |
||
| 136 | 'moduleid' => (int) $moduleId, |
||
| 137 | 'modulename' => 'glossary', |
||
| 138 | 'contextid' => (int) ($this->course->info['real_id'] ?? 0), |
||
| 139 | 'name' => get_lang('Glossary'), |
||
| 140 | 'description' => '', |
||
| 141 | 'timecreated' => time(), |
||
| 142 | 'timemodified' => time(), |
||
| 143 | 'sectionid' => $sectionId, |
||
| 144 | 'sectionnumber' => 0, |
||
| 145 | 'userid' => $adminId, |
||
| 146 | 'entries' => $entries, |
||
| 147 | 'users' => array_map('intval', array_keys($userIds)), |
||
| 148 | 'files' => [], |
||
| 149 | 'include_userinfo' => true, |
||
| 150 | ]; |
||
| 237 |