Completed
Push — master ( 3dee16...2bf36e )
by Andy
02:57
created

PopoloCollection   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 138
Duplicated Lines 5.8 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 2
dl 8
loc 138
ccs 56
cts 70
cp 0.8
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A __toString() 0 4 1
A __get() 8 8 2
A getFirst() 0 4 2
B filter() 0 17 5
A get() 0 13 3
A count() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 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 EveryPolitician\EveryPoliticianPopolo\Collections;
4
5
use \EveryPolitician\EveryPoliticianPopolo\Exceptions;
6
7
class PopoloCollection implements \Countable, \ArrayAccess, \Iterator
8
{
9
    protected $properties = [
10
        'first',
11
    ];
12
    public $lookupFromKey;
13
    private $objectClass;
14
    private $objectArr;
15
    private $position;
16
17
    /**
18
     *
19
     */
20 279
    public function __construct($dataArr, $objectClass, $allPopolo)
21
    {
22 279
        $this->position = 0;
23 279
        $this->objectClass = $objectClass;
24 279
        $this->objectArr = [];
25 279
        foreach ($dataArr as $data) {
26 255
            $this->objectArr[] = new $objectClass($data, $allPopolo);
27 186
        }
28 279
        $this->lookupFromKey = [];
29 279
        foreach ($this->objectArr as $o) {
30 255
            $this->lookupFromKey[$o->keyForHash] = $o;
31 186
        }
32 279
    }
33
34
    public function __toString()
35
    {
36
        return '<'.get_class($this).'>';
37
    }
38
39 126 View Code Duplication
    public function __get($prop)
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...
40
    {
41 126
        if (in_array($prop, $this->properties)) {
42 126
            $getter = 'get' . ucfirst($prop);
43 126
            return $this->$getter();
44
        }
45
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
46
    }
47
48 126
    private function getFirst()
49
    {
50 126
        return (count($this->objectArr) > 0) ? $this->objectArr[0] : null;
51
    }
52
53 15
    public function filter($filters)
54
    {
55 15
        $filtered = [];
56 15
        foreach ($this->objectArr as $obj) {
57 12
            $success = true;
58 12
            foreach ($filters as $prop => $value) {
59 12
                if ($obj->$prop !== $value) {
60 9
                    $success = false;
61 10
                    break;
62
                }
63 8
            }
64 12
            if ($success) {
65 10
                $filtered[] = $obj;
66 6
            }
67 10
        }
68 15
        return $filtered;
69
    }
70
71 9
    public function get($filters)
72
    {
73 9
        $matches = $this->filter($filters);
74 9
        $n = count($matches);
75 9
        if ($n == 0) {
76 3
            $msg = "No " . $this->objectClass . " found matching " . json_encode($filters);
77 3
            throw new Exceptions\ObjectDoesNotExistException($msg);
78 6
        } elseif ($n > 1) {
79 3
            $msg = "Multiple " . $this->objectClass . " objects ($n) found matching " . json_encode($filters);
80 3
            throw new Exceptions\MultipleObjectsReturnedException($msg);
81
        }
82 3
        return $matches[0];
83
    }
84
85
    // Countable interface
86 87
    public function count()
87
    {
88 87
        return count($this->objectArr);
89
    }
90
91
    // ArrayAccess interface
92
    public function offsetExists($offset)
93
    {
94
        return array_key_exists($offset, $this->objectArr);
95
    }
96
97
    // ArrayAccess interface
98 126
    public function offsetGet($offset)
99
    {
100 126
        return $this->objectArr[$offset];
101
    }
102
103
    // ArrayAccess interface
104
    public function offsetSet($offset, $value)
105
    {
106
        $this->objectArr[$offset] = $value;
107
    }
108
109
    // ArrayAccess interface
110
    public function offsetUnset($offset)
111
    {
112
        unset($this->objectArr[$offset]);
113
    }
114
115
    // Iterator interface
116 3
    public function current()
117
    {
118 3
        return $this->objectArr[$this->position];
119
    }
120
121
    // Iterator interface
122
    public function key()
123
    {
124
        return $this->position;
125
    }
126
127
    // Iterator interface
128 3
    public function next()
129
    {
130 3
        $this->position += 1;
131 3
    }
132
133
    // Iterator interface
134 3
    public function rewind()
135
    {
136 3
        $this->position = 0;
137 3
    }
138
139
    // Iterator interface
140 3
    public function valid()
141
    {
142 3
        return array_key_exists($this->position, $this->objectArr);
143
    }
144
}
145