Completed
Pull Request — master (#5)
by Dan Michael O.
01:19
created

Model   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 21.15 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 22
loc 104
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setData() 0 4 1
A isSearchResult() 0 4 1
A expandToFullRecord() 0 4 1
A getIterator() 0 3 1
A get() 11 11 3
A has() 11 11 3
A __toString() 0 4 1
A __get() 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 Scriptotek\GoogleBooks;
4
5
6
class Model implements \IteratorAggregate
7
{
8
    protected $client;
9
    private $_data;
10
11
    public function __construct(GoogleBooks $client, $data)
12
    {
13
        $this->client = $client;
14
        $this->setData($data);
15
    }
16
17
    /**
18
     * Set data for this model
19
     */
20
    protected function setData(\stdClass $data)
21
    {
22
        $this->_data = $data;
23
    }
24
25
    /**
26
     * Returns true if the model is created from a search result response
27
     * (and thus do not contain all the data of the full record).
28
     *
29
     * @return bool
30
     */
31
    public function isSearchResult()
32
    {
33
        return $this->has('searchInfo');
34
    }
35
36
    /**
37
     * Expand a search result response object to a full record.
38
     */
39
    public function expandToFullRecord()
40
    {
41
        $this->setData($this->client->getUrl($this->get('selfLink')));
42
    }
43
44
    /**
45
     * Special method that allows the object to be iterated over, for example
46
     * with a foreach statement.
47
     */
48
    public function getIterator() {
49
        return new \ArrayIterator($this->_data);
50
    }
51
52
    /**
53
     * Get an item from an array using "dot" notation.
54
     *
55
     * @param  string  $key
56
     * @param  mixed   $default
57
     * @return mixed
58
     */
59 View Code Duplication
    public function get($key, $default = null)
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...
60
    {
61
        $current = $this->_data;
62
        foreach (explode('.', $key) as $segment) {
63
            if (!isset($current->{$segment})) {
64
                return $default;
65
            }
66
            $current = $current->{$segment};
67
        }
68
        return $current;
69
    }
70
71
    /**
72
     * Check if an item or items exist in an array using "dot" notation.
73
     *
74
     * @param  string  $key
75
     * @return mixed
76
     */
77 View Code Duplication
    public function has($key)
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...
78
    {
79
        $current = $this->_data;
80
        foreach (explode('.', $key) as $segment) {
81
            if (!isset($current->{$segment})) {
82
                return false;
83
            }
84
            $current = $current->{$segment};
85
        }
86
        return true;
87
    }
88
89
    /**
90
     * Get a string representation of the object
91
     *
92
     * @return string
93
     */
94
    public function __toString()
95
    {
96
        return json_encode($this);
97
    }
98
99
    /**
100
     * Provide object-like access to the data.
101
     *
102
     * @param  string  $key
103
     * @return mixed
104
     */
105
    public function __get($key)
106
    {
107
        return $this->get($key);
108
    }
109
}
110