• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 19718738277

26 Nov 2025 10:10PM UTC coverage: 84.513% (-0.003%) from 84.516%
19718738277

Pull #9809

github

web-flow
Merge 150a1228f into 2609108c9
Pull Request #9809: feat(cache): add deleteMatching method definition in CacheInterface

2 of 7 new or added lines in 3 files covered. (28.57%)

147 existing lines in 7 files now uncovered.

21473 of 25408 relevant lines covered (84.51%)

196.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.62
/system/Cache/Handlers/BaseHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Cache\Handlers;
15

16
use Closure;
17
use CodeIgniter\Cache\CacheInterface;
18
use CodeIgniter\Exceptions\InvalidArgumentException;
19
use Config\Cache;
20

21
/**
22
 * Base class for cache handling
23
 *
24
 * @see \CodeIgniter\Cache\Handlers\BaseHandlerTest
25
 */
26
abstract class BaseHandler implements CacheInterface
27
{
28
    /**
29
     * Reserved characters that cannot be used in a key or tag. May be overridden by the config.
30
     * From https://github.com/symfony/cache-contracts/blob/c0446463729b89dd4fa62e9aeecc80287323615d/ItemInterface.php#L43
31
     *
32
     * @deprecated in favor of the Cache config
33
     */
34
    public const RESERVED_CHARACTERS = '{}()/\@:';
35

36
    /**
37
     * Maximum key length.
38
     */
39
    public const MAX_KEY_LENGTH = PHP_INT_MAX;
40

41
    /**
42
     * Prefix to apply to cache keys.
43
     * May not be used by all handlers.
44
     *
45
     * @var string
46
     */
47
    protected $prefix;
48

49
    /**
50
     * Validates a cache key according to PSR-6.
51
     * Keys that exceed MAX_KEY_LENGTH are hashed.
52
     * From https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158
53
     *
54
     * @param mixed  $key    The key to validate
55
     * @param string $prefix Optional prefix to include in length calculations
56
     *
57
     * @throws InvalidArgumentException When $key is not valid
58
     */
59
    public static function validateKey($key, $prefix = ''): string
60
    {
61
        if (! is_string($key)) {
244✔
62
            throw new InvalidArgumentException('Cache key must be a string');
5✔
63
        }
64
        if ($key === '') {
239✔
65
            throw new InvalidArgumentException('Cache key cannot be empty.');
×
66
        }
67

68
        $reserved = config(Cache::class)->reservedCharacters;
239✔
69

70
        if ($reserved !== '' && strpbrk($key, $reserved) !== false) {
239✔
71
            throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved);
1✔
72
        }
73

74
        // If the key with prefix exceeds the length then return the hashed version
75
        return strlen($prefix . $key) > static::MAX_KEY_LENGTH ? $prefix . md5($key) : $prefix . $key;
238✔
76
    }
77

78
    /**
79
     * Get an item from the cache, or execute the given Closure and store the result.
80
     *
81
     * @param string           $key      Cache item name
82
     * @param int              $ttl      Time to live
83
     * @param Closure(): mixed $callback Callback return value
84
     */
85
    public function remember(string $key, int $ttl, Closure $callback): mixed
86
    {
87
        $value = $this->get($key);
4✔
88

89
        if ($value !== null) {
4✔
UNCOV
90
            return $value;
×
91
        }
92

93
        $this->save($key, $value = $callback(), $ttl);
4✔
94

95
        return $value;
4✔
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc