GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#79)
by Ian
01:09
created

Filesystem::getNamesWithDifferentExtension()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Snapshots;
4
5
class Filesystem
6
{
7
    private string $basePath;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
8
9
    public function __construct(string $basePath)
10
    {
11
        $this->basePath = $basePath;
12
    }
13
14
    public static function inDirectory(string $path): self
15
    {
16
        return new self($path);
17
    }
18
19
    public function path(string $filename): string
20
    {
21
        return $this->basePath.DIRECTORY_SEPARATOR.$filename;
22
    }
23
24
    public function has(string $filename): bool
25
    {
26
        return file_exists($this->path($filename));
27
    }
28
29
    /**
30
     * Get all file names in this directory that have the same name
31
     * as $fileName, but have a different file extension.
32
     *
33
     * @param string $fileName
34
     *
35
     * @return array
36
     */
37
    public function getNamesWithDifferentExtension(string $fileName)
38
    {
39
        if (! file_exists($this->basePath)) {
40
            return [];
41
        }
42
43
        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
44
45
        $baseName = substr($fileName, 0, strlen($fileName) - strlen($extension) - 1);
46
47
        $allNames = scandir($this->basePath);
48
49
        $namesWithDifferentExtension = array_filter($allNames, function ($existingName) use ($baseName, $extension) {
50
            $existingExtension = pathinfo($existingName, PATHINFO_EXTENSION);
51
52
            $existingBaseName = substr($existingName, 0, strlen($existingName) - strlen($existingExtension) - 1);
53
54
            return $existingBaseName === $baseName && $existingExtension !== $extension;
55
        });
56
57
        return array_values($namesWithDifferentExtension);
58
    }
59
60
    public function read(string $filename): string
61
    {
62
        return file_get_contents($this->path($filename));
63
    }
64
65
    public function put(string $filename, string $contents)
66
    {
67
        if (! file_exists($this->basePath)) {
68
            mkdir($this->basePath, 0777, true);
69
        }
70
71
        file_put_contents($this->path($filename), $contents);
72
    }
73
74
    public function delete(string $fileName)
75
    {
76
        return unlink($this->path($fileName));
77
    }
78
79
    public function copy(string $filePath, string $fileName)
80
    {
81
        if (! file_exists($this->basePath)) {
82
            mkdir($this->basePath, 0777, true);
83
        }
84
85
        copy($filePath, $this->path($fileName));
86
    }
87
88
    public function fileEquals(string $filePath, string $fileName)
89
    {
90
        return sha1_file($filePath) === sha1_file($this->path($fileName));
91
    }
92
}
93