Completed
Push — master ( e0f179...77291c )
by Peter
06:24
created

SizeValidator::isValidValueOf()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 19.2573

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 16
cts 24
cp 0.6667
rs 5.1234
cc 13
eloc 24
nc 15
nop 4
crap 19.2573

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Maslosoft\Mangan\Validators\BuiltIn\Base;
4
5
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
6
use Maslosoft\Mangan\Meta\ManganMeta;
7
use Maslosoft\Mangan\Validators\BuiltIn\CountValidator;
8
use Maslosoft\Mangan\Validators\BuiltIn\StringValidator;
9
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
10
use Maslosoft\Mangan\Validators\Traits\Messages;
11
12
/**
13
 * Base class for size validators.
14
 *
15
 * This can be used as a base for validators checking sizes:
16
 *
17
 * * String Length
18
 * * Number of elements
19
 * * File size
20
 *
21
 * Override msg* attributes with custom `Label` annotations to
22
 * provide proper error messages.
23
 *
24
 * @see StringValidator
25
 * @see CountValidator
26
 * @see ValidatorInterface
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
abstract class SizeValidator
30
{
31
32
	use AllowEmpty,
33
	  Messages;
34
35
	/**
36
	 * @var integer maximum length. Defaults to null, meaning no maximum limit.
37
	 */
38
	public $max;
39
40
	/**
41
	 * @var integer minimum length. Defaults to null, meaning no minimum limit.
42
	 */
43
	public $min;
44
45
	/**
46
	 * @var integer exact length. Defaults to null, meaning no exact length limit.
47
	 */
48
	public $is;
49
50
	/**
51
	 * @var string user-defined error message used when the value is too short.
52
	 * @deprecated use `msgTooShort` instead
53
	 */
54
	public $tooShort;
55
56
	/**
57
	 * @var string user-defined error message used when the value is too long.
58
	 * @deprecated use `msgTooLong` instead
59
	 */
60
	public $tooLong;
61
62
	/**
63
	 * @Label('{attribute} is invalid')
64
	 * @var string
65
	 */
66
	public $msgInvalid = '';
67
68
	/**
69
	 * @Label('{attribute} is too small')
70
	 * @var string
71
	 */
72
	public $msgTooShort = '';
73
74
	/**
75
	 * @Label('{attribute} is too large')
76
	 * @var string
77
	 */
78
	public $msgTooLong = '';
79
80
	/**
81
	 * @Label('{attribute} is of the wrong size')
82
	 * @var string
83
	 */
84
	public $msgLength = '';
85
86 6
	protected function isValidValueOf($model, $attribute, $value, $label = '')
87
	{
88 6
		if ($this->allowEmpty && empty($value))
89
		{
90
			return true;
91
		}
92 6
		if (empty($label))
93
		{
94
			$label = ManganMeta::create($model)->field($attribute)->label;
95
		}
96 6
		if (!is_int($value))
97
		{
98
			$this->addError('msgInvalid', ['{attribute}' => $label]);
99
			return false;
100
		}
101
102 6
		if ($this->min !== null && $value < $this->min)
103
		{
104 1
			if ($this->tooShort)
1 ignored issue
show
Deprecated Code introduced by
The property Maslosoft\Mangan\Validat...izeValidator::$tooShort has been deprecated with message: use `msgTooShort` instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
105
			{
106
				$this->addError($this->tooShort, ['{min}' => $this->min, '{attribute}' => $label]);
1 ignored issue
show
Deprecated Code introduced by
The property Maslosoft\Mangan\Validat...izeValidator::$tooShort has been deprecated with message: use `msgTooShort` instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
107
				return false;
108
			}
109 1
			$this->addError('msgTooShort', array('{min}' => $this->min, '{attribute}' => $label));
110 1
			return false;
111
		}
112 5
		if ($this->max !== null && $value > $this->max)
113
		{
114 1
			if ($this->tooLong)
1 ignored issue
show
Deprecated Code introduced by
The property Maslosoft\Mangan\Validat...SizeValidator::$tooLong has been deprecated with message: use `msgTooLong` instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
115
			{
116
				$this->addError($this->tooLong, array('{max}' => $this->max, '{attribute}' => $label));
1 ignored issue
show
Deprecated Code introduced by
The property Maslosoft\Mangan\Validat...SizeValidator::$tooLong has been deprecated with message: use `msgTooLong` instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
117
				return false;
118
			}
119 1
			$this->addError('msgTooLong', array('{max}' => $this->max, '{attribute}' => $label));
120 1
			return false;
121
		}
122 4
		if ($this->is !== null && $value !== $this->is)
123
		{
124 1
			$this->addError('msgLength', array('{length}' => $this->is, '{attribute}' => $label));
125 1
			return false;
126
		}
127 3
		return true;
128
	}
129
130
}
131