Completed
Push — id3-metadata-objects ( 33018f...0cc3db )
by Daniel
07:46
created

LanguageTextFrameReader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 11.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 12
c 2
b 0
f 2
lcom 1
cbo 2
dl 12
loc 106
ccs 0
cts 32
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A readEncoding() 0 6 1
A getEncoding() 0 8 2
A readLanguage() 0 11 3
A getLanguage() 0 8 2
A readText() 12 12 2
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 project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2\Reader;
9
10
use GravityMedia\Metadata\ID3v2\StreamContainer;
11
12
/**
13
 * ID3v2 language text frame reader class.
14
 *
15
 * @package GravityMedia\Metadata\ID3v2\Reader
16
 */
17
class LanguageTextFrameReader extends StreamContainer
18
{
19
    /**
20
     * @var int
21
     */
22
    private $encoding;
23
24
    /**
25
     * @var string
26
     */
27
    private $language;
28
29
    /**
30
     * @var string
31
     */
32
    private $text;
33
34
    /**
35
     * Read encoding.
36
     *
37
     * @return int
38
     */
39
    protected function readEncoding()
40
    {
41
        $this->getStream()->seek($this->getOffset());
42
43
        return $this->getStream()->readUInt8();
44
    }
45
46
    /**
47
     * Get encoding.
48
     *
49
     * @return int
50
     */
51
    public function getEncoding()
52
    {
53
        if (null === $this->encoding) {
54
            $this->encoding = $this->readEncoding();
55
        }
56
57
        return $this->encoding;
58
    }
59
60
    /**
61
     * Read language.
62
     *
63
     * @return string
64
     */
65
    public function readLanguage()
66
    {
67
        $this->getStream()->seek($this->getOffset() + 1);
68
        $language = strtoupper(trim($this->getStream()->read(3), "\x00"));
69
70
        if ('XXX' === $language || strlen($language) !== 3) {
71
            $language = 'UND';
72
        }
73
74
        return $language;
75
    }
76
77
    /**
78
     * Get language.
79
     *
80
     * @return string
81
     */
82
    public function getLanguage()
83
    {
84
        if (null === $this->language) {
85
            $this->language = $this->readLanguage();
86
        }
87
88
        return $this->language;
89
    }
90
91
    /**
92
     * Read text.
93
     *
94
     * @return string
95
     */
96 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...
97
    {
98
        $offset = 4;
99
        $length = $this->getStream()->getSize() - $offset;
100
        if ($length < 1) {
101
            return '';
102
        }
103
104
        $this->getStream()->seek($this->getOffset() + $offset);
105
106
        return $this->getStream()->read($length);
107
    }
108
109
    /**
110
     * Get text.
111
     *
112
     * @return string
113
     */
114
    public function getText()
115
    {
116
        if (null === $this->text) {
117
            $this->text = $this->readText();
118
        }
119
120
        return $this->text;
121
    }
122
}
123