Issue Bug Closed

#97636Warning when trying to update not existing file properties with File::updateProperties

Florian Müller 05/16/22, 10:55 updated 07/05/23, 18:27

Description

We want to add custom properties to a \TYPO3\CMS\Core\Resource\File. However, as these properties may not exist, a warning is thrown.

PHP Warning: Undefined array key "alternative" in typo3/sysext/core/Classes/Resource/File.php line 184

The problematic code:

foreach ($properties as $key => $value) {
  if ($this->properties[$key] !== $value) {
    if (!in_array($key, $this->updatedProperties)) {
      $this->updatedProperties[] = $key;
    }
    $this->properties[$key] = $value;
  }
}

The simplest fix might be to change

$this->properties[$key] !== $value
to
$this->properties[$key] ?? null !== $value

or replace the whole code with something like this (untested):

        $this->updatedProperties = array_merge($this->updatedProperties, array_keys(array_diff_assoc($this->properties, $properties)));
        $this->properties = array_merge($this->properties, $properties);

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 6

  1. Florian Müller changed TYPO3 Version 12 11 05/18/22, 08:42

  2. Oliver Bartsch commented and changed status New Needs Feedback 05/24/22, 10:01

    Hi, thanks for creating an issue.

    I hade a quick look into this. Please note that this method is marked as internal and should therefore actually not be used in extension code. Additionally, the method is only meant to update existing properties, not to add new ones. That seems to be the reason, why there is no further check for existence of $key. What exactly is your use case for adding new properties via this method?

  3. Oliver Bartsch changed Related to #98387 07/05/23, 18:26

  4. Oliver Bartsch changed Related to #98387 07/05/23, 18:26

  5. Oliver Bartsch changed Duplicates #98387 07/05/23, 18:26

  6. Oliver Bartsch commented and changed status Needs Feedback Closed 07/05/23, 18:27

    Closing this as duplicate of #98387, which already fixed the issue.