Completed
Pull Request — master (#16)
by Maksim
01:34
created

ModelConfiguration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 177
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFillable() 0 4 1
A getHidden() 0 4 1
A scopeSelectedUnits() 0 6 2
A newInstance() 0 8 2
A hasSetMutator() 0 8 2
A hasGetMutator() 0 8 3
A mutateAttribute() 0 15 3
A fillableFromArray() 0 4 1
A setMutatedAttributeValue() 0 14 3
B processMultiUnitFieldChanges() 0 27 6
1
<?php
2
3
4
namespace MaksimM\MultiUnitModels\Traits;
5
6
7
use MaksimM\MultiUnitModels\Exceptions\NotSupportedMultiUnitField;
8
9
trait ModelConfiguration
10
{
11
12
    /**
13
     * @return array
14
     */
15
    public function getFillable()
16
    {
17
        return array_merge($this->getUnitConversionDataColumns(), parent::getFillable());
0 ignored issues
show
Bug introduced by
It seems like getUnitConversionDataColumns() 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...
18
    }
19
20
    /**
21
     * @return mixed
22
     */
23
    public function getHidden()
24
    {
25
        return array_merge(parent::getHidden(), $this->getUnitConversionDataColumns());
0 ignored issues
show
Bug introduced by
It seems like getUnitConversionDataColumns() 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...
26
    }
27
28
    public function scopeSelectedUnits($query, $units)
29
    {
30
        foreach ($units as $unitBasedColumn => $unit) {
31
            $query->getModel()->setMultiUnitFieldSelectedUnit($unitBasedColumn, $unit);
32
        }
33
    }
34
35
    /**
36
     * Create a new instance of the given model.
37
     *
38
     * @param  array  $attributes
39
     * @param  bool  $exists
40
     * @return static
41
     * @throws NotSupportedMultiUnitField
42
     */
43
    public function newInstance($attributes = [], $exists = false)
44
    {
45
        $model = parent::newInstance($attributes, $exists);
46
        foreach ($this->getMultiUnitColumns() as $unitBasedColumn => $options) {
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitColumns() 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...
47
            $model->setMultiUnitFieldSelectedUnit($unitBasedColumn, $this->getMultiUnitFieldSelectedUnit($unitBasedColumn)->getId());
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldSelectedUnit() 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...
48
        }
49
        return $model;
50
    }
51
52
    /**
53
     * Determine if a set mutator exists for an attribute.
54
     *
55
     * @param string $key
56
     *
57
     * @return bool
58
     */
59
    public function hasSetMutator($key)
60
    {
61
        if ($this->isMultiUnitField($key)) {
0 ignored issues
show
Bug introduced by
It seems like isMultiUnitField() 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...
62
            return true;
63
        }
64
65
        return parent::hasSetMutator($key);
66
    }
67
68
    /**
69
     * Determine if a get mutator exists for an attribute.
70
     *coo.
71
     *
72
     * @param string $key
73
     *
74
     * @return bool
75
     */
76
    public function hasGetMutator($key)
77
    {
78
        if ($this->isMultiUnitField($key) && isset($this->{$key})) {
0 ignored issues
show
Bug introduced by
It seems like isMultiUnitField() 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...
79
            return true;
80
        }
81
82
        return parent::hasGetMutator($key);
83
    }
84
85
    /**
86
     * Get the value of an attribute using its mutator.
87
     *
88
     * @param string $key
89
     * @param mixed  $value
90
     *
91
     * @throws NotSupportedMultiUnitField
92
     *
93
     * @return mixed
94
     */
95
    public function mutateAttribute($key, $value)
96
    {
97
        if ($this->isMultiUnitField($key)) {
0 ignored issues
show
Bug introduced by
It seems like isMultiUnitField() 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...
98
            $requestedUnit = $this->getMultiUnitFieldUnit($key);
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldUnit() 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...
99
100
            $value = $this->getMultiUnitFieldValue($key, new $requestedUnit());
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldValue() 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...
101
            if (parent::hasGetMutator($key)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasGetMutator() instead of mutateAttribute()). Are you sure this is correct? If so, you might want to change this to $this->hasGetMutator().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
102
                return parent::mutateAttribute($key, $value);
103
            }
104
105
            return $value;
106
        }
107
108
        return parent::mutateAttribute($key, $value);
109
    }
110
111
    /**
112
     * Allows to set input units and process them before multi-unit field.
113
     *
114
     * @param array $attributes
115
     *
116
     * @return array
117
     */
118
    protected function fillableFromArray(array $attributes)
119
    {
120
        return array_merge($attributes, parent::fillableFromArray($attributes));
121
    }
122
123
    /**
124
     * Set the value of an attribute using its mutator.
125
     *
126
     * @param string $key
127
     * @param mixed  $value
128
     *
129
     * @throws NotSupportedMultiUnitField
130
     *
131
     * @return mixed
132
     */
133
    protected function setMutatedAttributeValue($key, $value)
134
    {
135
        if ($this->isMultiUnitField($key)) {
0 ignored issues
show
Bug introduced by
It seems like isMultiUnitField() 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...
136
            $value = $this->processMultiUnitFieldChanges($key, $value);
137
138
            if (parent::hasSetMutator($key)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasSetMutator() instead of setMutatedAttributeValue()). Are you sure this is correct? If so, you might want to change this to $this->hasSetMutator().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
139
                return parent::setMutatedAttributeValue($key, $value);
140
            }
141
142
            return $value;
143
        }
144
145
        parent::setMutatedAttributeValue($key, $value);
146
    }
147
148
    /**
149
     * Detect changes and set proper database value
150
     *
151
     * @param $field
152
     * @param $value
153
     *
154
     * @throws NotSupportedMultiUnitField
155
     *
156
     * @return mixed
157
     */
158
    private function processMultiUnitFieldChanges($field, $value)
159
    {
160
        if(!is_null($value)) {
161
            $existingConversionData = $this->getMultiUnitExistingConversionData($field);
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitExistingConversionData() 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...
162
            if (!is_null($existingConversionData)) {
163
                $inputUnit = $this->getMultiUnitFieldUnit($field);
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldUnit() 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...
164
                //change existing value only in case if new value doesn't match with stored conversion table or not exists
165
                if (!isset(
166
                        $existingConversionData->{$inputUnit->getId()}
167
                    ) || $value != $existingConversionData->{$inputUnit->getId()}) {
168
                    $defaultUnitValue = (new $inputUnit($value))->as($this->getMultiUnitFieldDefaultUnit($field));
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldDefaultUnit() 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...
169
                    $this->attributes[$field] = $defaultUnitValue;
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
170
                } elseif ($value == $existingConversionData->{$inputUnit->getId()}) {
171
                    //forget changes if value actually isn't changed
172
                    $defaultUnitValue = $existingConversionData->{$this->getMultiUnitFieldDefaultUnit($field)->getId()};
0 ignored issues
show
Bug introduced by
It seems like getMultiUnitFieldDefaultUnit() 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...
173
                    $this->attributes[$field] = $defaultUnitValue;
174
                    $this->syncOriginalAttribute($field);
0 ignored issues
show
Bug introduced by
It seems like syncOriginalAttribute() 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...
175
                }
176
177
                return $value;
178
            }
179
        }
180
181
        $this->attributes[$field] = $value;
182
183
        return $value;
184
    }
185
}