Completed
Push — master ( 5a0a25...383c62 )
by Matteo
02:38
created

Document   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 111
Duplicated Lines 12.61 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 13.03%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 0
dl 14
loc 111
rs 10
ccs 6
cts 46
cp 0.1303

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A getId() 0 4 1
A set() 0 4 1
B get() 7 18 5
B has() 7 18 5
A __set() 0 4 1
A __get() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 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
namespace Mattbit\Flat\Document;
4
5
class Document implements Matchable, Identifiable, Encodable
6
{
7
    protected $attributes;
8
9
    /**
10
     * Construct a document.
11
     *
12
     * @param array $attributes
13
     */
14 11
    public function __construct(array $attributes = [])
15
    {
16 11
        $this->attributes = $attributes;
17 11
    }
18
19
    public function toArray()
20
    {
21
        return $this->attributes;
22
    }
23
24
    public function getId()
25
    {
26
        return $this->get('_id');
27
    }
28
29
    public function set($name, $value)
30
    {
31
        $this->attributes[$name] = $value;
32
    }
33
34
    /**
35
     * Get a document attribute using dot notation.
36
     *
37
     * @param $name
38
     *
39
     * @return mixed
40
     */
41 7
    public function get($name)
42
    {
43 7
        if (array_key_exists($name, $this->attributes)) {
44 7
            return $this->attributes[$name];
45
        }
46
47
        $attributes = $this->attributes;
48
49 View Code Duplication
        foreach (explode('.', $name) as $key) {
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...
50
            if (array_key_exists($key, $attributes) && is_array($attributes[$key])) {
51
                $attributes = $attributes[$key];
52
            } else {
53
                return;
54
            }
55
        }
56
57
        return $attributes;
58
    }
59
60
    /**
61
     * Check if the document as a given attribute.
62
     *
63
     * @param  $name
64
     *
65
     * @return bool
66
     */
67
    public function has($name)
68
    {
69
        if (array_key_exists($name, $this->attributes)) {
70
            return true;
71
        }
72
73
        $attributes = $this->attributes;
74
75 View Code Duplication
        foreach (explode('.', $name) as $key) {
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...
76
            if (array_key_exists($key, $attributes) && is_array($attributes[$key])) {
77
                $attributes = $attributes[$key];
78
            } else {
79
                return false;
80
            }
81
        }
82
83
        return true;
84
    }
85
86
    public function __set($name, $value)
87
    {
88
        return $this->set($name, $value);
89
    }
90
91
    public function __get($name)
92
    {
93
        return $this->get($name);
94
    }
95
96
    public function offsetExists($offset)
97
    {
98
        return array_key_exists($offset, $this->attributes);
99
    }
100
101
    public function offsetGet($offset)
102
    {
103
        return $this->attributes[$offset];
104
    }
105
106
    public function offsetSet($offset, $value)
107
    {
108
        $this->attributes[$offset] = $value;
109
    }
110
111
    public function offsetUnset($offset)
112
    {
113
        unset($this->attributes[$offset]);
114
    }
115
}
116