fix Using null as an array offset is deprecated, use an empty string instead#1596
fix Using null as an array offset is deprecated, use an empty string instead#1596mambax7 wants to merge 5 commits intoXOOPS:masterfrom
Conversation
…instead fix offset is deprecated, use an empty string instead replacing extract()
There was a problem hiding this comment.
Pull request overview
This PR addresses PHP deprecation warnings related to using null as an array offset by replacing extract() calls with explicit variable extraction and adding validation checks before using variables as array keys.
Key changes:
- Replaced
extract()calls inconfig()andwrite()methods with explicit array key checking and assignment - Added string type and empty string validation before using variables as array offsets
- Updated parameter documentation to reflect that null values are accepted
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
htdocs/class/cache/xoopscache.php
Outdated
| if (isset($name['name']) && is_string($name['name'])) { | ||
| $name = $name['name']; | ||
| } | ||
| if (isset($name['settings']) && is_array($name['settings'])) { | ||
| $settings = array_merge($settings, $name['settings']); |
There was a problem hiding this comment.
After reassigning $name to a string on line 113, the code attempts to access $name['settings'] on line 115. This will cause a "Cannot use a scalar value as an array" error. The original array should be preserved in a separate variable before extracting values from it.
| if (isset($name['name']) && is_string($name['name'])) { | |
| $name = $name['name']; | |
| } | |
| if (isset($name['settings']) && is_array($name['settings'])) { | |
| $settings = array_merge($settings, $name['settings']); | |
| $config = $name; | |
| if (isset($config['name']) && is_string($config['name'])) { | |
| $name = $config['name']; | |
| } | |
| if (isset($config['settings']) && is_array($config['settings'])) { | |
| $settings = array_merge($settings, $config['settings']); |
htdocs/class/cache/xoopscache.php
Outdated
|
|
||
| $config = $_this->config($config); | ||
|
|
||
|
|
There was a problem hiding this comment.
Extra blank line. There are two consecutive blank lines here which is inconsistent with the rest of the codebase.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
htdocs/class/cache/xoopscache.php
Outdated
|
|
||
| $config = $_this->config($config); | ||
|
|
||
|
|
There was a problem hiding this comment.
Extra blank line should be removed for code consistency.
htdocs/class/cache/xoopscache.php
Outdated
| } | ||
|
|
||
| if (isset($config['settings']) && is_array($config['settings'])) { | ||
| $settings = array_merge($settings, $config['settings']); |
There was a problem hiding this comment.
The behavior change from using extract to array_merge could introduce subtle bugs. The old code would completely replace settings with the value from the config array, but the new code merges them. If the intention is to maintain backward compatibility, line 119 should be changed to assign the settings directly rather than merging: $settings = $config['settings']; instead of $settings = array_merge($settings, $config['settings']);
| $settings = array_merge($settings, $config['settings']); | |
| $settings = $config['settings']; |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
htdocs/class/cache/xoopscache.php:435
- Potential null array offset issue: When
$_this->nameis null (which is its initial value), accessing$_this->configs[$_this->name]will use null as an array key. This should be guarded with a null check on$_this->namebefore attempting array access, similar to the pattern used in the updatedconfigmethod at lines 156-161.
if (!$engine && isset($_this->configs[$_this->name]['engine'])) {
$engine = $_this->configs[$_this->name]['engine'];
}
htdocs/class/cache/xoopscache.php:451
- Potential null array offset issue: When
$_this->nameis null (which is its initial value), accessing$_this->configs[$_this->name]will use null as an array key. This should be guarded with a null check on$_this->namebefore attempting array access, similar to the pattern used in the updatedconfigmethod at lines 156-161.
if (!$engine && isset($_this->configs[$_this->name]['engine'])) {
$engine = $_this->configs[$_this->name]['engine'];
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
|
|
There was a problem hiding this comment.
Excessive blank lines: There are three consecutive blank lines (lines 99-100) which is inconsistent with the rest of the codebase that typically uses single blank lines between methods.
| $config = $_this->resolveEngineConfig($config); | ||
| if ($config === false) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Inconsistent return type behavior: The method returns false when config resolution fails (line 338), but the PHPDoc at line 328 states the return type is mixed (cached data or false). While this is technically correct, it creates an ambiguity where false could mean either "config failed" or "cached data is false". Consider documenting this nuance or using a different sentinel value for configuration failures.
| } | ||
|
|
||
| $engine = $config['engine']; | ||
|
|
There was a problem hiding this comment.
Missing null check: The code assumes $_this->engine[$engine] exists and is not null before calling gc() on it. If the engine initialization failed (line 224 sets it to null), this will cause a fatal error. An isInitialized check should be added before accessing the engine, similar to the pattern used in write, read, delete, and clear methods.
| if (!isset($_this->engine[$engine]) || $_this->engine[$engine] === null) { | |
| return false; | |
| } |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * @var array<string, XoopsCacheEngine|null> | ||
| * @access protected | ||
| */ | ||
| protected $engine = []; |
There was a problem hiding this comment.
The $engine property is now initialized as an empty array, but throughout the codebase it's used with string keys (e.g., $_this->engine[$name]). Consider adding a check in the engine() method to ensure that $_this->engine[$name] is not just isset() but also an instance of the expected cache engine class before calling methods on it, to prevent potential type errors if the array is manipulated incorrectly elsewhere.
htdocs/class/cache/xoopscache.php
Outdated
| */ | ||
| private function resolveEngineName($engine) | ||
| { | ||
| if (is_string($engine) && $engine !== '') { |
There was a problem hiding this comment.
The validation $engine !== '' is checking for empty strings, but the parameter type allows string|null. Consider explicitly checking for null first ($engine !== null) before the string checks for clearer intent and to handle the null case distinctly from empty strings.
| if (is_string($engine) && $engine !== '') { | |
| if ($engine !== null && is_string($engine) && $engine !== '') { |
| $config = $name; | ||
|
|
||
| if (isset($config['name']) && is_string($config['name'])) { | ||
| $name = $config['name']; | ||
| } |
There was a problem hiding this comment.
When $name is an array, it's assigned to $config, but if $config['name'] is not set or not a string, $name remains an array. This could lead to type errors in subsequent code that expects $name to be a string. Consider setting a default string value (e.g., 'default') if the name cannot be extracted from the config array.
htdocs/class/cache/xoopscache.php
Outdated
| } | ||
|
|
||
| if (isset($config['settings']) && is_array($config['settings'])) { | ||
| // Preserve legacy behavior: overwrite settings, do NOT merge |
There was a problem hiding this comment.
The comment uses 'do NOT merge' for emphasis, but the standard style would be 'do not merge' without capitalization. Consider using consistent casing for better readability.
| // Preserve legacy behavior: overwrite settings, do NOT merge | |
| // Preserve legacy behavior: overwrite settings, do not merge |
htdocs/class/cache/xoopscache.php
Outdated
| // Check if engine is initialized before calling gc() | ||
| if (!$_this->isInitialized($engine)) { | ||
| trigger_error("Cache engine {$engine} not initialized for garbage collection", E_USER_WARNING); | ||
| return false; | ||
| } | ||
|
|
There was a problem hiding this comment.
The isInitialized() method now returns false if $engine is null (from resolveEngineName()). However, at line 274, $engine is extracted from the validated config, so it should always be a string. The check at line 277 is redundant given that resolveEngineConfig() already validates the engine. Consider removing this check or documenting why it's necessary despite the earlier validation.
| // Check if engine is initialized before calling gc() | |
| if (!$_this->isInitialized($engine)) { | |
| trigger_error("Cache engine {$engine} not initialized for garbage collection", E_USER_WARNING); | |
| return false; | |
| } |
htdocs/class/cache/xoopscache.php
Outdated
| return is_string($engine) | ||
| && isset($_this->engine[$engine]) | ||
| && $_this->engine[$engine] !== null; |
There was a problem hiding this comment.
The triple condition check is_string($engine) && isset($_this->engine[$engine]) && $_this->engine[$engine] !== null could be simplified. If $engine is null or not a string, the isset() check would return false anyway. Consider reordering to is_string($engine) && !empty($_this->engine[$engine]) for conciseness, though this changes semantics slightly if the engine value is falsy but not null.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| $engine = $config['engine']; | ||
|
|
||
| // Engine configuration is valid, but runtime initialization may have failed |
There was a problem hiding this comment.
Comment formatting is inconsistent with project standards. There should be a space after the double slashes in inline comments.
| // Engine configuration is valid, but runtime initialization may have failed | |
| // Engine configuration is valid, but runtime initialization may have failed |
| * @package core | ||
| * @subpackage cache | ||
| * @since 2.5.12 | ||
| * @deprecated 4.0.0 Use Xoops\Core\Cache\EngineInterface instead |
There was a problem hiding this comment.
The deprecation notice refers to version "4.0.0" which appears to be a hypothetical future version. While this is acceptable for deprecation notices indicating long-term plans, consider whether a more concrete version number should be used if the deprecation timeline is known, or add a note explaining this is a planned future major version.
| * @deprecated 4.0.0 Use Xoops\Core\Cache\EngineInterface instead | |
| * @deprecated 4.0.0 (planned future major version) Use Xoops\Core\Cache\EngineInterface instead |
No description provided.