Bug #97636
closedWarning when trying to update not existing file properties with File::updateProperties
0%
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);
Updated by Oliver Bartsch over 2 years ago
- Status changed from New to Needs Feedback
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?
Updated by Oliver Bartsch over 1 year ago
- Related to Bug #98387: PHP Warning: Undefined array key "XYZ" in File line 184 added
Updated by Oliver Bartsch over 1 year ago
- Related to deleted (Bug #98387: PHP Warning: Undefined array key "XYZ" in File line 184)
Updated by Oliver Bartsch over 1 year ago
- Is duplicate of Bug #98387: PHP Warning: Undefined array key "XYZ" in File line 184 added
Updated by Oliver Bartsch over 1 year ago
- Status changed from Needs Feedback to Closed
Closing this as duplicate of #98387, which already fixed the issue.