feat: Add typed Grav container access, enabling IDE autocomplete and static analysis#4050
Open
NoNoNo wants to merge 1 commit intogetgrav:1.8from
Open
feat: Add typed Grav container access, enabling IDE autocomplete and static analysis#4050NoNoNo wants to merge 1 commit intogetgrav:1.8from
NoNoNo wants to merge 1 commit intogetgrav:1.8from
Conversation
…()` magic method
This commit enables IDE autocomplete and static analysis for Grav's
dependency injection container, eliminating the need for manual `@var`
annotations when accessing services.
Changes:
- Add `__get()` and `__isset()` magic methods to `Grav\Framework\DI\Container`
that bridge property access (`$grav->log`) to array access (`$grav[log]`)
- Add `@property-read` PHPDoc annotations to `Grav\Common\Grav` for all
core services (config, pages, assets, uri, log, twig, etc.) plus
the popular admin plugin service
- Add comprehensive test suite (66 tests) covering Container magic
methods and Grav property access
Both syntaxes work simultaneously:
Old: `$this->grav['log']->info('Test')` // still works
New: `$this->grav->log->info('Test')` // IDE autocomplete works
Benefits:
- Full IDE autocomplete for all container services
- PHPStan/Psalm can validate service method calls
- Instant code navigation to service classes
- No breaking changes, zero runtime overhead
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 enables IDE autocomplete and static analysis for Grav's dependency injection container by adding:
__get()magic method toGrav\Framework\DI\Containerthat bridges property access to array access@property-readPHPDoc annotations onGrav\Common\Gravfor all core servicesResult: Plugins can now use
$this->grav->log->info('Test')instead of$this->grav['log']->info('Test'), with full IDE support.The Problem
Grav's container uses
ArrayAccesswith string keys. This provides zero type information to IDEs and static analyzers:Developers must manually annotate every access:
This is tedious, error-prone, and inconsistent across the codebase.
Based on analysis of all published Grav plugins, the most accessed services are:
twigassetsconfiguripagelanguagelocatorloguserpagesadmindebuggersessionThe Solution
1.
__get()Magic Method (Container.php)Add magic property access that delegates to
ArrayAccess::offsetGet():2.
@property-readAnnotations (Grav.php)Add PHPDoc annotations for all core services:
How It Works
Usage
Before
After
Both syntaxes work simultaneously. No breaking changes.
Benefits
Impact
Testing
Test Coverage
Two test files with 66 tests covering:
ContainerTest.php (29 tests, 44 assertions):
__get()magic method: string, int, array, bool, null, object parameters__get()service resolution: lazy services, factory services, invokable objects__isset()behavior: existing/non-existing, null, false, zero, empty string/arrayGravPropertyAccessTest.php (37 tests, 86 assertions):
__isset()for all annotated servicesRunning Tests
Files Changed
system/src/Grav/Framework/DI/Container.php__get()and__isset()methods (21 lines)system/src/Grav/Common/Grav.php@property-readannotations for all services (28 annotations)tests/unit/Grav/Framework/DI/ContainerTest.phptests/unit/Grav/Common/GravPropertyAccessTest.phpMigration for Plugins
Plugins can gradually adopt the new syntax:
No plugin code changes are required. The old
ArrayAccesssyntax continues to work.