Test Setup Failed
Push — master ( ab6994...40b3cc )
by Chauncey
02:40
created

NumberProperty::validateMax()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Charcoal\Property;
4
5
use PDO;
6
7
// From 'charcoal-property'
8
use Charcoal\Property\AbstractProperty;
9
10
/**
11
 * Number Property
12
 */
13
class NumberProperty extends AbstractProperty
14
{
15
    /**
16
     * Minimal value.
17
     *
18
     * If null (default), then skip minimum validation (no constraint).
19
     *
20
     * @var mixed|null
21
     */
22
    private $min;
23
24
    /**
25
     * Maximal value.
26
     *
27
     * If null (default), then skip maximum validation (no constrant).
28
     *
29
     * @var mixed|null
30
     */
31
    private $max;
32
33
    /**
34
     * @return string
35
     */
36
    public function type()
37
    {
38
        return 'number';
39
    }
40
41
    /**
42
     * Set the minimal value.
43
     *
44
     * @param mixed|null $min The minimal value.
45
     * @return self
46
     */
47
    public function setMin($min)
48
    {
49
        $this->min = $min;
50
        return $this;
51
    }
52
53
    /**
54
     * Retrieves the minimal value.
55
     *
56
     * @return mixed|null
57
     */
58
    public function min()
59
    {
60
        return $this->min;
61
    }
62
63
    /**
64
     * Set the maximal value.
65
     *
66
     * @param mixed|null $max The maximal value.
67
     * @return self
68
     */
69
    public function setMax($max)
70
    {
71
        $this->max = $max;
72
        return $this;
73
    }
74
75
    /**
76
     * Retrieves the maximal value.
77
     *
78
     * @return mixed|null
79
     */
80
    public function max()
81
    {
82
        return $this->max;
83
    }
84
85
    /**
86
     * The property's default validation methods.
87
     *
88
     * @return string[]
89
     */
90
    public function validationMethods()
91
    {
92
        $parentMethods = parent::validationMethods();
93
94
        return array_merge($parentMethods, [
95
            'max',
96
            'min',
97
        ]);
98
    }
99
100
    /**
101
     * @return boolean
102
     */
103
    public function validateRequired()
104
    {
105
        if ($this['required'] && !is_numeric($this->val())) {
0 ignored issues
show
Deprecated Code introduced by
The method Charcoal\Property\AbstractProperty::val() has been deprecated.

This method has been deprecated.

Loading history...
106
            $this->validator()->error('Value is required.', 'required');
0 ignored issues
show
Unused Code introduced by
The call to ValidatorInterface::error() has too many arguments starting with 'required'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
108
            return false;
109
        }
110
111
        return true;
112
    }
113
114
    /**
115
     * @return boolean
116
     */
117 View Code Duplication
    public function validateMin()
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...
118
    {
119
        $min = $this->min();
120
        if (!$min) {
121
            return true;
122
        }
123
        $valid = ($this->val() >= $min);
0 ignored issues
show
Deprecated Code introduced by
The method Charcoal\Property\AbstractProperty::val() has been deprecated.

This method has been deprecated.

Loading history...
124
        if ($valid === false) {
125
            $this->validator()->error('The number is smaller than the minimum value', 'min');
0 ignored issues
show
Unused Code introduced by
The call to ValidatorInterface::error() has too many arguments starting with 'min'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126
        }
127
        return $valid;
128
    }
129
130
    /**
131
     * @return boolean
132
     */
133 View Code Duplication
    public function validateMax()
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...
134
    {
135
        $max = $this->max();
136
        if (!$max) {
137
            return true;
138
        }
139
        $valid = ($this->val() <= $max);
0 ignored issues
show
Deprecated Code introduced by
The method Charcoal\Property\AbstractProperty::val() has been deprecated.

This method has been deprecated.

Loading history...
140
        if ($valid === false) {
141
            $this->validator()->error('The number is bigger than the maximum value', 'max');
0 ignored issues
show
Unused Code introduced by
The call to ValidatorInterface::error() has too many arguments starting with 'max'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
142
        }
143
        return $valid;
144
    }
145
146
    /**
147
     * Get the SQL type (Storage format)
148
     *
149
     * Stored as `VARCHAR` for max_length under 255 and `TEXT` for other, longer strings
150
     *
151
     * @see StorablePropertyTrait::sqlType()
152
     * @return string The SQL type
153
     */
154
    public function sqlType()
155
    {
156
        // Multiple number are stocked as TEXT because we do not know the maximum length
157
        if ($this['multiple']) {
158
            return 'TEXT';
159
        }
160
161
        return 'DOUBLE';
162
    }
163
164
    /**
165
     * @see StorablePropertyTrait::sqlPdoType()
166
     * @return integer
167
     */
168
    public function sqlPdoType()
169
    {
170
        return PDO::PARAM_STR;
171
    }
172
}
173