ElasticSearchPage_Validator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%
Metric Value
wmc 11
lcom 0
cbo 3
dl 0
loc 81
ccs 0
cts 58
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C php() 0 67 10
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