Completed
Push — id3-metadata-objects ( 491068...1cf97b )
by Daniel
09:08
created

TextFrameReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 8.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 3
dl 7
loc 80
ccs 0
cts 21
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A readEncoding() 0 6 1
A getEncoding() 0 8 2
A readText() 7 7 1
A getText() 0 8 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
/**
3
 * This file is part of the Metadata package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2\Reader;
9
10
use GravityMedia\Metadata\ID3v2\Filter\CharsetFilter;
11
use GravityMedia\Metadata\ID3v2\StreamContainer;
12
use GravityMedia\Stream\Stream;
13
14
/**
15
 * ID3v2 text frame reader class.
16
 *
17
 * @package GravityMedia\Metadata\ID3v2\Reader
18
 */
19
class TextFrameReader extends StreamContainer
20
{
21
    /**
22
     * @var CharsetFilter
23
     */
24
    private $charsetFilter;
25
26
    /**
27
     * @var int
28
     */
29
    private $encoding;
30
31
    /**
32
     * @var string[]
33
     */
34
    private $text;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __construct(Stream $stream)
40
    {
41
        parent::__construct($stream);
42
43
        $this->charsetFilter = new CharsetFilter();
44
    }
45
46
    /**
47
     * Read encoding.
48
     *
49
     * @return int
50
     */
51
    protected function readEncoding()
52
    {
53
        $this->getStream()->seek($this->getOffset());
54
55
        return $this->getStream()->readUInt8();
56
    }
57
58
    /**
59
     * Get encoding.
60
     *
61
     * @return int
62
     */
63
    public function getEncoding()
64
    {
65
        if (null === $this->encoding) {
66
            $this->encoding = $this->readEncoding();
67
        }
68
69
        return $this->encoding;
70
    }
71
72
    /**
73
     * Read text.
74
     *
75
     * @return string[]
76
     */
77 View Code Duplication
    protected function readText()
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...
78
    {
79
        $this->getStream()->seek($this->getOffset() + 1);
80
        $text = $this->getStream()->read($this->getStream()->getSize() - 1);
81
82
        return explode("\x00", $this->charsetFilter->decode($text, $this->getEncoding()));
83
    }
84
85
    /**
86
     * Get text.
87
     *
88
     * @return string[]
89
     */
90
    public function getText()
91
    {
92
        if (null === $this->text) {
93
            $this->text = $this->readText();
94
        }
95
96
        return $this->text;
97
    }
98
}
99