Completed
Push — master ( fbf118...df950e )
by Kristijan
05:18 queued 01:57
created

Rules::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kris\LaravelFormBuilder;
4
5
class Rules
6
{
7
8
    /**
9
     * @var array
10
     */
11
    protected $rules;
12
13
    /**
14
     * @var array
15
     */
16
    protected $attributes;
17
18
    /**
19
     * @var array
20
     */
21
    protected $messages;
22
23
    /**
24
     * @param array $rules
25
     * @param array $attributes
26
     * @param array $messages
27
     */
28 9
    public function __construct(array $rules, array $attributes = [], array $messages = []) {
29 9
        $this->rules = $rules;
30 9
        $this->attributes = $attributes;
31 9
        $this->messages = $messages;
32 9
    }
33
34
    /**
35
     * @param array $rules
36
     * @param array $attributes
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
37
     * @param array $messages
0 ignored issues
show
Bug introduced by
There is no parameter named $messages. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
     */
39 9
    public function append($rules) {
40 9
      if (is_array($rules)) {
41
        $rules = static::fromArray($rules);
42
      }
43
44 9
      $this->rules = array_replace_recursive($this->rules, $rules->getRules());
45 9
      $this->attributes = array_replace_recursive($this->attributes, $rules->getAttributes());
46 9
      $this->messages = array_replace_recursive($this->messages, $rules->getMessages());
47
48 9
      return $this;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 9
    public function getRules() {
55 9
      return $this->rules;
56
    }
57
58
    /**
59
     * @return array
60
     */
61 9
    public function getAttributes() {
62 9
      return $this->attributes;
63
    }
64
65
    /**
66
     * @return array
67
     */
68 9
    public function getMessages() {
69 9
      return $this->messages;
70
    }
71
72
    /**
73
     * @param array[] $rules
74
     * @return static
75
     */
76
    static public function fromArray($rules) {
77
        if (!$rules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rules 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...
78
            return new static([]);
79
        }
80
81
        $rules += [
82
            'rules' => [],
83
            'attributes' => [],
84
            'error_messages' => [],
85
        ];
86
87
        return new static($rules['rules'], $rules['attributes'], $rules['error_messages']);
88
    }
89
90
}
91