Problem/Motivation
Ran into this while working on #3364109: Configuration schema & required values: add test coverage for `nullable: true` validation support.
signature \Drupal\Component\Plugin\ConfigurableInterface::setConfiguration(array $configuration) (note type hint)
🆚
signature \Drupal\Component\Plugin\LazyPluginCollection::setConfiguration($configuration) (note NO type hint)
🆚
\Drupal\Core\Plugin\DefaultSingleLazyPluginCollection::setConfiguration() implementation of LazyPluginCollection::setConfiguration():
public function setConfiguration($configuration) {
$this->configuration = $configuration;
$plugin = $this->get($this->instanceId);
if ($plugin instanceof ConfigurableInterface) {
$plugin->setConfiguration($configuration);
}
return $this;
}
Steps to reproduce
$block = Block::load('block.block.olivero_content');
$block->set('settings', NULL);
$typed_data = $this->container->get('typed_data_manager');
$definition = $typed_data->createDataDefinition('entity:block');
$violations = $typed_data->create($definition, $block)->validate();
Results in:
TypeError: Drupal\Core\Block\BlockBase::setConfiguration(): Argument #1 ($configuration) must be of type array, null given, called in /Users/wim.leers/core/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php on line 85
/Users/wim.leers/core/core/lib/Drupal/Core/Block/BlockPluginTrait.php:79
/Users/wim.leers/core/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php:85
/Users/wim.leers/core/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:165
…
Same problem in \Drupal\Core\Plugin\DefaultLazyPluginCollection::setConfiguration() (try visibility on
Proposed resolution
Make
\Drupal\Core\Plugin\DefaultSingleLazyPluginCollection::setConfiguration()
\Drupal\Core\Plugin\DefaultLazyPluginCollection::setConfiguration()
treat NULL as [] — see #8 for rationale.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
N/A
Issue fork drupal-3404023
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:
- 3404023-plugincollection-null-configuration
changes, plain diff MR !5540
Comments
Comment #3
wim leersComment #4
wim leersIMHO this is such a simple change (purely to do with function signatures) that test coverage is excessive here?
Comment #5
wim leersPHPStan gets in the way once again. Locally, PHPStan does not complain 😬
Tried an alternative implementation.
Comment #6
mglamanI don't know why it passed locally, but it's probably trusting the phpdoc which probably says it's always an array and never null.
Comment #7
wim leersThanks, @mglaman, that makes sense!
Note that an alternative solution could be to modify
\Drupal\Core\Config\Entity\ConfigEntityBase::set():👆 We could make it not call
::setConfiguration()if$valueis NULL.Comment #8
mglamanI read
nullas reset/no config. So I'd transformnullto[]and still call. Otherwise the intent to remove or clear configuration wouldn't happen and existing configuration would persist.The idea that null === remove.
Comment #9
smustgrave commentedIf I'm understanding correctly believe the suggestion is to revert
https://git.drupalcode.org/project/drupal/-/merge_requests/5540/diffs?co...
Right?
Comment #10
wim leersDone.
Comment #11
phenaproximaThis looks good, but I think it could use a comment. Also, don't we need test coverage for this?
Comment #12
smustgrave commentedNW for the inline comment.
in #4 was mentioned test coverage may be overkill but if there's valid concern then test coverage should be added too.
Comment #13
wim leersComment #14
borisson_I'm not sure about the solution chosen here, should we deprecate calling DefaultSingleLazyPluginCollection::setConfiguration() with NULL, and attempt to make it typehinted in drupal 11?
Comment #15
wim leers@phenaproxima: Test coverage added. The test-only job fails 👍
@borisson_: Done.
But note that it's not that simple, becauseTurns out it is simple: https://3v4l.org/dcCF0 👍 CR: https://www.drupal.org/node/3406191\Drupal\Component\Plugin\LazyPluginCollection::setConfiguration()is an abstract method, which contrib/custom modules may be implementing too. How can we warn those?Comment #16
borisson_Looks great now.
Comment #17
phenaproximaOne small question about adding a future-proof type hint.
Comment #18
wim leersAFAICT PHPStan does not allow us to do that. See MR for PHPStan output.
Comment #19
phenaproximaWhat the hell is PHPStan talking about??? That is contravariant.
From https://www.php.net/manual/en/language.oop5.variance.php (my emphasis):
Comment #20
phenaproximaOh, I see -- PHPStan is annoyed because of the direction of the contravariance, since this MR doesn't change
\Drupal\Component\Plugin\LazyPluginCollection::setConfiguration.But then I gotta wonder...why aren't we also changing that method too? The doc comment says that $configuration needs to be an array.
Comment #21
wim leersBecause the end goal is
public function setConfiguration(array $configuration), without the?.And if we set it to
?arraynow, then it's impossible to change the typehint toarrayin 11.arrayworks,?arraydoes not workComment #22
phenaproximaYeah, that makes sense. It's too bad we can't change the type hints now, but in light of the discovery about
LazyPluginCollection, I think what we have is a good approach. In Drupal 11 we can fix it all properly,array $configurationeverywhere!Adjusting credit and restoring RTBC.
Comment #23
wim leersYay!
Comment #24
alexpottAsked a question / have a suggestion on the MR.
Comment #25
wim leersSee #15 and #21 for 3v4l.org examples for why the current typehint additions are AFAICT fine and @phenaproxima's last MR comment is AFAICT correct.
What are we missing?
Comment #26
wim leersAFAICT everything is actually addressed:
So: back to RTBC. Alex mentioned in #24 a question was asked on the MR, but I don't see any question, only responses on _suggestion threads_ that were responded to here in the issue? 🤔
Comment #27
alexpottCommitted 871bc1a and pushed to 11.x. Thanks!
Comment #29
quietone commentedPublished the CR