Completed
Push — master ( d8757f...fa9ad1 )
by Matteo
02:59
created

Document   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 17.28 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 0
dl 14
loc 81
ccs 32
cts 32
cp 1
rs 10

6 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

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 21
    public function __construct(array $attributes = [])
15
    {
16 21
        $this->attributes = $attributes;
17 21
    }
18
19 1
    public function toArray()
20
    {
21 1
        return $this->attributes;
22
    }
23
24 5
    public function getId()
25
    {
26 5
        return $this->get('_id');
27
    }
28
29 1
    public function set($name, $value)
30
    {
31 1
        $this->attributes[$name] = $value;
32 1
    }
33
34
    /**
35
     * Get a document attribute using dot notation.
36
     *
37
     * @param $name
38
     *
39
     * @return mixed
40
     */
41 15
    public function get($name)
42
    {
43 15
        if (array_key_exists($name, $this->attributes)) {
44 12
            return $this->attributes[$name];
45
        }
46
47 4
        $attributes = $this->attributes;
48
49 4 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 4
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
51 1
                $attributes = $attributes[$key];
52 1
            } else {
53 3
                return;
54
            }
55 1
        }
56
57 1
        return $attributes;
58
    }
59
60
    /**
61
     * Check if the document as a given attribute.
62
     *
63
     * @param  $name
64
     *
65
     * @return bool
66
     */
67 2
    public function has($name)
68
    {
69 2
        if (array_key_exists($name, $this->attributes)) {
70 1
            return true;
71
        }
72
73 2
        $attributes = $this->attributes;
74
75 2 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 2
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
77 1
                $attributes = $attributes[$key];
78 1
            } else {
79 2
                return false;
80
            }
81 1
        }
82
83 1
        return true;
84
    }
85
}
86