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

CharsetFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 0
dl 16
loc 66
ccs 0
cts 11
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A encode() 8 8 2
A decode() 8 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\Filter;
9
10
use GravityMedia\Metadata\ID3v2\Encoding;
11
12
/**
13
 * Charset filter class.
14
 *
15
 * @package GravityMedia\Metadata\ID3v2\Filter
16
 */
17
class CharsetFilter
18
{
19
    /**
20
     * String representations of encodings.
21
     *
22
     * @var string[]
23
     */
24
    protected static $encodings = [
25
        Encoding::ISO_8859_1 => 'ISO-8859-1',
26
        Encoding::UTF_8 => 'UTF-8',
27
        Encoding::UTF_16 => 'UTF-16',
28
        Encoding::UTF_16BE => 'UTF-16BE',
29
        Encoding::UTF_16LE => 'UTF-16LE'
30
    ];
31
32
    /**
33
     * The internal encoding.
34
     *
35
     * @var int
36
     */
37
    private $encoding;
38
39
    /**
40
     * Create charset filter object.
41
     *
42
     * @param int $encoding
43
     */
44
    public function __construct($encoding = Encoding::UTF_8)
45
    {
46
        $this->encoding = $encoding;
47
    }
48
49
    /**
50
     * Encode string.
51
     *
52
     * @param string $string
53
     * @param int    $encoding
54
     *
55
     * @return string
56
     */
57 View Code Duplication
    public function encode($string, $encoding = Encoding::UTF_8)
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...
58
    {
59
        if ($encoding === $this->encoding) {
60
            return $string;
61
        }
62
63
        return iconv(static::$encodings[$this->encoding], static::$encodings[$encoding], $string);
64
    }
65
66
    /**
67
     * Decode string.
68
     *
69
     * @param string $string
70
     * @param int    $encoding
71
     *
72
     * @return string
73
     */
74 View Code Duplication
    public function decode($string, $encoding = Encoding::UTF_8)
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...
75
    {
76
        if ($encoding === $this->encoding) {
77
            return $string;
78
        }
79
80
        return iconv(static::$encodings[$encoding], static::$encodings[$this->encoding], $string);
81
    }
82
}
83