| Conditions | 28 |
| Paths | 1926 |
| Total Lines | 188 |
| Code Lines | 120 |
| 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 |
||
| 98 | public function storeDocuments(array $documents, string $packageRoot): bool |
||
| 99 | { |
||
| 100 | $packageRoot = rtrim($packageRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
||
| 101 | |||
| 102 | $courseInfo = api_get_course_info(); |
||
| 103 | $courseEntity = api_get_course_entity($courseInfo['real_id']); |
||
| 104 | $sessionEnt = api_get_session_entity((int) api_get_session_id()); |
||
| 105 | $sessionId = (int) ($sessionEnt ? $sessionEnt->getId() : 0); |
||
| 106 | $group = api_get_group_entity(0); |
||
| 107 | |||
| 108 | $docRepo = Container::getDocumentRepository(); |
||
| 109 | |||
| 110 | // Ensure nested folder chain under Documents and return parent iid. |
||
| 111 | $ensureFolder = function (string $relPath) use ($docRepo, $courseEntity, $courseInfo, $sessionEnt, $group, $sessionId): int { |
||
| 112 | $rel = '/'.ltrim($relPath, '/'); |
||
| 113 | if ('/' === $rel || '' === $rel) { |
||
| 114 | return 0; |
||
| 115 | } |
||
| 116 | |||
| 117 | $parts = array_values(array_filter(explode('/', trim($rel, '/')))); |
||
| 118 | $accum = ''; |
||
| 119 | $parentId = 0; |
||
| 120 | |||
| 121 | foreach ($parts as $seg) { |
||
| 122 | $accum .= '/'.$seg; |
||
| 123 | |||
| 124 | $parentRes = $parentId ? $docRepo->find($parentId) : $courseEntity; |
||
| 125 | |||
| 126 | $existing = $docRepo->findCourseResourceByTitle( |
||
| 127 | $seg, |
||
| 128 | $parentRes->getResourceNode(), |
||
|
|
|||
| 129 | $courseEntity, |
||
| 130 | $sessionEnt, |
||
| 131 | $group |
||
| 132 | ); |
||
| 133 | |||
| 134 | if ($existing && method_exists($existing, 'getIid')) { |
||
| 135 | $parentId = (int) $existing->getIid(); |
||
| 136 | |||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | $entity = DocumentManager::addDocument( |
||
| 141 | ['real_id' => $courseInfo['real_id'], 'code' => $courseInfo['code']], |
||
| 142 | $accum, |
||
| 143 | 'folder', |
||
| 144 | 0, |
||
| 145 | $seg, |
||
| 146 | null, |
||
| 147 | 0, |
||
| 148 | null, |
||
| 149 | 0, |
||
| 150 | $sessionId, |
||
| 151 | 0, |
||
| 152 | false, |
||
| 153 | '', |
||
| 154 | $parentId, |
||
| 155 | '' |
||
| 156 | ); |
||
| 157 | |||
| 158 | $parentId = method_exists($entity, 'getIid') ? (int) $entity->getIid() : 0; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $parentId; |
||
| 162 | }; |
||
| 163 | |||
| 164 | // Base destination root for the package contents. |
||
| 165 | $baseRel = '/commoncartridge'; |
||
| 166 | $ensureFolder($baseRel); |
||
| 167 | |||
| 168 | foreach ($documents as $doc) { |
||
| 169 | $type = (string) ($doc[2] ?? ''); |
||
| 170 | |||
| 171 | // Compute destination subfolder: |
||
| 172 | $subdir = ''; |
||
| 173 | if ('file' === $type) { |
||
| 174 | $ref = trim((string) ($doc[4] ?? '')); |
||
| 175 | if ('' === $ref) { |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | $subdir = trim(\dirname($ref), '.\/'); |
||
| 179 | } elseif ('html' === $type) { |
||
| 180 | $subdir = trim((string) ($doc[6] ?? ''), '/'); |
||
| 181 | } else { |
||
| 182 | // Unknown type; skip gracefully. |
||
| 183 | Cc1p3Convert::logAction('storeDocuments: unknown type skipped', ['type' => $type]); |
||
| 184 | |||
| 185 | continue; |
||
| 186 | } |
||
| 187 | |||
| 188 | $destPath = $baseRel.($subdir ? '/'.$subdir : ''); |
||
| 189 | $parentId = $ensureFolder($destPath); |
||
| 190 | |||
| 191 | // Collision-safe final title. |
||
| 192 | $title = (string) ($doc[1] ?? ''); |
||
| 193 | $guessName = 'file' === $type ? basename((string) ($doc[4] ?? 'file.bin')) : 'page.html'; |
||
| 194 | $finalTitle = '' !== $title ? $title : $guessName; |
||
| 195 | |||
| 196 | $parentRes = $parentId ? $docRepo->find($parentId) : $courseEntity; |
||
| 197 | $nameExists = function (string $t) use ($docRepo, $parentRes, $courseEntity, $sessionEnt, $group): bool { |
||
| 198 | $e = $docRepo->findCourseResourceByTitle( |
||
| 199 | $t, |
||
| 200 | $parentRes->getResourceNode(), |
||
| 201 | $courseEntity, |
||
| 202 | $sessionEnt, |
||
| 203 | $group |
||
| 204 | ); |
||
| 205 | |||
| 206 | return (bool) ($e && method_exists($e, 'getIid')); |
||
| 207 | }; |
||
| 208 | |||
| 209 | if ($nameExists($finalTitle)) { |
||
| 210 | $pi = pathinfo($finalTitle); |
||
| 211 | $base = $pi['filename'] ?? $finalTitle; |
||
| 212 | $ext = isset($pi['extension']) && '' !== $pi['extension'] ? '.'.$pi['extension'] : ''; |
||
| 213 | $i = 1; |
||
| 214 | while ($nameExists($base.'_'.$i.$ext)) { |
||
| 215 | $i++; |
||
| 216 | } |
||
| 217 | $finalTitle = $base.'_'.$i.$ext; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Persist |
||
| 221 | if ('file' === $type) { |
||
| 222 | $ref = trim((string) ($doc[4] ?? '')); |
||
| 223 | $src = $packageRoot.str_replace('\\', '/', $ref); |
||
| 224 | if (!is_file($src) || !is_readable($src)) { |
||
| 225 | Cc1p3Convert::logAction('CC import: missing/unreadable file', ['src' => $src]); |
||
| 226 | |||
| 227 | continue; |
||
| 228 | } |
||
| 229 | |||
| 230 | try { |
||
| 231 | DocumentManager::addDocument( |
||
| 232 | ['real_id' => $courseInfo['real_id'], 'code' => $courseInfo['code']], |
||
| 233 | $destPath.'/'.$finalTitle, |
||
| 234 | 'file', |
||
| 235 | (int) @filesize($src), |
||
| 236 | $finalTitle, |
||
| 237 | '', |
||
| 238 | 0, |
||
| 239 | null, |
||
| 240 | 0, |
||
| 241 | $sessionId, |
||
| 242 | 0, |
||
| 243 | false, |
||
| 244 | '', // no inline content for binaries |
||
| 245 | $parentId, |
||
| 246 | $src // realPath → copied into ResourceFile storage |
||
| 247 | ); |
||
| 248 | } catch (Throwable $e) { |
||
| 249 | Cc1p3Convert::logAction('CC import: addDocument(file) failed', ['src' => $src, 'error' => $e->getMessage()]); |
||
| 250 | } |
||
| 251 | |||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ('html' === $type) { |
||
| 256 | // Inline HTML content, keep relative links intact (folder layout is mirrored). |
||
| 257 | $content = (string) ($doc[3] ?? ''); |
||
| 258 | |||
| 259 | try { |
||
| 260 | DocumentManager::addDocument( |
||
| 261 | ['real_id' => $courseInfo['real_id'], 'code' => $courseInfo['code']], |
||
| 262 | $destPath.'/'.$finalTitle, |
||
| 263 | 'file', |
||
| 264 | (int) \strlen($content), |
||
| 265 | $finalTitle, |
||
| 266 | '', |
||
| 267 | 0, |
||
| 268 | null, |
||
| 269 | 0, |
||
| 270 | $sessionId, |
||
| 271 | 0, |
||
| 272 | false, |
||
| 273 | $content, // inline HTML |
||
| 274 | $parentId, |
||
| 275 | '' // no realPath when content provided |
||
| 276 | ); |
||
| 277 | } catch (Throwable $e) { |
||
| 278 | Cc1p3Convert::logAction('CC import: addDocument(html) failed', ['dest' => $destPath.'/'.$finalTitle, 'error' => $e->getMessage()]); |
||
| 279 | } |
||
| 280 | |||
| 281 | continue; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return true; |
||
| 286 | } |
||
| 434 |
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.