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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 hits per line

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

92.98
/src/Cache/Cache.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\Utils;
18

19
/**
20
 * @author Andreas Möller <am@localheinz.com>
21
 *
22
 * @internal
23
 *
24
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
25
 */
26
final class Cache implements CacheInterface
27
{
28
    private SignatureInterface $signature;
29

30
    /**
31
     * @var array<string, string>
32
     */
33
    private array $hashes = [];
34

35
    public function __construct(SignatureInterface $signature)
36
    {
37
        $this->signature = $signature;
7✔
38
    }
39

40
    public function getSignature(): SignatureInterface
41
    {
42
        return $this->signature;
4✔
43
    }
44

45
    public function has(string $file): bool
46
    {
47
        return \array_key_exists($file, $this->hashes);
5✔
48
    }
49

50
    public function get(string $file): ?string
51
    {
52
        if (!$this->has($file)) {
5✔
53
            return null;
2✔
54
        }
55

56
        return $this->hashes[$file];
3✔
57
    }
58

59
    public function set(string $file, string $hash): void
60
    {
61
        $this->hashes[$file] = $hash;
4✔
62
    }
63

64
    public function clear(string $file): void
65
    {
66
        unset($this->hashes[$file]);
1✔
67
    }
68

69
    public function toJson(): string
70
    {
71
        $json = json_encode([
3✔
72
            'php' => $this->getSignature()->getPhpVersion(),
3✔
73
            'version' => $this->getSignature()->getFixerVersion(),
3✔
74
            'indent' => $this->getSignature()->getIndent(),
3✔
75
            'lineEnding' => $this->getSignature()->getLineEnding(),
3✔
76
            'rules' => $this->getSignature()->getRules(),
3✔
77
            'hashes' => $this->hashes,
3✔
78
        ]);
3✔
79

80
        if (\JSON_ERROR_NONE !== json_last_error() || false === $json) {
3✔
81
            throw new \UnexpectedValueException(\sprintf(
1✔
82
                'Cannot encode cache signature to JSON, error: "%s". If you have non-UTF8 chars in your signature, like in license for `header_comment`, consider enabling `ext-mbstring` or install `symfony/polyfill-mbstring`.',
1✔
83
                json_last_error_msg()
1✔
84
            ));
1✔
85
        }
86

87
        return $json;
2✔
88
    }
89

90
    /**
91
     * @throws \InvalidArgumentException
92
     */
93
    public static function fromJson(string $json): self
94
    {
95
        $data = json_decode($json, true);
7✔
96

97
        if (null === $data && \JSON_ERROR_NONE !== json_last_error()) {
7✔
98
            throw new \InvalidArgumentException(\sprintf(
1✔
99
                'Value needs to be a valid JSON string, got "%s", error: "%s".',
1✔
100
                $json,
1✔
101
                json_last_error_msg()
1✔
102
            ));
1✔
103
        }
104

105
        $requiredKeys = [
6✔
106
            'php',
6✔
107
            'version',
6✔
108
            'indent',
6✔
109
            'lineEnding',
6✔
110
            'rules',
6✔
111
            'hashes',
6✔
112
        ];
6✔
113

114
        $missingKeys = array_diff_key(array_flip($requiredKeys), $data);
6✔
115

116
        if (\count($missingKeys) > 0) {
6✔
117
            throw new \InvalidArgumentException(\sprintf(
4✔
118
                'JSON data is missing keys %s',
4✔
119
                Utils::naturalLanguageJoin(array_keys($missingKeys))
4✔
120
            ));
4✔
121
        }
122

123
        $signature = new Signature(
2✔
124
            $data['php'],
2✔
125
            $data['version'],
2✔
126
            $data['indent'],
2✔
127
            $data['lineEnding'],
2✔
128
            $data['rules']
2✔
129
        );
2✔
130

131
        $cache = new self($signature);
2✔
132

133
        // before v3.11.1 the hashes were crc32 encoded and saved as integers
134
        // @TODO: remove the to string cast/array_map in v4.0
135
        $cache->hashes = array_map(static fn ($v): string => \is_int($v) ? (string) $v : $v, $data['hashes']);
2✔
136

137
        return $cache;
2✔
138
    }
139

140
    /**
141
     * @internal
142
     */
143
    public function backfillHashes(self $oldCache): bool
144
    {
145
        if (!$this->getSignature()->equals($oldCache->getSignature())) {
×
146
            return false;
×
147
        }
148

149
        $this->hashes = array_merge($oldCache->hashes, $this->hashes);
×
150

151
        return true;
×
152
    }
153
}
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