Conditions | 6 |
Paths | 2 |
Total Lines | 86 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | public static function getPhraseSuggestion($alternativeQuerySuggestions) { |
||
46 | $originalQuery = $alternativeQuerySuggestions[0]['text']; |
||
47 | |||
48 | $highlightsCfg = \Config::inst()->get('Elastica', 'Highlights'); |
||
49 | $preTags = $highlightsCfg['PreTags']; |
||
50 | $postTags = $highlightsCfg['PostTags']; |
||
51 | |||
52 | //Use the first suggested phrase |
||
53 | $options = $alternativeQuerySuggestions[0]['options']; |
||
54 | |||
55 | $resultArray = null; |
||
56 | if (sizeof($options) > 0) { |
||
57 | //take the first suggestion |
||
58 | $suggestedPhrase = $options[0]['text']; |
||
59 | $suggestedPhraseHighlighted = $options[0]['highlighted']; |
||
60 | |||
61 | // now need to fix capitalisation |
||
62 | $originalParts = explode(' ', $originalQuery); |
||
63 | $suggestedParts = explode(' ', $suggestedPhrase); |
||
64 | |||
65 | $markedHighlightedParts = ' '.$suggestedPhraseHighlighted.' '; |
||
66 | $markedHighlightedParts = str_replace(' '.$preTags, ' '.self::$pre_marker, $markedHighlightedParts); |
||
67 | |||
68 | $markedHighlightedParts = str_replace($postTags.' ', self::$post_marker, $markedHighlightedParts); |
||
69 | |||
70 | $markedHighlightedParts = trim($markedHighlightedParts); |
||
71 | $markedHighlightedParts = trim($markedHighlightedParts); |
||
72 | |||
73 | $highlightedParts = preg_split('/\s+/', $markedHighlightedParts); |
||
74 | |||
75 | //Create a mapping of lowercase to uppercase terms |
||
76 | $lowerToUpper = array(); |
||
77 | $lowerToHighlighted = array(); |
||
78 | $ctr = 0; |
||
79 | foreach ($suggestedParts as $lowercaseWord) { |
||
80 | $lowerToUpper[$lowercaseWord] = $originalParts[$ctr]; |
||
81 | $lowerToHighlighted[$lowercaseWord] = $highlightedParts[$ctr]; |
||
82 | $ctr++; |
||
83 | } |
||
84 | |||
85 | $plain = array(); |
||
86 | $highlighted = array(); |
||
87 | foreach ($suggestedParts as $lowercaseWord) { |
||
88 | $possiblyUppercase = $lowerToUpper[$lowercaseWord]; |
||
89 | $possiblyUppercaseHighlighted = $lowerToHighlighted[$lowercaseWord]; |
||
90 | |||
91 | //If the terms are identical other than case, e.g. new => New, then simply swap |
||
92 | if (strtolower($possiblyUppercase) == $lowercaseWord) { |
||
93 | array_push($plain, $possiblyUppercase); |
||
94 | array_push($highlighted, $possiblyUppercase); |
||
95 | } else { |
||
96 | //Need to check capitalisation of terms suggested that are different |
||
97 | |||
98 | $chr = mb_substr ($possiblyUppercase, 0, 1, "UTF-8"); |
||
99 | if (mb_strtolower($chr, "UTF-8") != $chr) { |
||
100 | $upperLowercaseWord = $lowercaseWord; |
||
101 | $upperLowercaseWord[0] = $chr; |
||
102 | |||
103 | //$possiblyUppercaseHighlighted = str_replace($lowercaseWord, $possiblyUppercase, $possiblyUppercaseHighlighted); |
||
104 | $withHighlights = str_replace($lowercaseWord, $upperLowercaseWord, $possiblyUppercaseHighlighted); |
||
105 | |||
106 | $lowercaseWord[0] = $chr; |
||
107 | |||
108 | //str_replace(search, replace, subject) |
||
109 | |||
110 | array_push($plain, $lowercaseWord); |
||
111 | array_push($highlighted, $withHighlights); |
||
112 | } else { |
||
113 | //No need to capitalise, so add suggested word |
||
114 | array_push($plain, $lowercaseWord); |
||
115 | |||
116 | //No need to capitalise, so add suggested highlighted word |
||
117 | array_push($highlighted, $possiblyUppercaseHighlighted); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $highlighted = ' '.implode(' ', $highlighted).' '; |
||
123 | $highlighted = str_replace(self::$pre_marker, ' '.$preTags, $highlighted); |
||
124 | $highlighted = str_replace(self::$post_marker, $postTags.' ', $highlighted); |
||
125 | |||
126 | $resultArray['suggestedQuery'] = implode(' ', $plain); |
||
127 | $resultArray['suggestedQueryHighlighted'] = trim($highlighted); |
||
128 | } |
||
129 | return $resultArray; |
||
130 | } |
||
131 | |||
219 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.