Kohana_Jam_Validator_Rule_Count::validate()   B
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 16
nop 3
crap 10

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 defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Jam Validatior Rule
4
 *
5
 * @package    Jam
6
 * @category   Validation
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2011-2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
class Kohana_Jam_Validator_Rule_Count extends Jam_Validator_Rule {
12
13
	public $minimum;
14
15
	public $maximum;
16
17
	public $within;
18
19
	public $is;
20
21 16
	public function validate(Jam_Validated $model, $attribute, $value)
22
	{
23 16
		// Since PHP 7.2 method count() parameter must be an array or an object that implements Countable
24 16
		// https://www.php.net/manual/en/function.count.php#124263
25
		$count = count((array) $value);
26 16
		$params = (array) $this;
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28 2
		if ($this->minimum !== NULL AND ! ($count >= $this->minimum))
29
		{
30
			$model->errors()->add($attribute, 'count_minimum', array(':minimum' => $this->minimum));
31 16
		}
32
33 2
		if ($this->maximum !== NULL AND ! ($count <= $this->maximum))
34
		{
35
			$model->errors()->add($attribute, 'count_maximum', array(':maximum' => $this->maximum));
36 16
		}
37
38 4
		if ($this->within !== NULL AND ! ($count >= $this->within[0] AND $count <= $this->within[1]))
39
		{
40
			$model->errors()->add($attribute, 'count_within', array(':minimum' => $this->within[0], ':maximum' => $this->within[1]));
41 16
		}
42
43 3
		if ($this->is !== NULL AND ! ($count == $this->is))
44
		{
45
			$model->errors()->add($attribute, 'count_is', array(':is' => $this->is));
46
		}
47
48 16
49
50
	}
51
}
52