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