Completed
Pull Request — master (#6)
by
unknown
10:17
created

Negotiation   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 174
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 15
loc 174
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A negotiate() 0 16 1
A only() 0 12 3
A relations() 0 20 5
A filters() 0 21 5
A orderBy() 0 8 2
A limit() 0 8 2
A groupBy() 7 7 2
A get() 8 8 2

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 MarcusCampos\Dealer;
4
5
use MarcusCampos\Dealer\Parser;
6
use Illuminate\Database\Eloquent\Collection;
7
8
class Negotiation
9
{
10
11
    /**
12
     * @var Parser
13
     */
14
    private $parser;
15
16
    /**
17
     * @var array
18
     */
19
    private $stack;
20
21
    /**
22
     * @var \Illuminate\Database\Eloquent\Model
23
     */
24
    private $model;
25
26
    public function __construct()
27
    {
28
        $this->parser = new Parser();
29
    }
30
31
    /**
32
     * Negotiate
33
     *
34
     * @param string $query
35
     * @return Collection
36
     */
37
    public function negotiate(string $query)
38
    {
39
        $this->stack = $this->parser->parse($query);
40
41
        $namespace = config('dealer.models.namespace') ?? 'App\\';
42
        $this->model = app($namespace.$this->stack['model']);
43
44
        $this->model = $this->relations()
45
            ->filters()
46
            ->limit()
47
            ->groupBy()
48
            ->orderBy()
49
            ->get();
50
51
        return $this->only($this->model, $this->stack['fields']['only']);
52
    }
53
54
    /**
55
     * Return only selected elements
56
     *
57
     * @param Collection $collection
58
     * @param array $fields
59
     * @return Collection
60
     */
61
    private function only(Collection $collection, array $fields)
62
    {
63
        $fields = array_filter($fields);
64
        
65
        if(!$fields || $fields[0] == '*') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
            return $collection;
67
        }
68
69
        return $collection->map(function ($model) use($fields) {
70
            return $model->only($fields);
71
        });
72
    }
73
74
    /**
75
     * Get relations
76
     *
77
     * @return Negotiation
78
     */
79
    private function relations()
80
    {
81
        $relations = [];
82
83
        if (array_key_exists('extends', $this->stack['fields'])) {
84
            foreach ($this->stack['fields']['extends'] as $value) {
85
                $this->stack['fields']['only'][] = $value['name'];
86
87
                if (!$value['args'] || $value['args'][0] == '*') {
88
                    $relations[] = $value['name'];
89
                    continue;
90
                }
91
                $relations[] = $value['name'] . ':' . implode(',', $value['args']);
92
            }
93
        }
94
95
        $this->model = $this->model->with($relations);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->model->with($relations) can also be of type object<Illuminate\Database\Eloquent\Builder>. However, the property $model is declared as type object<Illuminate\Database\Eloquent\Model>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set filters
102
     *
103
     * @return Negotiation
104
     */
105
    private function filters()
106
    {
107
        $relations = [];
0 ignored issues
show
Unused Code introduced by
$relations is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
        if (array_key_exists('filters', $this->stack)) {
110
            if (array_key_exists('extends', $this->stack['filters'])) {
111
                foreach ($this->stack['filters']['extends'] as $value) {
112
                    $methodName = $value['name'];
113
114
                    if (!$value['args']) {
115
                        $this->model = $this->model->$value['name']();
116
                        continue;
117
                    }
118
119
                    $this->model = $this->model->$methodName(...$value['args']);
120
                }
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the sort
129
     *
130
     * @return Negotiation
131
     */
132
    private function orderBy()
133
    {
134
        if (array_key_exists('orderBy', $this->stack)) {
135
            $this->model = $this->model->orderBy($this->stack['orderBy'][0],$this->stack['orderBy'][1]);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set limit
143
     *
144
     * @return Negotiation
145
     */
146
    private function limit()
147
    {
148
        if (array_key_exists('limit', $this->stack)) {
149
            $this->model = $this->model->limit($this->stack['limit']);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set groups
157
     *
158
     * @return Negotiation
159
     */
160 View Code Duplication
    private function groupBy()
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...
161
    {
162
        if (array_key_exists('groupBy', $this->stack)) {
163
            $this->model = $this->model->groupBy($this->stack['groupBy']);
164
        }
165
        return $this;
166
    }
167
168
    /**
169
     * Get data
170
     *
171
     * @return Collection
172
     */
173 View Code Duplication
    private function get()
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...
174
    {
175
        if (array_key_exists('paginate', $this->stack)) {
176
            return $this->model->paginate($this->stack['paginate']);
177
        }
178
179
        return $this->model->get();
180
    }
181
}