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 (#35)
by
unknown
02:21
created

Filesystem::getNamesWithDifferentExtension()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Snapshots;
4
5
class Filesystem
6
{
7
    /** @var string */
8
    private $basePath;
9
10
    public function __construct(string $basePath)
11
    {
12
        $this->basePath = $basePath;
13
    }
14
15
    public static function inDirectory(string $path): self
16
    {
17
        return new self($path);
18
    }
19
20
    public function path(string $filename): string
21
    {
22
        return $this->basePath.DIRECTORY_SEPARATOR.$filename;
23
    }
24
25
    public function has(string $filename): bool
26
    {
27
        return file_exists($this->path($filename));
28
    }
29
30
    /**
31
     * Get all file names in this directory that have the same name
32
     * as $fileName, but have a different file extension
33
     * @param string $fileName
34
     * @return array
35
     */
36
    public function getNamesWithDifferentExtension(string $fileName)
37
    {
38
        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
39
40
        $baseName = substr($fileName, 0, strlen($fileName) - strlen($extension) - 1);
41
42
        $allNames = scandir($this->basePath);
43
44
        $namesWithDifferentExtension = array_filter($allNames, function ($existingName) use ($baseName, $extension) {
45
            $existingExtension = pathinfo($existingName, PATHINFO_EXTENSION);
46
47
            $existingBaseName = substr($existingName, 0, strlen($existingName) - strlen($existingExtension) - 1);
48
49
            return $existingBaseName === $baseName && $existingExtension !== $extension;
50
        });
51
52
        return array_values($namesWithDifferentExtension);
53
    }
54
55
    public function read(string $filename): string
56
    {
57
        return file_get_contents($this->path($filename));
58
    }
59
60
    public function put(string $filename, string $contents)
61
    {
62
        if (! file_exists($this->basePath)) {
63
            mkdir($this->basePath);
64
        }
65
66
        file_put_contents($this->path($filename), $contents);
67
    }
68
69
    public function delete(string $fileName)
70
    {
71
        return unlink($this->path($fileName));
72
    }
73
74
    public function copy(string $filePath, string $fileName)
75
    {
76
        if (! file_exists($this->basePath)) {
77
            mkdir($this->basePath);
78
        }
79
80
        copy($filePath, $this->path($fileName));
81
    }
82
83
    public function fileEquals(string $filePath, string $fileName)
84
    {
85
        return sha1_file($filePath) === sha1_file($this->path($fileName));
86
    }
87
}
88