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

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

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

78.57
/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\BadMethodCallException;
19
use CodeIgniter\Exceptions\InvalidArgumentException;
20
use Config\Cache;
21
use Exception;
22

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

38
    /**
39
     * Maximum key length.
40
     */
41
    public const MAX_KEY_LENGTH = PHP_INT_MAX;
42

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

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

70
        $reserved = config(Cache::class)->reservedCharacters ?? self::RESERVED_CHARACTERS;
223✔
71
        if ($reserved !== '' && strpbrk($key, $reserved) !== false) {
223✔
72
            throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved);
1✔
73
        }
74

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

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

92
        if ($value !== null) {
4✔
93
            return $value;
×
94
        }
95

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

98
        return $value;
4✔
99
    }
100

101
    /**
102
     * Deletes items from the cache store matching a given pattern.
103
     *
104
     * @param string $pattern Cache items glob-style pattern
105
     *
106
     * @return int|never
107
     *
108
     * @throws Exception
109
     */
110
    public function deleteMatching(string $pattern)
111
    {
112
        throw new BadMethodCallException('The deleteMatching method is not implemented.');
×
113
    }
114
}
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

© 2025 Coveralls, Inc