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

LanguageTextFrameReader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 6.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A readEncoding() 0 6 1
A getEncoding() 0 8 2
A readLanguage() 0 11 3
A getLanguage() 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 language text frame reader class.
16
 *
17
 * @package GravityMedia\Metadata\ID3v2\Reader
18
 */
19
class LanguageTextFrameReader 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 $language;
35
36
    /**
37
     * @var string[]
38
     */
39
    private $text;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function __construct(Stream $stream)
45
    {
46
        parent::__construct($stream);
47
48
        $this->charsetFilter = new CharsetFilter();
49
    }
50
51
    /**
52
     * Read encoding.
53
     *
54
     * @return int
55
     */
56
    protected function readEncoding()
57
    {
58
        $this->getStream()->seek($this->getOffset());
59
60
        return $this->getStream()->readUInt8();
61
    }
62
63
    /**
64
     * Get encoding.
65
     *
66
     * @return int
67
     */
68
    public function getEncoding()
69
    {
70
        if (null === $this->encoding) {
71
            $this->encoding = $this->readEncoding();
72
        }
73
74
        return $this->encoding;
75
    }
76
77
    /**
78
     * Read language.
79
     *
80
     * @return string
81
     */
82
    public function readLanguage()
83
    {
84
        $this->getStream()->seek($this->getOffset() + 1);
85
        $language = strtoupper(trim($this->getStream()->read(3), "\x00"));
86
87
        if ('XXX' === $language || strlen($language) !== 3) {
88
            $language = 'UND';
89
        }
90
91
        return $language;
92
    }
93
94
    /**
95
     * Get language.
96
     *
97
     * @return string
98
     */
99
    public function getLanguage()
100
    {
101
        if (null === $this->language) {
102
            $this->language = $this->readLanguage();
103
        }
104
105
        return $this->language;
106
    }
107
108
    /**
109
     * Read text.
110
     *
111
     * @return string[]
112
     */
113 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...
114
    {
115
        $this->getStream()->seek($this->getOffset() + 4);
116
        $text = $this->getStream()->read($this->getStream()->getSize() - 4);
117
118
        return explode("\x00", $this->charsetFilter->decode($text, $this->getEncoding()));
119
    }
120
121
    /**
122
     * Get text.
123
     *
124
     * @return string[]
125
     */
126
    public function getText()
127
    {
128
        if (null === $this->text) {
129
            $this->text = $this->readText();
130
        }
131
132
        return $this->text;
133
    }
134
}
135