Completed
Push — feature/0.7.0 ( d06a69...2e2153 )
by Ryuichi
04:48
created

FileOutputStream::close()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 0
1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\Exception\Extend\InvalidArgumentException;
5
use WebStream\Exception\Extend\IOException;
6
7
/**
8
 * FileOutputStream
9
 * @author Ryuichi TANAKA.
10
 * @since 2016/02/24
11
 * @version 0.7
12
 */
13
class FileOutputStream extends OutputStream
14
{
15
    /**
16
     * @var File ファイルオブジェクト
17
     */
18
    protected $file;
19
20
    /**
21
     * constructor
22
     * @param mixed $file ファイルオブジェクトまたはファイルパス
23
     * @throws InvalidArgumentException
24
     * @throws IOException
25
     */
26
    public function __construct($file, bool $isAppend = false)
27
    {
28 View Code Duplication
        if ($file instanceof File) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
29
            $this->file = $file;
30
        } elseif (is_string($file)) {
31
            $this->file = new File($file);
32
        } else {
33
            throw new InvalidArgumentException("Invalid argument type: " . $file);
34
        }
35
36
        $mode = $isAppend ? 'ab' : 'wb';
37
        $stream = @fopen($this->file->getAbsoluteFilePath(), $mode);
38
39 View Code Duplication
        if (!is_resource($stream) || $stream === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
40
            throw new IOException("Unable open " . $this->file->getAbsoluteFilePath());
41
        }
42
43
        parent::__construct($stream);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function write($buf, int $off = null, int $len = null)
50
    {
51
        $data = null;
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
        if ($off === null && $len === null) {
53
            $data = $buf;
54
        } elseif ($off !== null && $len === null) {
55
            $data = substr($buf, $off);
56
        } elseif ($off === null && $len !== null) {
57
            $data = substr($buf, 0, $len);
58
        } else {
59
            $data = substr($buf, $off, $len);
60
        }
61
62
        if (@fwrite($this->stream, $data) === false) {
63
            throw new IOException("Failed to write stream.");
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 View Code Duplication
    public function close()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
71
    {
72
        if ($this->stream === null) {
73
            return;
74
        }
75
76
        $this->flush();
77
78
        if (get_resource_type($this->stream) !== 'Unknown' && fclose($this->stream) === false) {
79
            throw new IOException("Cannot close output stream.");
80
        }
81
82
        $this->stream = null;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function flush()
89
    {
90
        if (@fflush($this->stream) === false) {
91
            throw new IOException("Failed to flush.");
92
        }
93
    }
94
}
95