Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
19 | class SolrQuery |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $className; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $urlParts = []; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $solrFuzzyBridge; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | private $solrWildcardBridge; |
||
41 | |||
42 | /** |
||
43 | * @var boolean |
||
44 | */ |
||
45 | private $andifyTerms; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $solrMap; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | private $paginationDefaultLimit; |
||
56 | |||
57 | /** |
||
58 | * @var Client |
||
59 | */ |
||
60 | private $solrClient; |
||
61 | |||
62 | /** |
||
63 | * @var RequestStack |
||
64 | */ |
||
65 | private $requestStack; |
||
66 | |||
67 | /** |
||
68 | * if the full search term matches one of these patterns, the whole thing is sent quoted to solr |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $fullTermPatterns = [ |
||
73 | '/^[0-9]+ [0-9\.]{9,}$/i' |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * pattern to match a solr field query |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $fieldQueryPattern = '/(.{2,}):(.+)/i'; |
||
82 | |||
83 | /** |
||
84 | * stuff that does not get andified/quoted/whatever |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | private $queryOperators = [ |
||
89 | 'AND', |
||
90 | 'NOT', |
||
91 | 'OR', |
||
92 | '&&', |
||
93 | '||', |
||
94 | '!' |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * Constructor |
||
99 | * |
||
100 | * @param string $solrUrl url to solr |
||
101 | * @param int $solrFuzzyBridge fuzzy bridge |
||
102 | * @param int $solrWildcardBridge wildcard bridge |
||
103 | * @param boolean $andifyTerms andify terms or not? |
||
104 | * @param array $solrMap solr class field weight map |
||
105 | * @param int $paginationDefaultLimit default pagination limit |
||
106 | * @param Client $solrClient solr client |
||
107 | * @param RequestStack $requestStack request stack |
||
108 | */ |
||
109 | 38 | public function __construct( |
|
130 | |||
131 | /** |
||
132 | * sets the class name to search - last part equates to solr core name |
||
133 | * |
||
134 | * @param string $className class name |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | 38 | public function setClassName($className) |
|
142 | |||
143 | /** |
||
144 | * returns true if solr is configured currently, false otherwise |
||
145 | * |
||
146 | * @return bool if solr is configured |
||
147 | */ |
||
148 | 2 | public function isConfigured() |
|
155 | |||
156 | /** |
||
157 | * executes the search on solr using the rql parsing nodes. |
||
158 | * |
||
159 | * @param SearchNode $node search node |
||
160 | * @param LimitNode|null $limitNode limit node |
||
161 | * |
||
162 | * @return array an array of just record ids (the ids of the matching documents in solr) |
||
163 | */ |
||
164 | 36 | public function query(SearchNode $node, LimitNode $limitNode = null) |
|
201 | |||
202 | /** |
||
203 | * returns the string search term to be used in the solr query |
||
204 | * |
||
205 | * @param SearchNode $node the search node |
||
206 | * |
||
207 | * @return string the composed search query |
||
208 | */ |
||
209 | 36 | private function getSearchTerm(SearchNode $node) |
|
253 | |||
254 | /** |
||
255 | * returns a single term how to search. here we can apply custom logic to the user input string |
||
256 | * |
||
257 | * @param string $term single search term |
||
258 | * |
||
259 | * @return string modified search term |
||
260 | */ |
||
261 | 34 | private function getSingleTerm($term) |
|
304 | |||
305 | /** |
||
306 | * parses the special solr field syntax fieldName:fieldValue, converts int ranges |
||
307 | * |
||
308 | * @param string $fieldQuery the query |
||
309 | * |
||
310 | * @return string solr compatible expression |
||
311 | */ |
||
312 | 8 | private function parseSolrFieldQuery($fieldQuery) |
|
330 | |||
331 | /** |
||
332 | * ORify a single term |
||
333 | * |
||
334 | * @param string $term search term |
||
335 | * @param string $modifier modified |
||
336 | * |
||
337 | * @return string ORified query |
||
338 | */ |
||
339 | 24 | private function doAndNotPrefixSingleTerm($term, $modifier) |
|
356 | |||
357 | /** |
||
358 | * returns the client to use for the current query |
||
359 | * |
||
360 | * @return Client client |
||
361 | */ |
||
362 | 36 | private function getClient() |
|
385 | } |
||
386 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.