Completed
Push — feature/0.7.0 ( c0c2ef...83eb72 )
by Ryuichi
02:50
created

FileInputStream::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 3
Ratio 17.65 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 17
rs 9.2
cc 4
eloc 11
nc 5
nop 1
1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\Exception\Extend\InvalidArgumentException;
5
use WebStream\Exception\Extend\IOException;
6
7
/**
8
 * FileInputStream
9
 * @author Ryuichi TANAKA.
10
 * @since 2016/02/05
11
 * @version 0.7
12
 */
13
class FileInputStream extends InputStream
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)
27
    {
28
        if ($file instanceof File) {
29
            $this->file = $file;
30
        } else if (is_string($file)) {
31
            $this->file = new File($file);
0 ignored issues
show
Documentation introduced by
$file is of type string, but the function expects a object<WebStream\IO\stirng>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
        } else {
33
            throw new InvalidArgumentException("Unable to open file: " . $file);
34
        }
35
36
        $stream = @fopen($this->file->getAbsoluteFilePath());
37 View Code Duplication
        if ($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...
38
            throw new IOException("Unable open " . $this->file->getAbsoluteFilePath() . " cause by " . $php_errormsg);
39
        }
40
41
        parent::__construct($stream);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function isMarkSupported()
48
    {
49
        return true;
50
    }
51
}
52