Conditions | 7 |
Paths | 5 |
Total Lines | 26 |
Code Lines | 15 |
Lines | 3 |
Ratio | 11.54 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
54 | public function write($data) |
||
55 | { |
||
56 | $stream = fopen($this->filepath, $this->mode); |
||
57 | |||
58 | if ($this->bufferSize !== null && stream_set_write_buffer($stream, $this->bufferSize) !== 0) { |
||
59 | throw new IOException("Failed to change the buffer size."); |
||
60 | } |
||
61 | |||
62 | if (!is_resource($stream) || $stream === false) { |
||
63 | throw new IOException("Unable open " . $this->filepath); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | if (!flock($stream, LOCK_EX | LOCK_NB)) { |
|
|
|||
67 | throw new IOException("Cannot lock file: " . $this->filepath); |
||
68 | } |
||
69 | |||
70 | if (fwrite($stream, $data) === false) { |
||
71 | flock($stream, LOCK_UN); |
||
72 | fclose($stream); |
||
73 | throw new IOException("Failed to write stream."); |
||
74 | } |
||
75 | |||
76 | fflush($stream); |
||
77 | flock($stream, LOCK_UN); |
||
78 | fclose($stream); |
||
79 | } |
||
80 | } |
||
81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.