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

keradus / PHP-CS-Fixer / 22042339290

15 Feb 2026 08:14PM UTC coverage: 92.957% (-0.2%) from 93.171%
22042339290

push

github

keradus
test: check PHP env in CI jobs

29302 of 31522 relevant lines covered (92.96%)

44.04 hits per line

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

84.62
/src/Cache/FileCacheManager.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Cache;
16

17
use PhpCsFixer\Hasher;
18

19
/**
20
 * Class supports caching information about state of fixing files.
21
 *
22
 * Cache is supported only for phar version and version installed via composer.
23
 *
24
 * File will be processed by PHP CS Fixer only if any of the following conditions is fulfilled:
25
 *  - cache is corrupt
26
 *  - fixer version changed
27
 *  - rules changed
28
 *  - file is new
29
 *  - file changed
30
 *
31
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
32
 *
33
 * @internal
34
 *
35
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
36
 */
37
final class FileCacheManager implements CacheManagerInterface
38
{
39
    public const WRITE_FREQUENCY = 10;
40

41
    private FileHandlerInterface $handler;
42

43
    private SignatureInterface $signature;
44

45
    private bool $isDryRun;
46

47
    private DirectoryInterface $cacheDirectory;
48

49
    private int $writeCounter = 0;
50

51
    private bool $signatureWasUpdated = false;
52

53
    private CacheInterface $cache;
54

55
    public function __construct(
56
        FileHandlerInterface $handler,
57
        SignatureInterface $signature,
58
        bool $isDryRun = false,
59
        ?DirectoryInterface $cacheDirectory = null
60
    ) {
61
        $this->handler = $handler;
11✔
62
        $this->signature = $signature;
11✔
63
        $this->isDryRun = $isDryRun;
11✔
64
        $this->cacheDirectory = $cacheDirectory ?? new Directory('');
11✔
65

66
        $this->readCache();
11✔
67
    }
68

69
    public function __destruct()
70
    {
71
        if (true === $this->signatureWasUpdated || 0 !== $this->writeCounter) {
11✔
72
            $this->writeCache();
6✔
73
        }
74
    }
75

76
    /**
77
     * This class is not intended to be serialized,
78
     * and cannot be deserialized (see __wakeup method).
79
     */
80
    public function __serialize(): array
81
    {
82
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
×
83
    }
84

85
    /**
86
     * Disable the deserialization of the class to prevent attacker executing
87
     * code by leveraging the __destruct method.
88
     *
89
     * @param array<string, mixed> $data
90
     *
91
     * @see https://owasp.org/www-community/vulnerabilities/PHP_Object_Injection
92
     */
93
    public function __unserialize(array $data): void
94
    {
95
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
×
96
    }
97

98
    public function needFixing(string $file, string $fileContent): bool
99
    {
100
        $file = $this->cacheDirectory->getRelativePathTo($file);
4✔
101

102
        return !$this->cache->has($file) || $this->cache->get($file) !== $this->calcHash($fileContent);
4✔
103
    }
104

105
    public function setFile(string $file, string $fileContent): void
106
    {
107
        $this->setFileHash($file, $this->calcHash($fileContent));
4✔
108
    }
109

110
    public function setFileHash(string $file, string $hash): void
111
    {
112
        $file = $this->cacheDirectory->getRelativePathTo($file);
4✔
113

114
        if ($this->isDryRun && $this->cache->has($file) && $this->cache->get($file) !== $hash) {
4✔
115
            $this->cache->clear($file);
1✔
116
        } else {
117
            $this->cache->set($file, $hash);
3✔
118
        }
119

120
        if (self::WRITE_FREQUENCY === ++$this->writeCounter) {
4✔
121
            $this->writeCounter = 0;
×
122
            $this->writeCache();
×
123
        }
124
    }
125

126
    private function readCache(): void
127
    {
128
        $cache = $this->handler->read();
11✔
129

130
        if (null === $cache || !$this->signature->equals($cache->getSignature())) {
11✔
131
            $cache = new Cache($this->signature);
2✔
132
            $this->signatureWasUpdated = true;
2✔
133
        }
134

135
        $this->cache = $cache;
11✔
136
    }
137

138
    private function writeCache(): void
139
    {
140
        $this->handler->write($this->cache);
6✔
141
    }
142

143
    private function calcHash(string $content): string
144
    {
145
        return Hasher::calculate($content);
7✔
146
    }
147
}
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