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

codeigniter4 / CodeIgniter4 / 20396177438

20 Dec 2025 03:10PM UTC coverage: 84.549% (+0.001%) from 84.548%
20396177438

push

github

web-flow
fix: `null` as an array offset is deprecated, use an empty string instead (#9849)

* fix: null as an array offset is deprecated, use an empty string instead

Co-authored-by: Denny Septian Panggabean <97607754+ddevsr@users.noreply.github.com>

* update tests signature

---------

Co-authored-by: Denny Septian Panggabean <97607754+ddevsr@users.noreply.github.com>

4 of 5 new or added lines in 1 file covered. (80.0%)

21532 of 25467 relevant lines covered (84.55%)

197.15 hits per line

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

89.36
/system/Autoloader/FileLocatorCached.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\Autoloader;
15

16
use CodeIgniter\Cache\CacheInterface;
17
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
18

19
/**
20
 * FileLocator with Cache
21
 *
22
 * @see \CodeIgniter\Autoloader\FileLocatorCachedTest
23
 */
24
final class FileLocatorCached implements FileLocatorInterface
25
{
26
    /**
27
     * @var CacheInterface|FileVarExportHandler
28
     */
29
    private $cacheHandler;
30

31
    /**
32
     * Cache data
33
     *
34
     * [method => data]
35
     * E.g.,
36
     * [
37
     *     'search' => [$path => $foundPaths],
38
     * ]
39
     *
40
     * @var array<string, array<string, mixed>>
41
     */
42
    private array $cache = [];
43

44
    /**
45
     * Is the cache updated?
46
     */
47
    private bool $cacheUpdated = false;
48

49
    private string $cacheKey = 'FileLocatorCache';
50

51
    /**
52
     * @param CacheInterface|FileVarExportHandler|null $cache
53
     */
54
    public function __construct(private readonly FileLocator $locator, $cache = null)
55
    {
56
        $this->cacheHandler = $cache ?? new FileVarExportHandler();
32✔
57

58
        $this->loadCache();
32✔
59
    }
60

61
    private function loadCache(): void
62
    {
63
        $data = $this->cacheHandler->get($this->cacheKey);
32✔
64

65
        if (is_array($data)) {
32✔
66
            $this->cache = $data;
31✔
67
        }
68
    }
69

70
    public function __destruct()
71
    {
72
        $this->saveCache();
32✔
73
    }
74

75
    private function saveCache(): void
76
    {
77
        if ($this->cacheUpdated) {
32✔
78
            $this->cacheHandler->save($this->cacheKey, $this->cache, 3600 * 24);
30✔
79
        }
80
    }
81

82
    /**
83
     * Delete cache data
84
     */
85
    public function deleteCache(): void
86
    {
87
        $this->cacheUpdated = false;
1✔
88
        $this->cacheHandler->delete($this->cacheKey);
1✔
89
    }
90

91
    public function findQualifiedNameFromPath(string $path): false|string
92
    {
93
        if (isset($this->cache['findQualifiedNameFromPath'][$path])) {
3✔
94
            return $this->cache['findQualifiedNameFromPath'][$path];
×
95
        }
96

97
        $classname = $this->locator->findQualifiedNameFromPath($path);
3✔
98

99
        $this->cache['findQualifiedNameFromPath'][$path] = $classname;
3✔
100
        $this->cacheUpdated                              = true;
3✔
101

102
        return $classname;
3✔
103
    }
104

105
    public function getClassname(string $file): string
106
    {
107
        if (isset($this->cache['getClassname'][$file])) {
3✔
108
            return $this->cache['getClassname'][$file];
×
109
        }
110

111
        $classname = $this->locator->getClassname($file);
3✔
112

113
        $this->cache['getClassname'][$file] = $classname;
3✔
114
        $this->cacheUpdated                 = true;
3✔
115

116
        return $classname;
3✔
117
    }
118

119
    /**
120
     * @return list<non-empty-string>
121
     */
122
    public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
123
    {
124
        if (isset($this->cache['search'][$path][$ext][$prioritizeApp])) {
5✔
125
            return $this->cache['search'][$path][$ext][$prioritizeApp];
×
126
        }
127

128
        $foundPaths = $this->locator->search($path, $ext, $prioritizeApp);
5✔
129

130
        $this->cache['search'][$path][$ext][$prioritizeApp] = $foundPaths;
5✔
131
        $this->cacheUpdated                                 = true;
5✔
132

133
        return $foundPaths;
5✔
134
    }
135

136
    public function listFiles(string $path): array
137
    {
138
        if (isset($this->cache['listFiles'][$path])) {
6✔
139
            return $this->cache['listFiles'][$path];
1✔
140
        }
141

142
        $files = $this->locator->listFiles($path);
5✔
143

144
        $this->cache['listFiles'][$path] = $files;
5✔
145
        $this->cacheUpdated              = true;
5✔
146

147
        return $files;
5✔
148
    }
149

150
    public function listNamespaceFiles(string $prefix, string $path): array
151
    {
152
        if (isset($this->cache['listNamespaceFiles'][$prefix][$path])) {
1✔
153
            return $this->cache['listNamespaceFiles'][$prefix][$path];
×
154
        }
155

156
        $files = $this->locator->listNamespaceFiles($prefix, $path);
1✔
157

158
        $this->cache['listNamespaceFiles'][$prefix][$path] = $files;
1✔
159
        $this->cacheUpdated                                = true;
1✔
160

161
        return $files;
1✔
162
    }
163

164
    public function locateFile(string $file, ?string $folder = null, string $ext = 'php'): false|string
165
    {
166
        $folderKey = $folder ?? '';
13✔
167

168
        if (isset($this->cache['locateFile'][$file][$folderKey][$ext])) {
13✔
NEW
169
            return $this->cache['locateFile'][$file][$folderKey][$ext];
×
170
        }
171

172
        $files = $this->locator->locateFile($file, $folder, $ext);
13✔
173

174
        $this->cache['locateFile'][$file][$folderKey][$ext] = $files;
13✔
175
        $this->cacheUpdated                                 = true;
13✔
176

177
        return $files;
13✔
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc