Completed
Push — master ( 04c37a...3d7f4d )
by Tobias
10:27 queued 08:42
created

Metadata   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 149
Duplicated Lines 13.42 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 0
dl 20
loc 149
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getState() 9 9 3
A setState() 0 5 1
A isApproved() 11 11 3
A setApproved() 0 5 2
A getSourceLocations() 0 14 3
A addCategory() 0 4 1
A toArray() 0 7 1
A getAllInCategory() 0 18 4
A removeAllInCategory() 0 8 3

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
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Model;
13
14
/**
15
 * Message metadata abstraction.
16
 *
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
final class Metadata
20
{
21
    /**
22
     * @var array
23
     */
24
    private $metadata;
25
26
    /**
27
     * @var array
28
     */
29
    private $notes = [];
30
31
    /**
32
     * @param array $metadata
33
     */
34
    public function __construct(array $metadata)
35
    {
36
        $this->metadata = $metadata;
37
        if (isset($metadata['notes'])) {
38
            $this->notes = $metadata['notes'];
39
        }
40
    }
41
42
    /**
43
     * @return string|null
44
     */
45 View Code Duplication
    public function getState()
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...
46
    {
47
        $notes = $this->getAllInCategory('state');
48
        foreach ($notes as $note) {
49
            if (isset($note['content'])) {
50
                return $note['content'];
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param string $state
57
     */
58
    public function setState($state)
59
    {
60
        $this->removeAllInCategory('state');
61
        $this->addCategory('state', $state);
62
    }
63
64
    /**
65
     * @return bool
66
     */
67 View Code Duplication
    public function isApproved()
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...
68
    {
69
        $notes = $this->getAllInCategory('approved');
70
        foreach ($notes as $note) {
71
            if (isset($note['content'])) {
72
                return $note['content'] === 'true';
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * @param bool $bool
81
     */
82
    public function setApproved($bool)
83
    {
84
        $this->removeAllInCategory('approved');
85
        $this->addCategory('approved', $bool ? 'true' : 'false');
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getSourceLocations()
92
    {
93
        $sources = [];
94
        $notes = $this->getAllInCategory('file-source');
95
        foreach ($notes as $note) {
96
            if (!isset($note['content'])) {
97
                continue;
98
            }
99
            list($path, $line) = explode(':', $note['content'], 2);
100
            $sources[] = ['path' => $path, 'line' => $line];
101
        }
102
103
        return $sources;
104
    }
105
106
    /**
107
     * Add metadata.
108
     *
109
     * @param string $name
110
     * @param string $content
111
     */
112
    public function addCategory($name, $content, $priority = 1)
113
    {
114
        $this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority];
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function toArray()
121
    {
122
        $metadata = $this->metadata;
123
        $metadata['notes'] = $this->notes;
124
125
        return $metadata;
126
    }
127
128
    /**
129
     * Return all notes for one category. It will also order data according to priority.
130
     *
131
     * @param string $category
132
     *
133
     * @return array
134
     */
135
    public function getAllInCategory($category)
136
    {
137
        $data = [];
138
        foreach ($this->notes as &$note) {
139
            if ($note['category'] === $category) {
140
                if (!isset($note['priority'])) {
141
                    $note['priority'] = '1';
142
                }
143
                $data[] = $note;
144
            }
145
        }
146
147
        usort($data, function (array $a, array $b) {
148
            return (int) $a['priority'] - (int) $b['priority'];
149
        });
150
151
        return $data;
152
    }
153
154
    /**
155
     * Remove all metadata in category.
156
     *
157
     * @param string $category
158
     */
159
    public function removeAllInCategory($category)
160
    {
161
        foreach ($this->notes as $i => $note) {
162
            if ($note['category'] === $category) {
163
                unset($this->notes[$i]);
164
            }
165
        }
166
    }
167
}
168