| Total Complexity | 93 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Course often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Course, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Course |
||
| 18 | { |
||
| 19 | public array $resources; |
||
| 20 | public string $code; |
||
| 21 | public string $path; |
||
| 22 | public ?string $destination_path = null; |
||
| 23 | public ?string $destination_db = null; |
||
| 24 | public string $encoding; |
||
| 25 | public string $type; |
||
| 26 | public string $backup_path = ''; |
||
| 27 | |||
| 28 | /** @var array<string,mixed> Legacy-friendly metadata bag (alias of $meta) */ |
||
| 29 | public array $info; |
||
| 30 | |||
| 31 | /** @var array<string,mixed> Canonical metadata bag */ |
||
| 32 | public array $meta; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new Course-object. |
||
| 36 | */ |
||
| 37 | public function __construct() |
||
| 38 | { |
||
| 39 | $this->resources = []; |
||
| 40 | $this->code = ''; |
||
| 41 | $this->path = ''; |
||
| 42 | $this->backup_path = ''; |
||
| 43 | $this->encoding = api_get_system_encoding(); |
||
| 44 | $this->type = ''; |
||
| 45 | |||
| 46 | // Keep $info and $meta in sync (alias) |
||
| 47 | $this->info = []; |
||
| 48 | $this->meta =& $this->info; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Check if a resource links to the given resource. |
||
| 53 | * |
||
| 54 | * @param mixed $resource_to_check |
||
| 55 | */ |
||
| 56 | public function is_linked_resource(&$resource_to_check): bool |
||
| 57 | { |
||
| 58 | foreach ($this->resources as $type => $resources) { |
||
| 59 | if (\is_array($resources)) { |
||
| 60 | foreach ($resources as $resource) { |
||
| 61 | Resource::setClassType($resource); |
||
| 62 | if ($resource->links_to($resource_to_check)) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | if (RESOURCE_LEARNPATH === $type && 'CourseCopyLearnpath' === $resource::class) { |
||
| 66 | if ($resource->has_item($resource_to_check)) { |
||
| 67 | return true; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Add a resource from a given type to this course. |
||
| 79 | */ |
||
| 80 | public function add_resource(&$resource): void |
||
| 81 | { |
||
| 82 | $this->resources[$resource->get_type()][$resource->get_id()] = $resource; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Does this course have resources? |
||
| 87 | * |
||
| 88 | * @param int|null $type If provided, only check that type. |
||
| 89 | */ |
||
| 90 | public function has_resources($type = null): bool |
||
| 91 | { |
||
| 92 | if (null !== $type) { |
||
| 93 | return isset($this->resources[$type]) |
||
| 94 | && \is_array($this->resources[$type]) |
||
| 95 | && \count($this->resources[$type]) > 0; |
||
| 96 | } |
||
| 97 | |||
| 98 | return \count($this->resources) > 0; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function show(): void |
||
| 103 | // no-op |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns sample text based on the imported course content. |
||
| 108 | * This is used for language/encoding detection when metadata is missing. |
||
| 109 | */ |
||
| 110 | public function get_sample_text(): string |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Converts to the system encoding all the language-sensitive fields in the imported course. |
||
| 216 | */ |
||
| 217 | public function to_system_encoding(): void |
||
| 218 | { |
||
| 219 | foreach ($this->resources as $type => &$resources) { |
||
| 220 | if (\count($resources) <= 0) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | foreach ($resources as &$resource) { |
||
| 225 | switch ($type) { |
||
| 226 | case RESOURCE_ANNOUNCEMENT: |
||
| 227 | case RESOURCE_EVENT: |
||
| 228 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 229 | $resource->content = api_to_system_encoding($resource->content, $this->encoding); |
||
| 230 | break; |
||
| 231 | |||
| 232 | case RESOURCE_DOCUMENT: |
||
| 233 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 234 | $resource->comment = api_to_system_encoding($resource->comment, $this->encoding); |
||
| 235 | break; |
||
| 236 | |||
| 237 | case RESOURCE_FORUM: |
||
| 238 | case RESOURCE_QUIZ: |
||
| 239 | case RESOURCE_FORUMCATEGORY: |
||
| 240 | if (isset($resource->title)) { |
||
| 241 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 242 | } |
||
| 243 | if (isset($resource->description)) { |
||
| 244 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 245 | } |
||
| 246 | if (isset($resource->obj)) { |
||
| 247 | foreach (['cat_title', 'cat_comment', 'title', 'description'] as $f) { |
||
| 248 | if (isset($resource->obj->{$f}) && \is_string($resource->obj->{$f})) { |
||
| 249 | $resource->obj->{$f} = api_to_system_encoding($resource->obj->{$f}, $this->encoding); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | break; |
||
| 254 | |||
| 255 | case RESOURCE_LINK: |
||
| 256 | case RESOURCE_LINKCATEGORY: |
||
| 257 | case RESOURCE_TEST_CATEGORY: |
||
| 258 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 259 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 260 | break; |
||
| 261 | |||
| 262 | case RESOURCE_FORUMPOST: |
||
| 263 | if (isset($resource->title)) { |
||
| 264 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 265 | } |
||
| 266 | if (isset($resource->text)) { |
||
| 267 | $resource->text = api_to_system_encoding($resource->text, $this->encoding); |
||
| 268 | } |
||
| 269 | if (isset($resource->poster_name)) { |
||
| 270 | $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding); |
||
| 271 | } |
||
| 272 | break; |
||
| 273 | |||
| 274 | case RESOURCE_FORUMTOPIC: |
||
| 275 | if (isset($resource->title)) { |
||
| 276 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 277 | } |
||
| 278 | if (isset($resource->topic_poster_name)) { |
||
| 279 | $resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding); |
||
| 280 | } |
||
| 281 | if (isset($resource->title_qualify)) { |
||
| 282 | $resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding); |
||
| 283 | } |
||
| 284 | break; |
||
| 285 | |||
| 286 | case RESOURCE_GLOSSARY: |
||
| 287 | $resource->name = api_to_system_encoding($resource->name, $this->encoding); |
||
| 288 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 289 | break; |
||
| 290 | |||
| 291 | case RESOURCE_LEARNPATH: |
||
| 292 | $resource->name = api_to_system_encoding($resource->name, $this->encoding); |
||
| 293 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 294 | $resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding); |
||
| 295 | $resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding); |
||
| 296 | break; |
||
| 297 | |||
| 298 | case RESOURCE_QUIZQUESTION: |
||
| 299 | $resource->question = api_to_system_encoding($resource->question, $this->encoding); |
||
| 300 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 301 | if (\is_array($resource->answers) && \count($resource->answers) > 0) { |
||
| 302 | foreach ($resource->answers as &$answer) { |
||
| 303 | $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding); |
||
| 304 | $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | break; |
||
| 308 | |||
| 309 | case RESOURCE_SCORM: |
||
| 310 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 311 | break; |
||
| 312 | |||
| 313 | case RESOURCE_SURVEY: |
||
| 314 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 315 | $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding); |
||
| 316 | $resource->author = api_to_system_encoding($resource->author, $this->encoding); |
||
| 317 | $resource->intro = api_to_system_encoding($resource->intro, $this->encoding); |
||
| 318 | $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding); |
||
| 319 | break; |
||
| 320 | |||
| 321 | case RESOURCE_SURVEYQUESTION: |
||
| 322 | $resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding); |
||
| 323 | $resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding); |
||
| 324 | break; |
||
| 325 | |||
| 326 | case RESOURCE_TOOL_INTRO: |
||
| 327 | $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding); |
||
| 328 | break; |
||
| 329 | |||
| 330 | case RESOURCE_WIKI: |
||
| 331 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 332 | $resource->content = api_to_system_encoding($resource->content, $this->encoding); |
||
| 333 | $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding); |
||
| 334 | break; |
||
| 335 | |||
| 336 | case RESOURCE_WORK: |
||
| 337 | $resource->url = api_to_system_encoding($resource->url, $this->encoding); |
||
| 338 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 339 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 340 | break; |
||
| 341 | |||
| 342 | default: |
||
| 343 | break; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | $this->encoding = api_get_system_encoding(); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Serialize the course with the best serializer available (optionally compressed). |
||
| 353 | */ |
||
| 354 | public static function serialize($course): string |
||
| 355 | { |
||
| 356 | $serialized = \extension_loaded('igbinary') |
||
| 357 | ? igbinary_serialize($course) |
||
| 358 | : serialize($course); |
||
| 359 | |||
| 360 | // Compress if possible |
||
| 361 | if (\function_exists('gzdeflate')) { |
||
| 362 | $deflated = gzdeflate($serialized, 9); |
||
| 363 | if ($deflated !== false) { |
||
| 364 | $serialized = $deflated; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | return $serialized; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Unserialize the course with the best serializer available. |
||
| 373 | * |
||
| 374 | * @return Course |
||
| 375 | */ |
||
| 376 | public static function unserialize($course): Course |
||
| 392 | } |
||
| 393 | } |
||
| 394 |