ElasticSearchForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 4
A enableAutocomplete() 0 4 1
A setButtonText() 0 5 1
1
<?php
2
/**
3
 * Elastica Search Form
4
 */
5
6
use Elastica\Document;
7
use Elastica\Query;
8
use \SilverStripe\Elastica\ResultList;
9
use Elastica\Query\QueryString;
10
11
class ElasticSearchForm extends Form {
12
13
	/**
14
	 * List of types to search for, default (blank) returns all
15
	 * @var string
16
	 */
17
	private $types = '';
0 ignored issues
show
Unused Code introduced by
The property $types is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
19
20
	/**
21
	 *
22
	 * @param Controller $controller
23
	 * @param string $name The name of the form (used in URL addressing)
24
	 * @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized
25
	 *  if fields are added to the form.
26
	 * @param FieldList $actions Optional, defaults to a single field named "Go".
27
	 */
28
	public function __construct($controller, $name, $fields = null, $actions = null) {
29
		$searchText = isset($this->Query) ? $this->Query : '';
0 ignored issues
show
Documentation introduced by
The property Query does not exist on object<ElasticSearchForm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
        $fields = new FieldList(
31
           $tf = new TextField("q", "", $searchText)
32
        );
33
34
35
        $buttonText = _t('SearchPage.SEARCH', 'Search');
36
        $actions = new FieldList(
37
            $fa = new FormAction('submit', $buttonText)
38
        );
39
40
        $this->SubmitButton = $fa;
0 ignored issues
show
Documentation introduced by
The property SubmitButton does not exist on object<ElasticSearchForm>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
42
		if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) {
43
			$fields->push(new HiddenField('searchlocale', 'searchlocale', Translatable::get_current_locale()));
44
		}
45
46
		parent::__construct($controller, $name, $fields, $actions);
47
		$this->setFormMethod('post');
48
		$this->disableSecurityToken();
49
		$this->enableAutocomplete();
50
	}
51
52
53
	public function enableAutocomplete() {
54
		$q = $this->Fields()->fieldByName('q');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $q. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
55
		$q->addExtraClass('autocomplete');
56
	}
57
58
59
	/**
60
	 * Optionally allow templates to override the text of the submit button, perhaps if using a icon font
61
	 * as per the default Simple theme
62
	 *
63
	 * @param string $newButtonText Alternative search text
64
	 */
65
	public function setButtonText($newButtonText) {
66
		$this->actions = new FieldList(
67
            $fa = new FormAction('submit', $newButtonText)
68
        );
69
	}
70
}
71