| Total Complexity | 111 |
| Total Lines | 577 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 113 | class Container |
||
| 114 | { |
||
| 115 | public static ?ContainerInterface $container = null; |
||
| 116 | public static ?Request $request = null; |
||
| 117 | // For legacy, to get the translator service is necessary get it by Container::$container->get('translator') |
||
| 118 | public static ?Translator $translator = null; |
||
| 119 | public static Environment $twig; |
||
| 120 | public static ?Session $session = null; |
||
| 121 | public static string $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig'; |
||
| 122 | |||
| 123 | public static function setContainer(ContainerInterface $container): void |
||
| 124 | { |
||
| 125 | self::$container = $container; |
||
| 126 | } |
||
| 127 | |||
| 128 | public static function getParameter(string $parameter): array|bool|float|int|string|UnitEnum|null |
||
| 129 | { |
||
| 130 | if (self::$container->hasParameter($parameter)) { |
||
|
|
|||
| 131 | return self::$container->getParameter($parameter); |
||
| 132 | } |
||
| 133 | |||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | public static function getLegacyHelper(): ContainerHelper |
||
| 138 | { |
||
| 139 | return self::$container->get(ContainerHelper::class); |
||
| 140 | } |
||
| 141 | |||
| 142 | public static function getEnvironment(): string |
||
| 143 | { |
||
| 144 | return self::getLegacyHelper()->getKernel()->getEnvironment(); |
||
| 145 | } |
||
| 146 | |||
| 147 | public static function getLogDir(): string |
||
| 148 | { |
||
| 149 | return self::getLegacyHelper()->getKernel()->getLogDir(); |
||
| 150 | } |
||
| 151 | |||
| 152 | public static function getCacheDir(): string |
||
| 153 | { |
||
| 154 | return self::getLegacyHelper()->getKernel()->getCacheDir().'/'; |
||
| 155 | } |
||
| 156 | |||
| 157 | public static function getProjectDir(): string |
||
| 158 | { |
||
| 159 | if (null !== self::$container) { |
||
| 160 | return self::getLegacyHelper()->getKernel()->getProjectDir().'/'; |
||
| 161 | } |
||
| 162 | |||
| 163 | return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/'; |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function isInstalled(): bool |
||
| 167 | { |
||
| 168 | return self::getLegacyHelper()->getKernel()->isInstalled(); |
||
| 169 | } |
||
| 170 | |||
| 171 | public static function getMessengerBus(): MessageBusInterface |
||
| 172 | { |
||
| 173 | return self::getLegacyHelper()->getMessengerBus(); |
||
| 174 | } |
||
| 175 | |||
| 176 | public static function getTwig(): Environment |
||
| 177 | { |
||
| 178 | return self::$twig; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return Editor |
||
| 183 | */ |
||
| 184 | public static function getHtmlEditor() |
||
| 185 | { |
||
| 186 | return self::$container->get(CkEditor::class); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return null|Request |
||
| 191 | */ |
||
| 192 | public static function getRequest() |
||
| 193 | { |
||
| 194 | if (null === self::$container) { |
||
| 195 | return null; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!empty(self::$request)) { |
||
| 199 | return self::$request; |
||
| 200 | } |
||
| 201 | |||
| 202 | return self::$container->get('request_stack')->getCurrentRequest(); |
||
| 203 | } |
||
| 204 | |||
| 205 | public static function setRequest(Request $request): void |
||
| 208 | } |
||
| 209 | |||
| 210 | public static function getSession(): bool|HttpSessionInterface|Session|null |
||
| 211 | { |
||
| 212 | if (null !== self::$session) { |
||
| 213 | return self::$session; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (null !== self::$container) { |
||
| 217 | return self::$container->get('request_stack')->getSession(); |
||
| 218 | } |
||
| 219 | |||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | public static function setSession(Session $session): void |
||
| 224 | { |
||
| 225 | self::$session = $session; |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function getAuthorizationChecker(): AuthorizationCheckerInterface |
||
| 229 | { |
||
| 230 | return self::getLegacyHelper()->getAuthorizationChecker(); |
||
| 231 | } |
||
| 232 | |||
| 233 | public static function getTokenStorage(): TokenStorageInterface|TokenStorage |
||
| 236 | } |
||
| 237 | |||
| 238 | public static function getMailer(): Mailer |
||
| 239 | { |
||
| 240 | return self::$container->get(Mailer::class); |
||
| 241 | } |
||
| 242 | |||
| 243 | public static function getSettingsManager(): SettingsManager |
||
| 244 | { |
||
| 245 | return self::$container->get(SettingsManager::class); |
||
| 246 | } |
||
| 247 | |||
| 248 | public static function getCourseSettingsManager(): SettingsCourseManager |
||
| 249 | { |
||
| 250 | return self::$container->get(SettingsCourseManager::class); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return EntityManager |
||
| 255 | */ |
||
| 256 | public static function getEntityManager() |
||
| 257 | { |
||
| 258 | return Database::getManager(); |
||
| 259 | } |
||
| 260 | |||
| 261 | public static function getAssetRepository(): AssetRepository |
||
| 262 | { |
||
| 263 | return self::$container->get(AssetRepository::class); |
||
| 264 | } |
||
| 265 | |||
| 266 | public static function getResourceNodeRepository(): ResourceNodeRepository |
||
| 267 | { |
||
| 268 | return self::$container->get(ResourceNodeRepository::class); |
||
| 269 | } |
||
| 270 | |||
| 271 | public static function getAttendanceRepository(): CAttendanceRepository |
||
| 272 | { |
||
| 273 | return self::$container->get(CAttendanceRepository::class); |
||
| 274 | } |
||
| 275 | |||
| 276 | public static function getAnnouncementRepository(): CAnnouncementRepository |
||
| 277 | { |
||
| 278 | return self::$container->get(CAnnouncementRepository::class); |
||
| 279 | } |
||
| 280 | |||
| 281 | public static function getAccessUrlRepository(): AccessUrlRepository |
||
| 282 | { |
||
| 283 | return self::$container->get(AccessUrlRepository::class); |
||
| 284 | } |
||
| 285 | |||
| 286 | public static function getAccessUrlRelPluginRepository(): AccessUrlRelPluginRepository |
||
| 287 | { |
||
| 288 | return self::$container->get(AccessUrlRelPluginRepository::class); |
||
| 289 | } |
||
| 290 | |||
| 291 | public static function getAnnouncementAttachmentRepository(): CAnnouncementAttachmentRepository |
||
| 292 | { |
||
| 293 | return self::$container->get(CAnnouncementAttachmentRepository::class); |
||
| 294 | } |
||
| 295 | |||
| 296 | public static function getTicketMessageAttachmentRepository(): TicketMessageAttachmentRepository |
||
| 297 | { |
||
| 298 | return self::$container->get(TicketMessageAttachmentRepository::class); |
||
| 299 | } |
||
| 300 | |||
| 301 | public static function getSocialPostAttachmentRepository(): SocialPostAttachmentRepository |
||
| 302 | { |
||
| 303 | return self::$container->get(SocialPostAttachmentRepository::class); |
||
| 304 | } |
||
| 305 | |||
| 306 | public static function getCourseRepository(): CourseRepository |
||
| 307 | { |
||
| 308 | return self::$container->get(CourseRepository::class); |
||
| 309 | } |
||
| 310 | |||
| 311 | public static function getCareerRepository(): CareerRepository |
||
| 312 | { |
||
| 313 | return self::$container->get(CareerRepository::class); |
||
| 314 | } |
||
| 315 | |||
| 316 | public static function getCourseCategoryRepository(): CourseCategoryRepository |
||
| 317 | { |
||
| 318 | return self::$container->get(CourseCategoryRepository::class); |
||
| 319 | } |
||
| 320 | |||
| 321 | public static function getCourseDescriptionRepository(): CCourseDescriptionRepository |
||
| 322 | { |
||
| 323 | return self::$container->get(CCourseDescriptionRepository::class); |
||
| 324 | } |
||
| 325 | |||
| 326 | public static function getCalendarEventRepository(): CCalendarEventRepository |
||
| 327 | { |
||
| 328 | return self::$container->get(CCalendarEventRepository::class); |
||
| 329 | } |
||
| 330 | |||
| 331 | public static function getCalendarEventAttachmentRepository(): CCalendarEventAttachmentRepository |
||
| 332 | { |
||
| 333 | return self::$container->get(CCalendarEventAttachmentRepository::class); |
||
| 334 | } |
||
| 335 | |||
| 336 | public static function getDocumentRepository(): CDocumentRepository |
||
| 337 | { |
||
| 338 | return self::$container->get(CDocumentRepository::class); |
||
| 339 | } |
||
| 340 | |||
| 341 | public static function getQuizCategoryRepository(): CQuizCategoryRepository |
||
| 342 | { |
||
| 343 | return self::$container->get(CQuizCategoryRepository::class); |
||
| 344 | } |
||
| 345 | |||
| 346 | public static function getExternalToolRepository(): ExternalToolRepository |
||
| 347 | { |
||
| 348 | return self::$container->get(ExternalToolRepository::class); |
||
| 349 | } |
||
| 350 | |||
| 351 | public static function getExtraFieldRepository(): ExtraFieldRepository |
||
| 352 | { |
||
| 353 | return self::$container->get(ExtraFieldRepository::class); |
||
| 354 | } |
||
| 355 | |||
| 356 | public static function getExtraFieldOptionsRepository(): ExtraFieldOptionsRepository |
||
| 357 | { |
||
| 358 | return self::$container->get(ExtraFieldOptionsRepository::class); |
||
| 359 | } |
||
| 360 | |||
| 361 | public static function getGlossaryRepository(): CGlossaryRepository |
||
| 362 | { |
||
| 363 | return self::$container->get(CGlossaryRepository::class); |
||
| 364 | } |
||
| 365 | |||
| 366 | public static function getGradeBookCategoryRepository(): GradeBookCategoryRepository |
||
| 367 | { |
||
| 368 | return self::$container->get(GradeBookCategoryRepository::class); |
||
| 369 | } |
||
| 370 | |||
| 371 | public static function getGradeBookCertificateRepository(): GradebookCertificateRepository |
||
| 372 | { |
||
| 373 | return self::$container->get(GradebookCertificateRepository::class); |
||
| 374 | } |
||
| 375 | |||
| 376 | public static function getGradebookResultRepository(): GradebookResultRepository |
||
| 377 | { |
||
| 378 | return self::$container->get(GradebookResultRepository::class); |
||
| 379 | } |
||
| 380 | |||
| 381 | public static function getGroupRepository(): CGroupRepository |
||
| 382 | { |
||
| 383 | return self::$container->get(CGroupRepository::class); |
||
| 384 | } |
||
| 385 | |||
| 386 | public static function getGroupCategoryRepository(): CGroupCategoryRepository |
||
| 387 | { |
||
| 388 | return self::$container->get(CGroupCategoryRepository::class); |
||
| 389 | } |
||
| 390 | |||
| 391 | public static function getForumRepository(): CForumRepository |
||
| 392 | { |
||
| 393 | return self::$container->get(CForumRepository::class); |
||
| 394 | } |
||
| 395 | |||
| 396 | public static function getForumCategoryRepository(): CForumCategoryRepository |
||
| 397 | { |
||
| 398 | return self::$container->get(CForumCategoryRepository::class); |
||
| 399 | } |
||
| 400 | |||
| 401 | public static function getForumPostRepository(): CForumPostRepository |
||
| 402 | { |
||
| 403 | return self::$container->get(CForumPostRepository::class); |
||
| 404 | } |
||
| 405 | |||
| 406 | public static function getForumAttachmentRepository(): CForumAttachmentRepository |
||
| 407 | { |
||
| 408 | return self::$container->get(CForumAttachmentRepository::class); |
||
| 409 | } |
||
| 410 | |||
| 411 | public static function getForumThreadRepository(): CForumThreadRepository |
||
| 412 | { |
||
| 413 | return self::$container->get(CForumThreadRepository::class); |
||
| 414 | } |
||
| 415 | |||
| 416 | public static function getIllustrationRepository(): IllustrationRepository |
||
| 417 | { |
||
| 418 | return self::$container->get(IllustrationRepository::class); |
||
| 419 | } |
||
| 420 | |||
| 421 | public static function getQuizRepository(): CQuizRepository |
||
| 422 | { |
||
| 423 | return self::$container->get(CQuizRepository::class); |
||
| 424 | } |
||
| 425 | |||
| 426 | public static function getQuestionRepository(): CQuizQuestionRepository |
||
| 429 | } |
||
| 430 | |||
| 431 | public static function getQuestionCategoryRepository(): CQuizQuestionCategoryRepository |
||
| 432 | { |
||
| 433 | return self::$container->get(CQuizQuestionCategoryRepository::class); |
||
| 434 | } |
||
| 435 | |||
| 436 | public static function getLanguageRepository(): LanguageRepository |
||
| 437 | { |
||
| 438 | return self::$container->get(LanguageRepository::class); |
||
| 439 | } |
||
| 440 | |||
| 441 | public static function getLinkRepository(): CLinkRepository |
||
| 444 | } |
||
| 445 | |||
| 446 | public static function getLinkCategoryRepository(): CLinkCategoryRepository |
||
| 447 | { |
||
| 448 | return self::$container->get(CLinkCategoryRepository::class); |
||
| 449 | } |
||
| 450 | |||
| 451 | public static function getLpRepository(): CLpRepository |
||
| 452 | { |
||
| 453 | return self::$container->get(CLpRepository::class); |
||
| 454 | } |
||
| 455 | |||
| 456 | public static function getLpItemRepository(): CLpItemRepository |
||
| 457 | { |
||
| 458 | return self::$container->get(CLpItemRepository::class); |
||
| 459 | } |
||
| 460 | |||
| 461 | public static function getLpCategoryRepository(): CLpCategoryRepository |
||
| 462 | { |
||
| 463 | return self::$container->get(CLpCategoryRepository::class); |
||
| 464 | } |
||
| 465 | |||
| 466 | public static function getMessageRepository(): MessageRepository |
||
| 467 | { |
||
| 468 | return self::$container->get(MessageRepository::class); |
||
| 469 | } |
||
| 470 | |||
| 471 | public static function getMessageAttachmentRepository(): MessageAttachmentRepository |
||
| 472 | { |
||
| 473 | return self::$container->get(MessageAttachmentRepository::class); |
||
| 474 | } |
||
| 475 | |||
| 476 | public static function getNotebookRepository(): CNotebookRepository |
||
| 477 | { |
||
| 478 | return self::$container->get(CNotebookRepository::class); |
||
| 479 | } |
||
| 480 | |||
| 481 | public static function getPersonalFileRepository(): PersonalFileRepository |
||
| 482 | { |
||
| 483 | return self::$container->get(PersonalFileRepository::class); |
||
| 484 | } |
||
| 485 | |||
| 486 | public static function getPromotionRepository(): PromotionRepository |
||
| 487 | { |
||
| 488 | return self::$container->get(PromotionRepository::class); |
||
| 489 | } |
||
| 490 | |||
| 491 | public static function getUserRepository(): UserRepository |
||
| 492 | { |
||
| 493 | return self::$container->get(UserRepository::class); |
||
| 494 | } |
||
| 495 | |||
| 496 | public static function getUsergroupRepository(): UsergroupRepository |
||
| 497 | { |
||
| 498 | return self::$container->get(UsergroupRepository::class); |
||
| 499 | } |
||
| 500 | |||
| 501 | public static function getPluginRepository(): PluginRepository |
||
| 502 | { |
||
| 503 | /** @var PluginRepository $pluginRepo */ |
||
| 504 | return self::$container->get(PluginRepository::class); |
||
| 505 | } |
||
| 506 | |||
| 507 | public static function getUserToJsonNormalizer(): UserToJsonNormalizer |
||
| 508 | { |
||
| 509 | return self::$container->get(UserToJsonNormalizer::class); |
||
| 510 | } |
||
| 511 | |||
| 512 | public static function getShortcutRepository(): CShortcutRepository |
||
| 513 | { |
||
| 514 | return self::$container->get(CShortcutRepository::class); |
||
| 515 | } |
||
| 516 | |||
| 517 | public static function getStudentPublicationRepository(): CStudentPublicationRepository |
||
| 518 | { |
||
| 519 | return self::$container->get(CStudentPublicationRepository::class); |
||
| 520 | } |
||
| 521 | |||
| 522 | public static function getStudentPublicationAssignmentRepository(): CStudentPublicationAssignmentRepository |
||
| 523 | { |
||
| 524 | return self::$container->get(CStudentPublicationAssignmentRepository::class); |
||
| 525 | } |
||
| 526 | |||
| 527 | public static function getStudentPublicationCommentRepository(): CStudentPublicationCommentRepository |
||
| 528 | { |
||
| 529 | return self::$container->get(CStudentPublicationCommentRepository::class); |
||
| 530 | } |
||
| 531 | |||
| 532 | public static function getStudentPublicationCorrectionRepository(): CStudentPublicationCorrectionRepository |
||
| 533 | { |
||
| 534 | return self::$container->get(CStudentPublicationCorrectionRepository::class); |
||
| 535 | } |
||
| 536 | |||
| 537 | public static function getSequenceResourceRepository(): SequenceResourceRepository |
||
| 538 | { |
||
| 539 | return self::$container->get(SequenceResourceRepository::class); |
||
| 540 | } |
||
| 541 | |||
| 542 | public static function getSequenceRepository(): SequenceRepository |
||
| 543 | { |
||
| 544 | return self::$container->get(SequenceRepository::class); |
||
| 545 | } |
||
| 546 | |||
| 547 | public static function getSessionRepository(): SessionRepository |
||
| 548 | { |
||
| 549 | return self::$container->get(SessionRepository::class); |
||
| 550 | } |
||
| 551 | |||
| 552 | public static function getSkillRepository(): SkillRepository |
||
| 553 | { |
||
| 554 | return self::$container->get(SkillRepository::class); |
||
| 555 | } |
||
| 556 | |||
| 557 | public static function getSurveyRepository(): CSurveyRepository |
||
| 558 | { |
||
| 559 | return self::$container->get(CSurveyRepository::class); |
||
| 560 | } |
||
| 561 | |||
| 562 | public static function getSurveyInvitationRepository(): CSurveyInvitationRepository |
||
| 563 | { |
||
| 564 | return self::$container->get(CSurveyInvitationRepository::class); |
||
| 565 | } |
||
| 566 | |||
| 567 | public static function getSurveyQuestionRepository(): CSurveyQuestionRepository |
||
| 568 | { |
||
| 569 | return self::$container->get(CSurveyQuestionRepository::class); |
||
| 570 | } |
||
| 571 | |||
| 572 | public static function getSysAnnouncementRepository(): SysAnnouncementRepository |
||
| 573 | { |
||
| 574 | return self::$container->get(SysAnnouncementRepository::class); |
||
| 575 | } |
||
| 576 | |||
| 577 | public static function getTagRepository(): TagRepository |
||
| 578 | { |
||
| 579 | return self::$container->get(TagRepository::class); |
||
| 580 | } |
||
| 581 | |||
| 582 | public static function getThematicRepository(): CThematicRepository |
||
| 583 | { |
||
| 584 | return self::$container->get(CThematicRepository::class); |
||
| 585 | } |
||
| 586 | |||
| 587 | public static function getThematicPlanRepository(): CThematicPlanRepository |
||
| 588 | { |
||
| 589 | return self::$container->get(CThematicPlanRepository::class); |
||
| 590 | } |
||
| 591 | |||
| 592 | public static function getThematicAdvanceRepository(): CThematicAdvanceRepository |
||
| 593 | { |
||
| 594 | return self::$container->get(CThematicAdvanceRepository::class); |
||
| 595 | } |
||
| 596 | |||
| 597 | public static function getTrackEExerciseRepository(): TrackEExerciseRepository |
||
| 598 | { |
||
| 599 | return self::$container->get(TrackEExerciseRepository::class); |
||
| 600 | } |
||
| 601 | |||
| 602 | public static function getTrackEDownloadsRepository(): TrackEDownloadsRepository |
||
| 603 | { |
||
| 604 | return self::$container->get(TrackEDownloadsRepository::class); |
||
| 605 | } |
||
| 606 | |||
| 607 | public static function getWikiRepository(): CWikiRepository |
||
| 608 | { |
||
| 609 | return self::$container->get(CWikiRepository::class); |
||
| 610 | } |
||
| 611 | |||
| 612 | public static function getToolIntroRepository(): CToolIntroRepository |
||
| 613 | { |
||
| 614 | return self::$container->get(CToolIntroRepository::class); |
||
| 615 | } |
||
| 616 | |||
| 617 | public static function getLegalRepository(): LegalRepository |
||
| 618 | { |
||
| 619 | return self::$container->get(LegalRepository::class); |
||
| 620 | } |
||
| 621 | |||
| 622 | public static function getFormFactory(): FormFactory |
||
| 623 | { |
||
| 624 | return self::$container->get('form.factory'); |
||
| 625 | } |
||
| 626 | |||
| 627 | public static function addFlash(string $message, string $type = 'success'): void |
||
| 628 | { |
||
| 629 | $type = match ($type) { |
||
| 630 | 'confirmation', 'confirm' => 'success', |
||
| 631 | default => 'info', |
||
| 632 | }; |
||
| 633 | |||
| 634 | $session = self::getSession(); |
||
| 635 | |||
| 636 | if ($session instanceof Session) { |
||
| 637 | $session->getFlashBag()->add($type, $message); |
||
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | public static function getRouter(): Router |
||
| 642 | { |
||
| 643 | return self::$container->get('router'); |
||
| 644 | } |
||
| 645 | |||
| 646 | public static function getToolChain(): ToolChain |
||
| 647 | { |
||
| 648 | return self::$container->get(ToolChain::class); |
||
| 649 | } |
||
| 650 | |||
| 651 | public static function setLegacyServices(ContainerInterface $container): void |
||
| 659 | } |
||
| 660 | |||
| 661 | public static function getSocialPostRepository(): SocialPostRepository |
||
| 662 | { |
||
| 663 | return self::$container->get(SocialPostRepository::class); |
||
| 664 | } |
||
| 665 | |||
| 666 | public static function getTrackELoginRepository(): TrackELoginRepository |
||
| 667 | { |
||
| 668 | return self::$container->get(TrackELoginRepository::class); |
||
| 669 | } |
||
| 670 | |||
| 671 | public static function getTrackELoginRecordRepository(): TrackELoginRecordRepository |
||
| 672 | { |
||
| 673 | return self::$container->get(TrackELoginRecordRepository::class); |
||
| 674 | } |
||
| 675 | |||
| 676 | public static function getThemeHelper(): ThemeHelper |
||
| 679 | } |
||
| 680 | |||
| 681 | public static function getAccessUrlUtil(): AccessUrlHelper |
||
| 682 | { |
||
| 683 | return self::$container->get(AccessUrlHelper::class); |
||
| 684 | } |
||
| 685 | |||
| 686 | public static function getEventDispatcher(): EventDispatcherInterface |
||
| 687 | { |
||
| 688 | /** @var EventDispatcherInterface $dispatcher */ |
||
| 689 | return self::$container->get('event_dispatcher'); |
||
| 690 | } |
||
| 691 | } |
||
| 692 |
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.