Passed
Push — master ( ddf6f5...2e933f )
by Nicolas
03:42 queued 28s
created

StreamedBinary   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 25.58 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 1
cbo 5
dl 11
loc 43
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A asTransported() 11 19 2
A isChunked() 0 4 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 Puzzle\AMQP\Messages\Bodies;
4
5
use Puzzle\ValueObjects\Uuid;
6
use Puzzle\AMQP\Messages\Chunks\Chunk;
7
use Puzzle\AMQP\Messages\Chunks\ChunkSize;
8
use Puzzle\AMQP\Messages\Chunks\ChunkedMessageMetadata;
9
10
class StreamedBinary extends Binary
11
{
12
    private
13
        $chunkSize,
14
        $metadata;
15
16 2
    public function __construct($content, ChunkSize $chunkSize)
17
    {
18 2
        parent::__construct($content);
19
20 2
        $this->chunkSize = $chunkSize;
21
22 2
        $size = strlen($content);
23 2
        $nbChunks = (int) ceil($size / $chunkSize->toBytes());
24
25 2
        $this->metadata = new ChunkedMessageMetadata(new Uuid(), $size, $nbChunks, sha1($content));
26 2
    }
27
28 3
    public function asTransported()
29
    {
30 3
        $wholeContent = parent::asTransported();
31 3
        $length = strlen($wholeContent);
32 3
        $offset = 0;
33 3
        $playhead = 0;
34
35 3 View Code Duplication
        while($offset < $length)
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...
36
        {
37 3
            $content = substr($wholeContent, $offset, $this->chunkSize->toBytes());
38 3
            $playhead++;
39
40 3
            $chunk = new Chunk($playhead, $offset, $content, $this->metadata);
41 3
            yield $chunk;
42
43 3
            $offset += $chunk->size();
44 3
            unset($chunk, $content);
45 3
        }
46 3
    }
47
48 1
    public function isChunked()
49
    {
50 1
        return true;
51
    }
52
}
53