Problem/Motivation

I am getting this couple issues after installing key module and most likely both are related to php 8.1 upgrade.
1. [error] Error: Call to a member function getKeyValue() on null in Drupal\key\KeyConfigOverrides->loadOverrides() (line 89 of /app/docroot/modules/contrib/key/src/KeyConfigOverrides.php)

2. Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\key\Plugin\KeyProviderBase::obscureValue() (line 66 of modules/contrib/key/src/Plugin/KeyProviderBase.php).

Steps to reproduce

1. Install key module in a project and add your necessary key overrides.
2. During the project build the 1st issue occurs.
3. Once you bypass the 1st issue and from UI go to the keys /admin/config/system/keys and click on managing one of the keys, the 2nd issue pops up as a deprecated function error.

Proposed resolution

For the first issue, in KeyConfigOverrides.php change this to check the null value first -
Before
$key_value = $storage->load($key_id)->getKeyValue();
After
$key_value = $storage->load($key_id) ? $storage->load($key_id)->getKeyValue() : '';

For the 2nd issue, in KeyProviderBase.php change this -
Before
elseif (strlen($key_value) - $options['visible_right'] > 0) {
After
elseif (!is_null($key_value) && strlen($key_value) - $options['visible_right'] > 0) {

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#4 3385523-4.patch1.26 KBnadim hossain
#3 3385523-3.patch1.26 KBnadim hossain

Issue fork key-3385523

Command icon 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

Nadim Hossain created an issue. See original summary.

nadim hossain’s picture

StatusFileSize
new1.26 KB

Created a merge request and also added this patch version to use it in the composer to avoid using the merge request diff directly.

nadim hossain’s picture

StatusFileSize
new1.26 KB

Thanks for the review @matthew. I have update the merge request and adding a updated patch as well.

neclimdul’s picture

  1. +++ b/src/KeyConfigOverrides.php
    @@ -86,7 +86,7 @@ class KeyConfigOverrides implements ConfigFactoryOverrideInterface {
    -        $key_value = $storage->load($key_id)->getKeyValue();
    +        $key_value = $storage->load($key_id) ? $storage->load($key_id)->getKeyValue() : NULL;
    

    This triggers two loads. It should be either something like:

    $key = $storage->load($key_id);
    $key_value = $key ? $key->getKeyValue() : NULL;
    

    Or if we can require PHP 8 the much easier to read:

    $key_value = $storage($key_id)?->getKeyValue();
    
  2. +++ b/src/Plugin/KeyProviderBase.php
    @@ -63,7 +63,7 @@ abstract class KeyProviderBase extends KeyPluginBase implements KeyProviderInter
    -    elseif (strlen($key_value) - $options['visible_right'] > 0) {
    +    elseif (!is_null($key_value) && strlen($key_value) - $options['visible_right'] > 0) {
    

    The interface says this should only be a string. What's causing it to be null? Do you have a deeper trace so we can maybe fix the code that's not following the interface?

monaw’s picture

i noticed this patch is still not in the latest release which is June 2024 and wondering why not? is there a problem?

rajeshreeputra made their first commit to this issue’s fork.

rajeshreeputra’s picture

Status: Active » Needs review

japerry made their first commit to this issue’s fork.

japerry’s picture

Status: Needs review » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.