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.
Passed
Pull Request — master (#214)
by joseph
20:33
created

File::removeIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
6
use SplFileObject;
7
use function dirname;
8
9
class File extends AbstractFilesystemItem
10
{
11
    protected const PATH_TYPE = 'file';
12
13
    /**
14
     * The permissions to be set on newly created files
15
     *
16
     * @var int
17
     */
18
    protected $createMode = 0644;
19
20
    /**
21
     * @var string|null
22
     */
23
    protected $contents;
24
25
    /**
26
     * Stores an instance fo the SplFileObject object for the file.
27
     *
28
     * @var SplFileObject
29
     */
30
    protected $splFileObject;
31
32
    /**
33
     * @var Directory
34
     */
35
    private $directory;
36
37
    public function removeIfExists(): self
38
    {
39
        if ($this->exists()) {
40
            unlink($this->path);
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param string $path
48
     *
49
     * @return $this
50
     */
51 2
    public function setPath(string $path): self
52
    {
53 2
        parent::setPath($path);
54 2
        $this->directory = new Directory(dirname($path));
55
56 2
        return $this;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 2
    public function getContents(): ?string
63
    {
64 2
        return $this->contents;
65
    }
66
67
    /**
68
     * @param string $contents
69
     *
70
     * @return File
71
     */
72
    public function setContents(string $contents): File
73
    {
74
        $this->contents = $contents;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return File
81
     * @throws DoctrineStaticMetaException
82
     */
83 2
    public function putContents(): self
84
    {
85 2
        $this->assertExists();
86
        \ts\file_put_contents($this->path, $this->contents);
0 ignored issues
show
Bug introduced by
It seems like $this->contents can also be of type null; however, parameter $data of ts\file_put_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        \ts\file_put_contents($this->path, /** @scrutinizer ignore-type */ $this->contents);
Loading history...
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return File
93
     * @throws DoctrineStaticMetaException
94
     */
95 2
    public function loadContents(): self
96
    {
97 2
        $this->assertExists();
98
        $this->contents = \ts\file_get_contents($this->path);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Provide an SplFileObject object, asserting that the path exists
105
     *
106
     * @return SplFileObject
107
     * @throws DoctrineStaticMetaException
108
     */
109 2
    public function getSplFileObject(): SplFileObject
110
    {
111 2
        $this->assertExists();
112
        if (null !== $this->splFileObject && $this->path === $this->splFileObject->getRealPath()) {
113
            return $this->splFileObject;
114
        }
115
        $this->splFileObject = new SplFileObject($this->path);
116
117
        return $this->splFileObject;
118
    }
119
120
    /**
121
     * @return Directory
122
     */
123
    public function getDirectory(): Directory
124
    {
125
        return $this->directory;
126
    }
127
128
    /**
129
     * This is the specific creation logic for the concrete filesystem type.
130
     */
131 1
    protected function doCreate(): void
132
    {
133 1
        $this->directory->assertExists();
134
        \ts\file_put_contents($this->path, '');
135
    }
136
137
    protected function isCorrectType(): bool
138
    {
139
        return $this->createSplFileInfo()->isFile();
140
    }
141
}
142