Skip to content

Add missing add() method to NullEngine to prevent undefined prefix key error - #19568

Merged
dereuromark merged 1 commit into
cakephp:5.xfrom
CakeDC:issue/adding-add-method-to-null-engine
Jul 28, 2026
Merged

dereuromark merged 1 commit into
cakephp:5.xfrom
CakeDC:issue/adding-add-method-to-null-engine

Conversation

@ajibarra

Copy link
Copy Markdown
Member

When Cache::add() is called with NullEngine, it falls through to the parent CacheEngine::add() which calls $this->_key(). That method accesses $this->_config['prefix'], but NullEngine does not populate _config with defaults since it skips the standard init() setup.

This results in: "Undefined array key prefix" in CacheEngine.php

The fix adds an add() method to NullEngine that simply returns true, consistent with how set(), get(), delete(), and other methods already work in NullEngine (they all no-op and return the expected default).

Reproducible when any code calls Cache::add() on a cache config using NullEngine, for example queue workers using unique job deduplication via Cache::add().

@dereuromark dereuromark added this to the 5.4.1 milestone Jul 28, 2026
@dereuromark

Copy link
Copy Markdown
Member

CI failure here is unrelated to your change. It's the rector step in the "Coding Standard & Static Analysis" job blowing up on a dependency mismatch:

PHP Fatal error:  Uncaught Rector\Exception\Reflection\MissingPrivatePropertyException:
Property "$container" was not found in "PHPStan\Parser\RichParser" class
in vendor/rector/rector/src/Util/Reflection/PrivatesAccessor.php:82
Script vendor/bin/rector process --dry-run handling the rector-check event returned with error code 255

composer rector-setup runs an unpinned update, which pulls rector 2.4.6 against phpstan 2.2.6, and rector's PHPStanContainerMemento::removeRichVisitors() reaches for a private property that no longer exists there. phpstan, phpcs and the split-package analysis all pass. The same failure hits #19565, and this has been red since the last green 5.x run on 2026-07-23, so there is nothing to fix on your branch.

On the change itself: the diagnosis is correct. NullEngine::init() overrides the parent without calling setConfig(), so _configInitialized stays false and _config stays empty. CacheEngine::_key() then reads $this->_config['prefix'] directly instead of going through getConfig(), so the lazy default merge never happens, hence the undefined key. Returning true from add() is consistent with the rest of the class as well: get() returns the default and set() returns true, so the fall-through result would have been true anyway.

Two things worth considering:

  • A test in tests/TestCase/Cache/Engine/NullEngineTest.php covering add().
  • The override fixes this one method, but the underlying fragility stays: any future CacheEngine method touching _config directly breaks on NullEngine the same way, which is exactly how add() slipped through when it was introduced. Either NullEngine::init() calling parent::init($config), or _key() using getConfig('prefix'), would close the whole class.

@ajibarra
ajibarra force-pushed the issue/adding-add-method-to-null-engine branch from 7004542 to 19c6231 Compare July 28, 2026 10:48
@ajibarra

ajibarra commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

You are right, better to ensure parent init is called..Added the test as well to ensure it behaves as expected.

This one failed: CI / testsuite (8.2, mysql, highest) (pull_request) because of "Mysql Server has gone away"..not sure if you can re-run the job

@dereuromark
dereuromark merged commit 3971a30 into cakephp:5.x Jul 28, 2026
12 of 15 checks passed
@dereuromark

Copy link
Copy Markdown
Member

Lets see if 5.x becomes green, it should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants