Passed
Push — 1.8 ( 8b2854...6b63e4 )
by Robbie
15:17 queued 11:24
created

CwpSearchResult   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
dl 0
loc 187
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 2 1
A getOriginal() 0 2 1
A getOriginalLink() 0 2 1
A hasField() 0 3 1
A getRSSLink() 0 2 1
A getAtomLink() 0 2 1
A setOriginal() 0 2 1
A __construct() 0 13 4
A getSuggestion() 0 2 1
A getSearchLink() 0 2 1
A hasResults() 0 2 2
A getSuggestionLink() 0 2 1
A getLink() 0 9 3
A getQuery() 0 2 1
1
<?php
2
3
/**
4
 * Container for a set of search results
5
 */
6
class CwpSearchResult extends ViewableData {
7
	
8
	private static $casting = array(
9
		'Original' => 'Text',
10
		'OriginalLink' => 'Text',
11
		'Suggestion' => 'Text',
12
		'SuggestionLink' => 'Text',
13
		'Query' => 'Text',
14
		'SearchLink' => 'Text',
15
		'RSSLink' => 'Text',
16
		'AtomLink' => 'Text',
17
	);
18
	
19
	/**
20
	 * List of results
21
	 *
22
	 * @var PaginatedList
23
	 */
24
	protected $matches;
25
	
26
	/**
27
	 * Search terms for these results
28
	 *
29
	 * @var string 
30
	 */
31
	protected $query;
32
	
33
	/**
34
	 * Suggested search keywords
35
	 * Used when this search has suggested terms, but following suggestions isn't enabled
36
	 *
37
	 * @var string 
38
	 */
39
	protected $suggestion;
40
	
41
	/**
42
	 * Original terms superceded by these result.
43
	 * Used when a prior search had suggested terms, and follow suggestions is enabled.
44
	 *
45
	 * @var type 
46
	 */
47
	protected $original;
48
	
49
	/**
50
	 * Create a new CwpSearchResult
51
	 * 
52
	 * @param string $terms
53
	 * @param ArrayData $results Result from SolrIndex
54
	 */
55
	public function __construct($terms = '', ArrayData $results = null) {
56
		$this->query = $terms;
57
		if($results) {
58
			// Clean up the results.
59
			$matches = $results->Matches;
60
			foreach($matches as $result) {
61
				if(!$result->canView()) {
62
					$matches->remove($result);
0 ignored issues
show
Bug introduced by
The method remove() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
					$matches->/** @scrutinizer ignore-call */ 
63
               remove($result);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
				}
64
			}
65
66
			$this->matches = $matches;
67
			$this->suggestion = $results->SuggestionNice;
68
		}
69
	}
70
	
71
	/**
72
	 * Get search results
73
	 * 
74
	 * @return PaginatedList
75
	 */
76
	public function getResults() {
77
		return $this->matches;
78
	}
79
80
	/**
81
	 * Check if there are found results
82
	 * 
83
	 * @return bool
84
	 */
85
	public function hasResults() {
86
		return $this->matches && $this->matches->exists();
87
	}
88
89
	/**
90
	 * Get search keywords matching these results
91
	 * 
92
	 * @return string
93
	 */
94
	public function getQuery() {
95
		return $this->query;
96
	}
97
98
	/**
99
	 * Get suggested search keywords
100
	 * 
101
	 * @return string
102
	 */
103
	public function getSuggestion() {
104
		return $this->suggestion;
105
	}
106
107
	/**
108
	 * Get original search keywords superceded by these results
109
	 * 
110
	 * @return string
111
	 */
112
	public function getOriginal() {
113
		return $this->original;
114
	}
115
116
	/**
117
	 * Set original keywords
118
	 * 
119
	 * @param string $original
120
	 */
121
	public function setOriginal($original) {
122
		$this->original = $original;
0 ignored issues
show
Documentation Bug introduced by
It seems like $original of type string is incompatible with the declared type type of property $original.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
123
	}
124
125
	/**
126
	 * Get the link to the suggested search
127
	 * 
128
	 * @param string $format Optional output format
129
	 * @return string
130
	 */
131
	public function getSuggestionLink($format = null) {
132
		return $this->getLink($this->suggestion, $format);
133
	}
134
	
135
	/**
136
	 * Gets the link to the given search
137
	 * 
138
	 * @param string $format Optional output format
139
	 * @return string
140
	 */
141
	public function getSearchLink($format = null) {
142
		return $this->getLink($this->query, $format);
143
	}
144
	
145
	/**
146
	 * Gets the link to the original search, with suggestions disabled
147
	 * 
148
	 * @param string $format Optional output format
149
	 * @return string
150
	 */
151
	public function getOriginalLink($format = null) {
152
		return $this->getLink($this->original, $format) . "&suggestions=0";
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getLink($this->original, $format) targeting CwpSearchResult::getLink() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
153
	}
154
	
155
	/**
156
	 * Get link to these results in RSS format
157
	 * 
158
	 * @return string
159
	 */
160
	public function getRSSLink() {
161
		return $this->getLink($this->query, 'rss');
162
	}
163
	
164
	/**
165
	 * Get link to these results in atom format
166
	 * 
167
	 * @return string
168
	 */
169
	public function getAtomLink() {
170
		return $this->getLink($this->query, 'atom');
171
	}
172
	
173
	/**
174
	 * Get a search link for given terms
175
	 * 
176
	 * @param string $terms
177
	 * @return string|null
178
	 */
179
	protected function getLink($terms, $format = null) {
180
		if(!$terms) {
181
			return null;
182
		}
183
		$link = 'search/SearchForm?Search='.rawurlencode($terms);
184
		if($format) {
185
			$link .= '&format='.rawurlencode($format);
186
		}
187
		return $link;
188
	}
189
	
190
	public function hasField($field) {
191
		// Fix customise not detecting custom field getters
192
		return array_key_exists($field, $this->config()->casting);
0 ignored issues
show
Bug introduced by
It seems like $this->config()->casting can also be of type integer and string and boolean and double; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
		return array_key_exists($field, /** @scrutinizer ignore-type */ $this->config()->casting);
Loading history...
193
	}
194
}
195