Skip to content

[Cache] Psr16Cache incompatible with non-Symfony cache pools #44736

@colinodell

Description

@colinodell

Symfony version(s) affected

6.0.0+

Description

#42025 added types to all private properties, including Psr16Cache::$createCacheItem. The constructor will only set this property if the $pool also implements Symfony\Component\Cache\Adapter\AdapterInterface. However, later on in set() and setMultiple(), the Psr16Cache attempts to check if $createCacheItem is null - this triggers the following uncaught Error:

Typed property Symfony\Component\Cache\Psr16Cache::$createCacheItem must not be accessed before initialization

As a result, this class does not work with non-Symfony cache pool instances since 6.0.0.

How to reproduce

Instantiate a new Psr16Cache with anything that implements CacheItemPoolInterface but not AdapterInterface. For example:

use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;

$cache = new Psr16Cache(new ExternalAdapter());
$cache->set('foo', 'bar');

Possible Solution

Making Psr16Cache::$createCacheItem nullable would fix this issue.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions