Problem/Motivation
PHPStan (mglaman/phpstan-drupal) reports the following against SvgImageFieldFormatter:
Logger assigned from LoggerChannelFactory in a class using DependencySerializationTrait will break serialization. Inject a named logger channel service directly (e.g. @logger.channel.my_channel) instead.
loggerFromFactory.propertyAssignment
The formatter's constructor receives logger.factory and stores $factory->get('svg_image_field') in $this->logger. FormatterBase uses DependencySerializationTrait, which only swaps out properties that are container services (objects with a _serviceId). A channel obtained from the factory is not one, so if the formatter is ever serialized (for example in cached form state during AJAX rebuilds), the whole channel is serialized along with it, including its attached loggers, the request stack and the current user. That can fail outright or bloat the serialized data.
Steps to reproduce
Run PHPStan with mglaman/phpstan-drupal against the module, e.g. phpstan analyse src.
Proposed resolution
- Add
svg_image_field.services.ymldefininglogger.channel.svg_image_field(parentlogger.channel_base, argumentsvg_image_field). This is the same channel the factory returns, so log output is unchanged. - Inject
@logger.channel.svg_image_fieldinSvgImageFieldFormatter::create(). - For backwards compatibility, keep accepting
LoggerChannelFactoryInterfacein the constructor with anE_USER_DEPRECATEDnotice (deprecated in 2.3.8, removed in 3.0.0). In that case, load the named channel from the container so serialization is still fixed. - Add an empty post-update hook so sites rebuild the container and register the new service. Without a rebuild, rendering SVG image fields would throw
ServiceNotFoundException.
Remaining tasks
- Review MR.
- Update the deprecation message's "See" link to point at this issue (or a change record).
User interface changes
None.
API changes
The 8th constructor argument of SvgImageFieldFormatter is now LoggerChannelInterface|LoggerChannelFactoryInterface. Subclasses passing logger.factory continue to work but trigger a deprecation; they should pass @logger.channel.svg_image_field instead. New service: logger.channel.svg_image_field.
Data model changes
None. Sites must run database updates (or rebuild caches) after upgrading.
Issue fork svg_image_field-3629891
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
mandclu commented