Completed
Pull Request — master (#80)
by Aydin
01:51
created

ResourceInputStream   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 54.84 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 17
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 17 17 6
A read() 0 5 1

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
3
namespace PhpSchool\CliMenu\IO;
4
5
use function is_resource;
6
use function get_resource_type;
7
use function stream_get_meta_data;
8
use function strpos;
9
10
/**
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class ResourceInputStream implements InputStream
14
{
15
    /**
16
     * @var resource
17
     */
18
    private $stream;
19
20 View Code Duplication
    public function __construct($stream = null)
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...
21
    {
22
        if ($stream === null) {
23
            $stream = STDIN;
24
        }
25
26
        if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
27
            throw new \InvalidArgumentException('Expected a valid stream');
28
        }
29
30
        $meta = stream_get_meta_data($stream);
31
        if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
32
            throw new \InvalidArgumentException('Expected a readable stream');
33
        }
34
35
        $this->stream = $stream;
36
    }
37
38
    public function read(int $numBytes, callable $callback) : void
39
    {
40
        $buffer = fread($this->stream, $numBytes);
41
        $callback($buffer);
42
    }
43
}
44