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

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 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 __sleep(): 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
     * @see https://owasp.org/www-community/vulnerabilities/PHP_Object_Injection
90
     */
91
    public function __wakeup(): void
92
    {
93
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
×
94
    }
95

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

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

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

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

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

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

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

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

133
        $this->cache = $cache;
11✔
134
    }
135

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

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