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

keradus / PHP-CS-Fixer / 17328452173

29 Aug 2025 03:54PM UTC coverage: 94.693% (+0.01%) from 94.683%
17328452173

push

github

keradus
Merge branch 'test' of github.com:keradus/PHP-CS-Fixer into test

19 of 32 new or added lines in 6 files covered. (59.38%)

33 existing lines in 4 files now uncovered.

28318 of 29905 relevant lines covered (94.69%)

45.61 hits per line

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

93.22
/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
        try {
72
            return json_encode(
3✔
73
                [
3✔
74
                    'php' => $this->getSignature()->getPhpVersion(),
3✔
75
                    'version' => $this->getSignature()->getFixerVersion(),
3✔
76
                    'indent' => $this->getSignature()->getIndent(),
3✔
77
                    'lineEnding' => $this->getSignature()->getLineEnding(),
3✔
78
                    'rules' => $this->getSignature()->getRules(),
3✔
79
                    'hashes' => $this->hashes,
3✔
80
                ],
3✔
81
                \JSON_THROW_ON_ERROR
3✔
82
            );
3✔
83
        } catch (\JsonException $e) {
1✔
84
            throw new \UnexpectedValueException(\sprintf(
1✔
85
                '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✔
86
                $e->getMessage()
1✔
87
            ));
1✔
88
        }
89
    }
90

91
    /**
92
     * @throws \InvalidArgumentException
93
     */
94
    public static function fromJson(string $json): self
95
    {
96
        try {
97
            $data = json_decode($json, true, 512, \JSON_THROW_ON_ERROR);
7✔
98
        } catch (\JsonException $e) {
1✔
99
            throw new \InvalidArgumentException(\sprintf(
1✔
100
                'Value needs to be a valid JSON string, got "%s", error: "%s".',
1✔
101
                $json,
1✔
102
                $e->getMessage()
1✔
103
            ));
1✔
104
        }
105

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

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

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

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

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

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

138
        return $cache;
2✔
139
    }
140

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

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

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