| Conditions | 10 |
| Paths | 13 |
| Total Lines | 93 |
| Code Lines | 72 |
| 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 |
||
| 45 | public function getData(int $pageId, int $sectionId): ?array |
||
| 46 | { |
||
| 47 | $contextid = $this->course->info['real_id']; |
||
| 48 | if ($pageId === 0) { |
||
| 49 | $introText = trim($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']->intro_text ?? ''); |
||
| 50 | |||
| 51 | if (!empty($introText)) { |
||
| 52 | $files = []; |
||
| 53 | $resources = \DocumentManager::get_resources_from_source_html($introText); |
||
| 54 | $courseInfo = api_get_course_info($this->course->code); |
||
| 55 | $adminId = MoodleExport::getAdminUserData()['id']; |
||
| 56 | |||
| 57 | foreach ($resources as [$src]) { |
||
| 58 | if (preg_match('#/document(/[^"\']+)#', $src, $matches)) { |
||
| 59 | $path = $matches[1]; |
||
| 60 | $docId = \DocumentManager::get_document_id($courseInfo, $path); |
||
| 61 | if ($docId) { |
||
| 62 | $this->course->used_page_doc_ids[] = $docId; |
||
| 63 | $document = \DocumentManager::get_document_data_by_id($docId, $this->course->code); |
||
| 64 | if ($document) { |
||
|
|
|||
| 65 | $contenthash = hash('sha1', basename($document['path'])); |
||
| 66 | $mimetype = (new FileExport($this->course))->getMimeType($document['path']); |
||
| 67 | |||
| 68 | $files[] = [ |
||
| 69 | 'id' => $document['id'], |
||
| 70 | 'contenthash' => $contenthash, |
||
| 71 | 'contextid' => $contextid, |
||
| 72 | 'component' => 'mod_page', |
||
| 73 | 'filearea' => 'content', |
||
| 74 | 'itemid' => 1, |
||
| 75 | 'filepath' => '/Documents/', |
||
| 76 | 'documentpath' => 'document' . $document['path'], |
||
| 77 | 'filename' => basename($document['path']), |
||
| 78 | 'userid' => $adminId, |
||
| 79 | 'filesize' => $document['size'], |
||
| 80 | 'mimetype' => $mimetype, |
||
| 81 | 'status' => 0, |
||
| 82 | 'timecreated' => time() - 3600, |
||
| 83 | 'timemodified' => time(), |
||
| 84 | 'source' => $document['title'], |
||
| 85 | 'author' => 'Unknown', |
||
| 86 | 'license' => 'allrightsreserved', |
||
| 87 | ]; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return [ |
||
| 94 | 'id' => 0, |
||
| 95 | 'moduleid' => 0, |
||
| 96 | 'modulename' => 'page', |
||
| 97 | 'contextid' => $contextid, |
||
| 98 | 'name' => get_lang('Introduction'), |
||
| 99 | 'intro' => '', |
||
| 100 | 'content' => $this->normalizeContent($introText), |
||
| 101 | 'sectionid' => $sectionId, |
||
| 102 | 'sectionnumber' => 1, |
||
| 103 | 'display' => 0, |
||
| 104 | 'timemodified' => time(), |
||
| 105 | 'users' => [], |
||
| 106 | 'files' => $files, |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $pageResources = $this->course->resources[RESOURCE_DOCUMENT] ?? []; |
||
| 112 | foreach ($pageResources as $page) { |
||
| 113 | if ($page->source_id == $pageId) { |
||
| 114 | $name = $page->title ?? ''; |
||
| 115 | if ($sectionId > 0) { |
||
| 116 | $name = $this->lpItemTitle($sectionId, RESOURCE_DOCUMENT, $page->source_id, $name); |
||
| 117 | } |
||
| 118 | |||
| 119 | return [ |
||
| 120 | 'id' => $page->source_id, |
||
| 121 | 'moduleid' => $page->source_id, |
||
| 122 | 'modulename' => 'page', |
||
| 123 | 'contextid' => $contextid, |
||
| 124 | 'name' => $name, |
||
| 125 | 'intro' => $page->comment ?? '', |
||
| 126 | 'content' => $this->normalizeContent($this->getPageContent($page)), |
||
| 127 | 'sectionid' => $sectionId, |
||
| 128 | 'sectionnumber' => 1, |
||
| 129 | 'display' => 0, |
||
| 130 | 'timemodified' => time(), |
||
| 131 | 'users' => [], |
||
| 132 | 'files' => [], |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return null; |
||
| 138 | } |
||
| 194 |
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.