Completed
Push — master ( 944c9c...280a9a )
by Antonio Carlos
03:20 queued 10s
created

Collection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 151
Duplicated Lines 13.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 20
loc 151
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 15 5
A hydrateDefaultElements() 0 4 1
A where() 0 14 3
A whereLanguage() 0 4 1
A whereISO639_3() 0 4 1
A whereISO4217() 0 4 1
A arrayFinder() 0 14 3
A _whereKey() 10 10 1
A _whereAttribute() 10 10 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 PragmaRX\Countries\Package\Support;
4
5
use Closure;
6
use IlluminateAgnostic\Arr\Support\Arr;
7
use PragmaRX\Coollection\Package\Coollection;
8
9
class Collection extends Coollection
10
{
11
    /**
12
     * Magic call methods.
13
     *
14
     * @param string $name
15
     * @param array $arguments
16
     * @return mixed|static
17
     */
18
    public function __call($name, $arguments)
19
    {
20
        if ($name !== 'where' && starts_with($name, 'where')) {
21
            $name = strtolower(preg_replace('/([A-Z])/', '.$1', lcfirst(substr($name, 5))));
22
            if (\count($arguments) === 2) {
23
                return $this->where($name, $arguments[0], $arguments[1]);
24
            }
25
26
            if (\count($arguments) === 1) {
27
                return $this->where($name, $arguments[0]);
28
            }
29
        }
30
31
        return parent::__call($name, $arguments);
32
    }
33
34
    /**
35
     * Hydrate configured default elements.
36
     *
37
     * @param Collection $countries
38
     * @return Collection
39
     */
40
    public function hydrateDefaultElements($countries)
41
    {
42
        return $countries->hydrate();
43
    }
44
45
    /**
46
     * Where on steroids.
47
     *
48
     * @param string $key
49
     * @param mixed $operator
50
     * @param null $value
51
     * @return static
52
     */
53
    public function where($key, $operator, $value = null)
54
    {
55
        if (\func_num_args() === 2) {
56
            $value = $operator;
57
58
            $operator = '=';
59
        }
60
61
        $countries = method_exists($this, 'where'.ucfirst($key))
62
            ? $this->{'where'.ucfirst($key)}($value)
63
            : parent::where($key, $operator, $value);
64
65
        return $this->hydrateDefaultElements($countries);
66
    }
67
68
    /**
69
     * Where language.
70
     *
71
     * @param $value
72
     * @return static
73
     */
74
    public function whereLanguage($value)
75
    {
76
        return $this->_whereAttribute('languages', $value);
77
    }
78
79
    /**
80
     * Where language using iso code.
81
     *
82
     * @param $value
83
     * @return static
84
     */
85
    public function whereISO639_3($value)
86
    {
87
        return $this->_whereKey('languages', $value);
88
    }
89
90
    /**
91
     * Where currency using ISO code.
92
     *
93
     * @param $value
94
     * @return static
95
     */
96
    public function whereISO4217($value)
97
    {
98
        return $this->_whereAttribute('currency', $value);
99
    }
100
101
    /**
102
     * Where for different attributes.
103
     *
104
     * @param string $propertyName
105
     * @param $find
106
     * @param Closure $finderClosure
107
     * @return static
108
     */
109
    private function arrayFinder(string $propertyName, $find, Closure $finderClosure)
110
    {
111
        return $this->filter(function ($data) use ($find, $propertyName, $finderClosure) {
112
            try {
113
                $attributeValue = $data->{$propertyName};
114
            } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...ttributeValue = null; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
115
                $attributeValue = null;
116
            }
117
118
            return \is_null($attributeValue)
119
                ? null
120
                : $finderClosure($find, $attributeValue, $data);
121
        });
122
    }
123
124
    /**
125
     * Where for keys.
126
     *
127
     * @param string $arrayName
128
     * @param $value
129
     * @return static
130
     */
131 View Code Duplication
    private function _whereKey(string $arrayName, $value)
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...
132
    {
133
        $finderClosure = function ($value, $attributeValue) {
134
            return Arr::has($attributeValue, $value);
135
        };
136
137
        return $this->hydrateDefaultElements(
138
            $this->arrayFinder($arrayName, $value, $finderClosure)
139
        );
140
    }
141
142
    /**
143
     * Where for different attributes.
144
     *
145
     * @param string $arrayName
146
     * @param $value
147
     * @return static
148
     */
149 View Code Duplication
    private function _whereAttribute(string $arrayName, $value)
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...
150
    {
151
        $finderClosure = function ($value, $attributeValue) {
152
            return \in_array($value, $attributeValue->toArray());
153
        };
154
155
        return $this->hydrateDefaultElements(
156
            $this->arrayFinder($arrayName, $value, $finderClosure)
157
        );
158
    }
159
}
160