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

PHP-CS-Fixer / PHP-CS-Fixer / 3721300657

pending completion
3721300657

push

github

GitHub
minor: Follow PSR12 ordered imports in Symfony ruleset (#6712)

9 of 9 new or added lines in 2 files covered. (100.0%)

22674 of 24281 relevant lines covered (93.38%)

39.08 hits per line

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

68.18
/src/Cache/FileHandler.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 Symfony\Component\Filesystem\Exception\IOException;
18

19
/**
20
 * @author Andreas Möller <am@localheinz.com>
21
 *
22
 * @internal
23
 */
24
final class FileHandler implements FileHandlerInterface
25
{
26
    private string $file;
27

28
    public function __construct(string $file)
29
    {
30
        $this->file = $file;
9✔
31
    }
32

33
    public function getFile(): string
34
    {
35
        return $this->file;
1✔
36
    }
37

38
    public function read(): ?CacheInterface
39
    {
40
        if (!file_exists($this->file)) {
3✔
41
            return null;
1✔
42
        }
43

44
        $content = file_get_contents($this->file);
2✔
45

46
        try {
47
            $cache = Cache::fromJson($content);
2✔
48
        } catch (\InvalidArgumentException $exception) {
1✔
49
            return null;
1✔
50
        }
51

52
        return $cache;
1✔
53
    }
54

55
    public function write(CacheInterface $cache): void
56
    {
57
        $content = $cache->toJson();
4✔
58

59
        if (file_exists($this->file)) {
4✔
60
            if (is_dir($this->file)) {
1✔
61
                throw new IOException(
1✔
62
                    sprintf('Cannot write cache file "%s" as the location exists as directory.', realpath($this->file)),
1✔
63
                    0,
1✔
64
                    null,
1✔
65
                    $this->file
1✔
66
                );
1✔
67
            }
68

69
            if (!is_writable($this->file)) {
×
70
                throw new IOException(
×
71
                    sprintf('Cannot write to file "%s" as it is not writable.', realpath($this->file)),
×
72
                    0,
×
73
                    null,
×
74
                    $this->file
×
75
                );
×
76
            }
77
        } else {
78
            $dir = \dirname($this->file);
3✔
79

80
            if (!is_dir($dir)) {
3✔
81
                throw new IOException(
1✔
82
                    sprintf('Directory of cache file "%s" does not exists.', $this->file),
1✔
83
                    0,
1✔
84
                    null,
1✔
85
                    $this->file
1✔
86
                );
1✔
87
            }
88

89
            @touch($this->file);
2✔
90
            @chmod($this->file, 0666);
2✔
91
        }
92

93
        $bytesWritten = @file_put_contents($this->file, $content);
2✔
94

95
        if (false === $bytesWritten) {
2✔
96
            $error = error_get_last();
×
97

98
            throw new IOException(
×
99
                sprintf('Failed to write file "%s", "%s".', $this->file, $error['message'] ?? 'no reason available'),
×
100
                0,
×
101
                null,
×
102
                $this->file
×
103
            );
×
104
        }
105
    }
106
}
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

© 2025 Coveralls, Inc