1 | <?php |
||||
2 | |||||
3 | namespace WebStream\IO\Writer; |
||||
4 | |||||
5 | use WebStream\Exception\Extend\IOException; |
||||
0 ignored issues
–
show
|
|||||
6 | use WebStream\IO\File; |
||||
7 | |||||
8 | /** |
||||
9 | * SimpleFileWriter |
||||
10 | * OutputStreamWriterを継承しているが、write以外の機能を無効にする |
||||
11 | * @author Ryuichi TANAKA. |
||||
12 | * @since 2016/02/25 |
||||
13 | * @version 0.7 |
||||
14 | */ |
||||
15 | class SimpleFileWriter extends OutputStreamWriter |
||||
16 | { |
||||
17 | /** |
||||
18 | * @var string ファイルパス |
||||
19 | */ |
||||
20 | private string $filepath; |
||||
21 | |||||
22 | /** |
||||
23 | * @var int バッファリングサイズ |
||||
24 | */ |
||||
25 | private int $bufferSize; |
||||
26 | |||||
27 | /** |
||||
28 | * @var string 書き込みモード |
||||
29 | */ |
||||
30 | private string $mode; |
||||
31 | |||||
32 | /** |
||||
33 | * constructor |
||||
34 | * @param string $filepath ファイルパス |
||||
35 | * @param int $bufferSize バッファリングサイズ |
||||
36 | * @throws IOException |
||||
37 | */ |
||||
38 | 2 | public function __construct(string $filepath, int $bufferSize = 0) |
|||
39 | { |
||||
40 | 2 | $dirname = dirname($filepath); |
|||
41 | 2 | $dir = new File($dirname); |
|||
42 | 2 | if (!$dir->isWritable()) { |
|||
43 | throw new IOException("Cannot writable: " . $filepath); |
||||
44 | } |
||||
45 | |||||
46 | 2 | $this->filepath = $filepath; |
|||
47 | 2 | $this->bufferSize = $bufferSize; |
|||
48 | 2 | $this->mode = file_exists($this->filepath) ? 'ab' : 'wb'; |
|||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * ファイルに書き込む |
||||
53 | * ファイルが存在する場合、常に追記モード |
||||
54 | * @param mixed $data 書き込みデータ |
||||
55 | * @throws IOException |
||||
56 | */ |
||||
57 | 2 | public function write($data) |
|||
58 | { |
||||
59 | 2 | $stream = fopen($this->filepath, $this->mode); |
|||
60 | |||||
61 | 2 | if ($this->bufferSize > 0 && stream_set_write_buffer($stream, $this->bufferSize) !== 0) { |
|||
0 ignored issues
–
show
It seems like
$stream can also be of type false ; however, parameter $stream of stream_set_write_buffer() does only seem to accept resource , 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
![]() |
|||||
62 | throw new IOException("Failed to change the buffer size."); |
||||
63 | } |
||||
64 | |||||
65 | 2 | if (!is_resource($stream) || $stream === false) { |
|||
66 | throw new IOException("Unable open " . $this->filepath); |
||||
67 | } |
||||
68 | |||||
69 | 2 | if (!flock($stream, LOCK_EX | LOCK_NB)) { |
|||
70 | throw new IOException("Cannot lock file: " . $this->filepath); |
||||
71 | } |
||||
72 | |||||
73 | 2 | if (fwrite($stream, $data) === false) { |
|||
74 | flock($stream, LOCK_UN); |
||||
75 | fclose($stream); |
||||
76 | throw new IOException("Failed to write stream."); |
||||
77 | } |
||||
78 | |||||
79 | 2 | fflush($stream); |
|||
80 | 2 | flock($stream, LOCK_UN); |
|||
81 | 2 | fclose($stream); |
|||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * {@inheritdoc} |
||||
86 | */ |
||||
87 | public function close() |
||||
88 | { |
||||
89 | // Nothing to do |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * {@inheritdoc} |
||||
94 | */ |
||||
95 | public function flush() |
||||
96 | { |
||||
97 | // Nothing to do |
||||
98 | } |
||||
99 | |||||
100 | /** |
||||
101 | * {@inheritdoc} |
||||
102 | * @throws IOException |
||||
103 | */ |
||||
104 | public function newLine() |
||||
105 | { |
||||
106 | $this->write(PHP_EOL); |
||||
107 | } |
||||
108 | } |
||||
109 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths