Completed
Push — id3-metadata-objects ( e37014...ee91c4 )
by Daniel
12:20
created

HeaderReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 16.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 13
loc 79
ccs 0
cts 28
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A readRevision() 0 6 1
A getRevision() 0 8 2
B readFlags() 13 28 3
A readSize() 0 6 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
 * 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\Flag\HeaderFlag;
11
use GravityMedia\Metadata\ID3v2\Version;
12
13
/**
14
 * ID3v2 header reader class.
15
 *
16
 * @package GravityMedia\Metadata\ID3v2\Reader
17
 */
18
class HeaderReader extends AbstractReader
19
{
20
    /**
21
     * @var int
22
     */
23
    private $revision;
24
25
    /**
26
     * Read ID3v2 revision.
27
     *
28
     * @return int
29
     */
30
    protected function readRevision()
31
    {
32
        $this->getStream()->seek($this->getOffset());
33
34
        return $this->getStream()->readUInt8();
35
    }
36
37
    /**
38
     * Get $revision
39
     *
40
     * @return int
41
     */
42
    public function getRevision()
43
    {
44
        if (null === $this->revision) {
45
            $this->revision = $this->readRevision();
46
        }
47
48
        return $this->revision;
49
    }
50
51
    /**
52
     * Read ID3v2 header flags.
53
     *
54
     * @return bool[]
55
     */
56
    protected function readFlags()
57
    {
58
        $this->getStream()->seek($this->getOffset() + 1);
59
60
        $flags = $this->getStream()->readUInt8();
61
62 View Code Duplication
        if (Version::VERSION_22 === $this->getVersion()) {
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...
63
            return [
64
                HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
65
                HeaderFlag::FLAG_COMPRESSION => (bool)($flags & 0x40)
66
            ];
67
        }
68
69 View Code Duplication
        if (Version::VERSION_23 === $this->getVersion()) {
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...
70
            return [
71
                HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
72
                HeaderFlag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40),
73
                HeaderFlag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20)
74
            ];
75
        }
76
77
        return [
78
            HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
79
            HeaderFlag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40),
80
            HeaderFlag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20),
81
            HeaderFlag::FLAG_FOOTER_PRESENT => (bool)($flags & 0x10)
82
        ];
83
    }
84
85
    /**
86
     * Read ID3v2 header size.
87
     *
88
     * @return int
89
     */
90
    protected function readSize()
91
    {
92
        $this->getStream()->seek($this->getOffset() + 2);
93
94
        return $this->getSynchsafeIntegerFilter()->decode($this->getStream()->readUInt32());
95
    }
96
}
97