ElasticSearchPage_Validator::php()   C
last analyzed

Complexity

Conditions 10
Paths 48

Size

Total Lines 67
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110
Metric Value
dl 0
loc 67
ccs 0
cts 53
cp 0
rs 6.1506
cc 10
eloc 41
nc 48
nop 1
crap 110

How to fix   Long Method    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
class ElasticSearchPage_Validator extends RequiredFields {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
4
5
	protected $customRequired = array('Name');
6
7
	/**
8
	 * Constructor
9
	 */
10
	public function __construct() {
11
		$required = array('ResultsPerPage', 'Identifier');
12
13
		parent::__construct($required);
14
	}
15
16
	function php($data) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
		parent::php($data);
18
		$valid = true;
0 ignored issues
show
Unused Code introduced by
$valid 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...
19
		//Debug::message("Validating data: " . print_r($data, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
20
		$valid = parent::php($data);
21
		//Debug::message("Returning false, just to check");
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
		//return false;
23
		//
24
25
		if ($data['ClassesToSearch'] == array()) {
26
			$data['ClassesToSearch'] = '';
27
		}
28
		$debug = $data['SiteTreeOnly'];
0 ignored issues
show
Unused Code introduced by
$debug 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...
29
		// Check if any classes to search if site tree only is not ticked
30
		if (!$data['SiteTreeOnly']) {
31
			if (!$data['ClassesToSearch']) {
32
				$valid = false;
33
				$this->validationError("ClassesToSearch",
34
					"Please provide at least one class to search, or select 'Site Tree Only'",
35
					'error'
36
				);
37
			} else {
38
				$toSearch = $data['ClassesToSearch'];
39
				foreach ($toSearch as $clazz) {
40
					try {
41
						$instance = Injector::inst()->create($clazz);
42
						if (!$instance->hasExtension('SilverStripe\Elastica\Searchable')) {
43
							$this->validationError('ClassesToSearch', 'The class '.$clazz.' must have the Searchable extension');
44
						}
45
					} catch (ReflectionException $e) {
46
						$this->validationError("ClassesToSearch",
47
							'The class '.$clazz.' does not exist',
48
							'error'
49
						);
50
					}
51
				}
52
			}
53
		}
54
55
56
		// Check the identifier is unique
57
		$mode = Versioned::get_reading_mode();
58
		$suffix =  '';
59
		if ($mode == 'Stage.Live') {
60
			$suffix = '_Live';
61
		}
62
		$where = 'ElasticSearchPage'.$suffix.'.ID != '.$data['ID']." AND `Identifier` = '".$data['Identifier']."'";
63
		$existing = ElasticSearchPage::get()->where($where)->count();
64
		if ($existing > 0) {
65
			$valid = false;
66
			$this->validationError('Identifier',
67
					'The identifier '.$data['Identifier'].' already exists',
68
					'error'
69
			);
70
		}
71
72
73
		// Check number of results per page >= 1
74
		if ($data['ResultsPerPage'] <= 0) {
75
			$valid = false;
76
			$this->validationError('ResultsPerPage',
77
				'Results per page must be >=1'
78
				,'error'
79
			);
80
		}
81
		return $valid;
82
	}
83
}
84