Completed
Push — master ( d83c65...6847f0 )
by Ítalo
05:59 queued 02:40
created

CommonMutableContainerTrait   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 180
Duplicated Lines 25.56 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.64%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 40
c 6
b 2
f 2
lcom 0
cbo 3
dl 46
loc 180
ccs 53
cts 56
cp 0.9464
rs 8.2608

11 Methods

Rating   Name   Duplication   Size   Complexity  
A values() 0 4 1
A toKeysArray() 0 9 2
A fromArray() 0 13 3
A groupBy() 0 16 4
A indexBy() 0 10 2
A toValuesArray() 13 13 3
A toArray() 13 13 3
A concat() 10 10 3
A replace() 10 10 3
C concatRecurse() 0 24 8
B replaceRecurse() 0 17 8

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CommonMutableContainerTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CommonMutableContainerTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\ArrayList;
6
use Collections\Dictionary;
7
use Collections\Iterable;
8
use Collections\VectorInterface;
9
10
trait CommonMutableContainerTrait
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 1
    public function values()
16
    {
17 1
        return new ArrayList($this);
18
    }
19
20
    /**
21
     * @return array
22
     */
23 2 View Code Duplication
    public function toValuesArray()
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...
24
    {
25 1
        $arr = [];
26 1
        foreach ($this as $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\...nMutableContainerTrait> is not traversable.
Loading history...
27 1
            if ($value instanceof Iterable) {
28
                $arr[] = $value->toArray();
29
            } else {
30 1
                $arr[] = $value;
31
            }
32 1
        }
33
34 2
        return $arr;
35
    }
36
37 3
    public function toKeysArray()
38 3
    {
39 1
        $res = [];
40 1
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\...nMutableContainerTrait> is not traversable.
Loading history...
41 1
            $res[] = $k;
42 1
        }
43
44 1
        return $res;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 17 View Code Duplication
    public function toArray()
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...
51
    {
52 17
        $arr = [];
53 17
        foreach ($this as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\...nMutableContainerTrait> is not traversable.
Loading history...
54 16
            if ($value instanceof Iterable) {
55 6
                $arr[$key] = $value->toArray();
56 6
            } else {
57 16
                $arr[$key] = $value;
58 1
            }
59 17
        }
60
61 17
        return $arr;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public static function fromArray(array $arr)
68
    {
69 1
        $map = new static();
70 1
        foreach ($arr as $k => $v) {
71 1
            if (is_array($v)) {
72 1
                $map[$k] = new static($v);
0 ignored issues
show
Unused Code introduced by
The call to CommonMutableContainerTrait::__construct() has too many arguments starting with $v.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
            } else {
74 1
                $map[$k] = $v;
75
            }
76 1
        }
77
78 1
        return $map;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     * @return $this
84
     */
85 2
    public function groupBy($callback)
86
    {
87 2
        $group = new Dictionary();
88 2
        foreach ($this as $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\...nMutableContainerTrait> is not traversable.
Loading history...
89 2
            $key = $callback($value);
90 2
            if (!$group->containsKey($key)) {
91 2
                $element = $this instanceof VectorInterface ? new static([$value]) : new ArrayList([$value]);
0 ignored issues
show
Unused Code introduced by
The call to CommonMutableContainerTrait::__construct() has too many arguments starting with array($value).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
92 2
                $group->add($key, $element);
93 2
            } else {
94 2
                $value = $group->get($key)->add($value);
95 2
                $group->set($key, $value);
96
            }
97 2
        }
98
99 2
        return $group;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     * @return $this
105
     */
106 2
    public function indexBy($callback)
107
    {
108 2
        $group = new Dictionary();
109 2
        foreach ($this as $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\...nMutableContainerTrait> is not traversable.
Loading history...
110 2
            $key = $callback($value);
111 2
            $group->set($key, $value);
112 2
        }
113
114 2
        return $group;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     * @return $this
120
     */
121 View Code Duplication
    public function concat($iterable)
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...
122
    {
123
        if (!is_array($iterable) && !$iterable instanceof \Traversable) {
124
            throw new \InvalidArgumentException('The items must be an array or Traversable');
125
        }
126
127
        $this->setAll($this->concatRecurse($this, $iterable));
0 ignored issues
show
Bug introduced by
It seems like setAll() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
128
129
        return $this;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     * @return $this
135
     */
136 View Code Duplication
    public function replace($iterable)
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...
137
    {
138
        if (!is_array($iterable) && !$iterable instanceof \Traversable) {
139
            throw new \InvalidArgumentException('The items must be an array or Traversable');
140
        }
141
142
        $this->setAll($this->concatRecurse($this, $iterable));
0 ignored issues
show
Bug introduced by
It seems like setAll() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
143
144
        return $this;
145
    }
146
147
    private function concatRecurse($array, $array1)
148
    {
149
        $merged = $array;
150
151
        foreach ($array1 as $key => & $value) {
152
            $isValid = function ($value) {
153
                return (is_array($value) || $value instanceof \Traversable);
154
            };
155
156
            if (($isValid($value) && isset($merged[$key])) && $isValid($merged[$key])) {
157
                $merged[$key] = $this->concatRecurse($merged[$key], $value);
158
            } else {
159
                if (is_numeric($key)) {
160
                    if (!isset($merged[$value])) {
161
                        $merged[] = $value;
162
                    }
163
                } else {
164
                    $merged[$key] = $value;
165
                }
166
            }
167
        }
168
169
        return $merged;
170
    }
171
172
    private function replaceRecurse($array, $array1)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
173
    {
174
        foreach ($array1 as $key => $value) {
175
            // create new key in $array, if it is empty or not an array
176
            if (!isset($array[$key]) || (isset($array[$key]) && (!is_array($array[$key]) && !$array[$key] instanceof \Traversable))) {
177
                $array[$key] = [];
178
            }
179
180
            // overwrite the value in the base array
181
            if (is_array($value) || $value instanceof \Traversable) {
182
                $value = $this->replaceRecurse($array[$key], $value);
183
            }
184
            $array[$key] = $value;
185
        }
186
187
        return $array;
188
    }
189
}
190