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.

FileHandler::truncate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * This file is part of the php-vfs package.
4
 *
5
 * (c) Michael Donat <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace VirtualFileSystem\Wrapper;
12
13
use VirtualFileSystem\Structure\File;
14
15
/**
16
 * User as file handle by streamWrapper implementation.
17
 *
18
 * This class is responsible mainly for managing the pointer position during reading and writing.
19
 *
20
 * @author Michael Donat <[email protected]>
21
 * @package php-vfs
22
 */
23
class FileHandler
24
{
25
    const READ_MODE     = 1;
26
    const WRITE_MODE    = 2;
27
28
    protected $position = 0;
29
30
    protected $mode = 0;
31
32
    /**
33
     * @var File
34
     */
35
    protected $file;
36
37
    /**
38
     * Sets file in context.
39
     *
40
     * @param File $file
41
     */
42
    public function setFile(File $file)
43
    {
44
        $this->file = $file;
45
    }
46
47
    /**
48
     * Returns file in context.
49
     *
50
     * @return File
51
     */
52
    public function getFile() {
53
        return $this->file;
54
    }
55
56
    /**
57
     * Writes data to file. Will return the number of bytes written. Will advance pointer by number of bytes written.
58
     *
59
     * @param string $data
60
     *
61
     * @return int
62
     */
63
    public function write($data)
64
    {
65
        $content = $this->file->data();
66
        $content = substr($content, 0, $this->position());
67
        $content .= $data;
68
        $this->file->setData($content);
69
        $written = strlen($data);
70
        $this->offsetPosition($written);
71
        $this->file->setModificationTime(time());
72
        $this->file->setChangeTime(time());
73
74
        return $written;
75
    }
76
77
    /**
78
     * Will read and return $bytes bytes from file. Will advance pointer by $bytes bytes.
79
     *
80
     * @param int $bytes
81
     *
82
     * @return string
83
     */
84
    public function read($bytes)
85
    {
86
        $content = $this->file->data();
87
88
        $return = substr($content, $this->position(), $bytes);
89
90
        $newPosition = $this->position()+$bytes;
91
92
        $newPosition = $newPosition > strlen($content) ? strlen($content) : $newPosition;
93
94
        $this->position($newPosition);
95
        $this->file->setAccessTime(time());
96
97
        return $return;
98
    }
99
100
    /**
101
     * Returns current pointer position.
102
     *
103
     * @param integer|null $position
104
     *
105
     * @return int
106
     */
107
    public function position($position = null)
108
    {
109
        return is_null($position) ? $this->position : $this->position = $position;
110
    }
111
112
    /**
113
     * Moves pointer to the end of file (for append modes).
114
     *
115
     * @return int
116
     */
117
    public function seekToEnd()
118
    {
119
        return $this->position(strlen($this->file->data()));
120
    }
121
122
    /**
123
     * Offsets position by $offset
124
     *
125
     * @param int $offset
126
     */
127
    public function offsetPosition($offset)
128
    {
129
        $this->position += $offset;
130
    }
131
132
    /**
133
     * Tells whether pointer is at the end of file.
134
     *
135
     * @return bool
136
     */
137
    public function atEof()
138
    {
139
        return $this->position() >= strlen($this->file->data());
140
    }
141
142
    /**
143
     * Removed all data from file and sets pointer to 0
144
     *
145
     * @param int $newSize
146
     *
147
     * @return void
148
     */
149
    public function truncate($newSize = 0)
150
    {
151
        $this->position(0);
152
        $newData = substr($this->file->data(), 0, $newSize);
153
        $newData = false === $newData ? '' : $newData;
154
        $this->file->setData($newData);
155
        $this->file->setModificationTime(time());
156
        $this->file->setChangeTime(time());
157
158
        return;
159
    }
160
161
    /**
162
     * Sets handler to read only
163
     */
164
    public function setReadOnlyMode()
165
    {
166
        $this->mode = self::READ_MODE;
167
    }
168
169
    /**
170
     * Sets handler into read/write mode
171
     */
172
    public function setReadWriteMode()
173
    {
174
        $this->mode = self::READ_MODE | self::WRITE_MODE;
175
    }
176
177
    public function setWriteOnlyMode()
178
    {
179
        $this->mode = self::WRITE_MODE;
180
    }
181
182
    /**
183
     * Checks if pointer allows writing
184
     *
185
     * @return bool
186
     */
187
    public function isOpenedForWriting()
188
    {
189
        return (bool) ($this->mode & self::WRITE_MODE);
190
    }
191
192
    /**
193
     * Checks if pointer allows reading
194
     *
195
     * @return bool
196
     */
197
    public function isOpenedForReading()
198
    {
199
        return (bool) ($this->mode & self::READ_MODE);
200
    }
201
202
    /**
203
     * @param  $resource
204
     */
205
    public function lock($resource, $operation)
206
    {
207
        return $this->file->lock($resource, $operation);
208
    }
209
}
210