Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

BuilderRules   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 11
Bugs 6 Features 2
Metric Value
wmc 19
c 11
b 6
f 2
lcom 1
cbo 1
dl 9
loc 144
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isRequiredField() 0 4 1
A isAssociativeArr() 0 4 1
A isMultidimensionalArray() 9 9 2
A isCarbon() 0 8 2
A getRequiredFields() 0 9 3
A isReadyArrToFormatInJson() 0 13 3
A isString() 0 8 2
A isNumeric() 0 8 2
A hasRequiredFields() 0 10 3

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 Fenos\Notifynder\Builder;
4
5
use Illuminate\Contracts\Config\Repository;
6
use InvalidArgumentException;
7
use Carbon\Carbon;
8
9
/**
10
 * Class BuilderRules.
11
 *
12
 * Simple trait that define the rules that
13
 * the builder has to match. It required mandatory
14
 * fields listed in the $requiredFields property
15
 */
16
trait BuilderRules
17
{
18
    /**
19
     * @var array
20
     */
21
    private $requiredFields = ['from_id', 'to_id', 'category_id'];
22
23
    /**
24
     * Value has to be a string.
25
     *
26
     * @param $value
27
     * @return bool
28
     */
29
    protected function isString($value)
30
    {
31
        if (! is_string($value)) {
32
            throw new InvalidArgumentException('The value Passed is not a string');
33
        }
34
35
        return true;
36
    }
37
38
    /**
39
     * Value has to be a valid Carbon Instance.
40
     *
41
     * @param $value
42
     * @return bool | InvalidArgumentException
43
     */
44
    protected function isCarbon($value)
45
    {
46
        if ($value instanceof Carbon) {
47
            return true;
48
        }
49
50
        throw new InvalidArgumentException('The value Passed has to be an instance of Carbon\\Carbon');
51
    }
52
53
    /**
54
     * Value has to be numeric.
55
     *
56
     * @param $value
57
     * @return bool
58
     */
59
    protected function isNumeric($value)
60
    {
61
        if (! is_numeric($value)) {
62
            throw new InvalidArgumentException('The value Passed must be a number');
63
        }
64
65
        return true;
66
    }
67
68
    /**
69
     * Returns all required fields including the config ones.
70
     *
71
     * @return array
72
     */
73
    public function getRequiredFields()
74
    {
75
        $customRequiredFields = [];
76
        if (property_exists($this, 'config') && $this->config instanceof Repository) {
77
            $customRequiredFields = $this->config->get('notifynder.additional_fields.required', []);
0 ignored issues
show
Bug introduced by
The property config 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...
78
        }
79
80
        return array_unique($this->requiredFields + $customRequiredFields);
81
    }
82
83
    /**
84
     * Check that the builder has
85
     * the required field to send the
86
     * notifications correctly.
87
     *
88
     * @param $array
89
     * @return bool
90
     */
91
    public function hasRequiredFields($array)
92
    {
93
        foreach ($this->getRequiredFields() as $field) {
94
            if (! array_key_exists($field, $array)) {
95
                return false;
96
            }
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * Check if is a required field.
104
     *
105
     * @param $offset
106
     * @return bool
107
     */
108
    public function isRequiredField($offset)
109
    {
110
        return in_array($offset, $this->getRequiredFields());
111
    }
112
113
    /**
114
     * Check if the array passed is
115
     * multidimensional.
116
     *
117
     * @param $arr
118
     * @return bool
119
     */
120
    protected function isReadyArrToFormatInJson(array $arr)
121
    {
122
        if ($this->isAssociativeArr($arr)) {
123
            return true;
124
        }
125
126
        if (count($arr) > 0) {
127
            $error = "The 'extra' value must to be an associative array";
128
            throw new InvalidArgumentException($error);
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     * @param array $arr
136
     * @return bool
137
     */
138
    protected function isAssociativeArr(array $arr)
139
    {
140
        return array_keys($arr) !== range(0, count($arr) - 1);
141
    }
142
143
    /**
144
     * Check if the array is
145
     * multidimensional.
146
     *
147
     * @param $arr
148
     * @return bool
149
     */
150 View Code Duplication
    public function isMultidimensionalArray($arr)
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...
151
    {
152
        $rv = array_filter($arr, 'is_array');
153
        if (count($rv) > 0) {
154
            return true;
155
        }
156
157
        return false;
158
    }
159
}
160