Skip to content

Add dependency injection coding standards - #228

Draft
cameron1729 wants to merge 1 commit into
moodlehq:mainfrom
cameron1729:dependency-injection-coding-standards
Draft

Add dependency injection coding standards#228
cameron1729 wants to merge 1 commit into
moodlehq:mainfrom
cameron1729:dependency-injection-coding-standards

Conversation

@cameron1729

@cameron1729 cameron1729 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds guidance for using Moodle's dependency injection container without introducing service location patterns.

It introduces two checks:

  • A coding standard error when application code receives the DI container as a dependency (this is explicitly prohibited in developer documentation)
  • An opt-in warning in moodle-extra, for when a class performs direct \core\di lookups

Rationale

Dependency injection means that classes declare their immediate collaborators. Injecting the container instead gives a class access to arbitrary services, hides its real dependencies, and effectively turns the container into a service locator.

Direct container lookups inside classes are also usually service location. However, container access is legitimate at composition roots and other application boundaries, so this is provided as a warning through Moodle Extra rather than an unconditional error.

Related work

This change was prompted by the propsed Calendar API migration in MDL-89216, but the coding standard rules added here apply generally to the existing DI container and can be merged independently.

Supporting improvements to the developer documentation are also in progress here: moodle/devdocs#1640

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.34%. Comparing base (b60df78) to head (a9d9ef5).

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #228      +/-   ##
============================================
+ Coverage     98.27%   98.34%   +0.06%     
- Complexity      994     1050      +56     
============================================
  Files            42       44       +2     
  Lines          2961     3082     +121     
============================================
+ Hits           2910     3031     +121     
  Misses           51       51              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Disallow injecting the core dependency injection container and add an
optional Moodle Extra warning for container lookups inside classes.
@cameron1729
cameron1729 force-pushed the dependency-injection-coding-standards branch from 6ed830c to a9d9ef5 Compare July 19, 2026 09:25
@cameron1729
cameron1729 marked this pull request as draft July 19, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant