Completed
Push — feature/0.7.0 ( 2e2153...887737 )
by Ryuichi
03:23
created

FileOutputStream   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 99
Duplicated Lines 3.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 23
c 2
b 0
f 0
lcom 1
cbo 4
dl 3
loc 99
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 3 33 9
B write() 0 17 8
A close() 0 17 4
A flush() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\IO\OutputStream;
5
use WebStream\Exception\Extend\InvalidArgumentException;
6
use WebStream\Exception\Extend\IOException;
7
8
/**
9
 * FileOutputStream
10
 * @author Ryuichi TANAKA.
11
 * @since 2016/02/24
12
 * @version 0.7
13
 */
14
class FileOutputStream extends OutputStream
15
{
16
    /**
17
     * @var File ファイルオブジェクト
18
     */
19
    protected $file;
20
21
    /**
22
     * constructor
23
     * @param mixed $file ファイルオブジェクトまたはファイルパス
24
     * @throws InvalidArgumentException
25
     * @throws IOException
26
     */
27
    public function __construct($file, bool $isAppend = false)
28
    {
29
        $filepath = null;
30
        if ($file instanceof File) {
31
            $this->file = $file;
32
            $filepath = $this->file->getFilePath();
33
        } elseif (is_string($file)) {
34
            if (!file_exists($file)) {
35
                $dirname = dirname($filepath);
36
                $dir = new File($dirname);
37
                if (!$dir->isWritable()) {
38
                    throw new IOException("Cannot writable: " . $filepath);
39
                }
40
            }
41
            $this->file = new File($file);
42
            $filepath = $this->file->getFilePath();
43
        } else {
44
            throw new InvalidArgumentException("Invalid argument type: " . $file);
45
        }
46
47
        $mode = $isAppend ? 'ab' : 'wb';
48
        $stream = fopen($filepath, $mode);
49
50
        if (!is_resource($stream) || $stream === false) {
51
            throw new IOException("Unable open " . $this->file->getFilePath());
52
        }
53
54 View Code Duplication
        if (!flock($stream, LOCK_EX | LOCK_NB)) {
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...
55
            throw new IOException("Cannot lock file: " . $this->file->getFilePath());
56
        }
57
58
        parent::__construct($stream);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function write($buf, int $off = null, int $len = null)
65
    {
66
        $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...
67
        if ($off === null && $len === null) {
68
            $data = $buf;
69
        } elseif ($off !== null && $len === null) {
70
            $data = substr($buf, $off);
71
        } elseif ($off === null && $len !== null) {
72
            $data = substr($buf, 0, $len);
73
        } else {
74
            $data = substr($buf, $off, $len);
75
        }
76
77
        if (@fwrite($this->stream, $data) === false) {
78
            throw new IOException("Failed to write stream.");
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function close()
86
    {
87
        if ($this->stream === null) {
88
            return;
89
        }
90
91
        $this->flush();
92
93
        // PHP5.3.2以降はfcloseではロック解放されなくなり、明示的に開放する必要がある
94
        flock($this->stream, LOCK_UN);
95
96
        if (get_resource_type($this->stream) !== 'Unknown' && fclose($this->stream) === false) {
97
            throw new IOException("Cannot close output stream.");
98
        }
99
100
        $this->stream = null;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function flush()
107
    {
108
        if (@fflush($this->stream) === false) {
109
            throw new IOException("Failed to flush.");
110
        }
111
    }
112
}
113