Add dependency injection coding standards - #228
Draft
cameron1729 wants to merge 1 commit into
Draft
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Disallow injecting the core dependency injection container and add an optional Moodle Extra warning for container lookups inside classes.
cameron1729
force-pushed
the
dependency-injection-coding-standards
branch
from
July 19, 2026 09:25
6ed830c to
a9d9ef5
Compare
cameron1729
marked this pull request as draft
July 19, 2026 09:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds guidance for using Moodle's dependency injection container without introducing service location patterns.
It introduces two checks:
\core\dilookupsRationale
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