ExtendedFileStream::stream_write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Io;
22
23
use Phing\Exception\BuildException;
24
25
/**
26
 * Extended file stream wrapper class which auto-creates directories.
27
 *
28
 * @author  Michiel Rook <[email protected]>
29
 * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
30
 */
31
class ExtendedFileStream
32
{
33
    private $fp;
34
35
    public static function registerStream()
36
    {
37
        if (!in_array('efile', stream_get_wrappers())) {
38
            stream_wrapper_register('efile', __CLASS__);
39
        }
40
    }
41
42
    public static function unregisterStream()
43
    {
44
        stream_wrapper_unregister('efile');
45
    }
46
47
    // @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
48
49
    /**
50
     * @param $path
51
     * @param $mode
52
     * @param $options
53
     * @param $opened_path
54
     *
55
     * @throws IOException
56
     *
57
     * @return bool
58
     */
59
    public function stream_open($path, $mode, $options, &$opened_path)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

59
    public function stream_open($path, $mode, /** @scrutinizer ignore-unused */ $options, &$opened_path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $opened_path is not used and could be removed. ( Ignorable by Annotation )

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

59
    public function stream_open($path, $mode, $options, /** @scrutinizer ignore-unused */ &$opened_path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        // if we're on Windows, urldecode() the path again
62
        if ('\\' == FileSystem::getFileSystem()->getSeparator()) {
63
            $path = urldecode($path);
64
        }
65
66
        $filepath = substr($path, 8);
67
68
        $this->createDirectories(dirname($filepath));
69
70
        $this->fp = fopen($filepath, $mode);
71
72
        if (!$this->fp) {
73
            throw new BuildException("Unable to open stream for path {$path}");
74
        }
75
76
        return true;
77
    }
78
79
    public function stream_close()
80
    {
81
        fclose($this->fp);
82
        $this->fp = null;
83
    }
84
85
    /**
86
     * @param $count
87
     *
88
     * @return string
89
     */
90
    public function stream_read($count)
91
    {
92
        return fread($this->fp, $count);
93
    }
94
95
    /**
96
     * @param $data
97
     *
98
     * @return int
99
     */
100
    public function stream_write($data)
101
    {
102
        return fwrite($this->fp, $data);
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function stream_eof()
109
    {
110
        return feof($this->fp);
111
    }
112
113
    /**
114
     * @return int
115
     */
116
    public function stream_tell()
117
    {
118
        return ftell($this->fp);
119
    }
120
121
    /**
122
     * @param $offset
123
     * @param $whence
124
     *
125
     * @return int
126
     */
127
    public function stream_seek($offset, $whence)
128
    {
129
        return fseek($this->fp, $offset, $whence);
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function stream_flush()
136
    {
137
        return fflush($this->fp);
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function stream_stat()
144
    {
145
        return fstat($this->fp);
146
    }
147
148
    // @phpcs:enable
149
150
    /**
151
     * @param  $path
152
     *
153
     * @return bool
154
     */
155
    public function unlink($path)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

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

155
    public function unlink(/** @scrutinizer ignore-unused */ $path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        return false;
158
    }
159
160
    /**
161
     * @param  $path_from
162
     * @param  $path_to
163
     *
164
     * @return bool
165
     */
166
    public function rename($path_from, $path_to)
0 ignored issues
show
Unused Code introduced by
The parameter $path_from is not used and could be removed. ( Ignorable by Annotation )

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

166
    public function rename(/** @scrutinizer ignore-unused */ $path_from, $path_to)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path_to is not used and could be removed. ( Ignorable by Annotation )

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

166
    public function rename($path_from, /** @scrutinizer ignore-unused */ $path_to)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        return false;
169
    }
170
171
    /**
172
     * @param  $path
173
     * @param  $mode
174
     * @param  $options
175
     *
176
     * @return bool
177
     */
178
    public function mkdir($path, $mode, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

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

178
    public function mkdir(/** @scrutinizer ignore-unused */ $path, $mode, $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $mode is not used and could be removed. ( Ignorable by Annotation )

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

178
    public function mkdir($path, /** @scrutinizer ignore-unused */ $mode, $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

178
    public function mkdir($path, $mode, /** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        return false;
181
    }
182
183
    /**
184
     * @param  $path
185
     * @param  $options
186
     *
187
     * @return bool
188
     */
189
    public function rmdir($path, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

189
    public function rmdir($path, /** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

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

189
    public function rmdir(/** @scrutinizer ignore-unused */ $path, $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
    {
191
        return false;
192
    }
193
194
    /**
195
     * @param $path
196
     */
197
    private function createDirectories($path)
198
    {
199
        $f = new File($path);
200
        if (!$f->exists()) {
201
            $f->mkdirs();
202
        }
203
    }
204
}
205