Total Complexity | 52 |
Total Lines | 200 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like distance often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use distance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class distance extends complexCommonTextSimilarities |
||
16 | { |
||
17 | public static function jaroWinkler($a, $b, $round=2) |
||
18 | { |
||
19 | if (!is_string($a)||!is_string($b)) { |
||
20 | return false; |
||
21 | } |
||
22 | static $distance=array(); |
||
23 | static $previous=array(); |
||
24 | if (array($a,$b)===$previous) { |
||
25 | return $distance; |
||
26 | } |
||
27 | $previous=array($a,$b); |
||
28 | return self::getJWDistance($a, $b, $distance, $round); |
||
29 | } |
||
30 | |||
31 | |||
32 | |||
33 | private static function getJWDistance(&$a, &$b, &$distance, $round) |
||
49 | } |
||
50 | |||
51 | private static function prepareJaroWinkler(&$a, &$b) |
||
63 | } |
||
64 | |||
65 | private static function jwMatches(&$a, &$b, &$transpositions, $Δ) |
||
66 | { |
||
67 | foreach ($a as $ind=>$chr) { |
||
68 | foreach ($b as $index=>$char) { |
||
69 | self::_jwMatches($chr, $char, $index, $ind, $transpositions, $Δ); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | private static function _jwMatches($chr, $char, $index, $ind, &$transpositions, $Δ) |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | public static function hamming($a, $b) |
||
91 | { |
||
92 | if (!is_string($a)||!is_string($b)||(strlen($a)!==strlen($b))) { |
||
93 | return false; |
||
94 | } |
||
95 | static $distance=0; |
||
96 | static $previous=array(); |
||
97 | if (array($a,$b)===$previous) { |
||
98 | return $distance; |
||
99 | } |
||
100 | $previous=array($a,$b); |
||
101 | $a=self::split($a); |
||
102 | $b=self::split($b); |
||
103 | $distance=count(array_diff_assoc($a, $b)); |
||
104 | return $distance; |
||
105 | } |
||
106 | |||
107 | public static function dice($a, $b, $round=2) |
||
108 | { |
||
109 | if (!is_string($a)||!is_string($b)) { |
||
110 | return false; |
||
111 | } |
||
112 | if (empty($a)||empty($b)) { |
||
113 | return 0.0; |
||
114 | } |
||
115 | if ($a===$b) { |
||
116 | return 1.0; |
||
117 | } |
||
118 | |||
119 | static $distance=0; |
||
120 | static $previous=array(); |
||
121 | if (array($a,$b)===$previous) { |
||
122 | return $distance; |
||
123 | } |
||
124 | $previous=array($a,$b); |
||
125 | $a=self::split($a, 2); |
||
126 | $b=self::split($b, 2); |
||
127 | return self::getDiceDistance($distance, $a, $b, $round); |
||
128 | } |
||
129 | |||
130 | private static function getDiceDistance(&$distance, &$a, &$b, $round) |
||
131 | { |
||
132 | $ca=($caGrams=count($a))*2-self::getEndStrLen($a); |
||
133 | $cb=($cbGrams=count($b))*2-self::getEndStrLen($b); |
||
134 | $distance=round(2*count($caGrams>$cbGrams?array_intersect($a, $b):array_intersect($b, $a))/($ca+$cb), $round); |
||
135 | } |
||
136 | |||
137 | private static function getEndStrLen($a) |
||
138 | { |
||
139 | if (function_exists('array_key_last')) { |
||
140 | $end=array_key_last($a); |
||
141 | $end=(isset($end[1]))?0:1; |
||
142 | } else { |
||
143 | $end=end($a); |
||
144 | $end=(isset($end[1]))?0:1; |
||
145 | reset($a); |
||
146 | } |
||
147 | return $end; |
||
148 | } |
||
149 | |||
150 | public static function levenshtein($a, $b) |
||
151 | { |
||
152 | if (!is_string($a)||!is_string($b)) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | |||
157 | static $distance=0; |
||
158 | static $previous=array(); |
||
159 | if (array($a,$b)===$previous) { |
||
160 | return $distance; |
||
161 | } |
||
162 | $previous=array($a,$b); |
||
163 | $a=self::split($a); |
||
164 | $b=self::split($b); |
||
165 | $ca = count($a); |
||
166 | $cb = count($b); |
||
167 | $dis = range(0, $cb); |
||
168 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis); |
||
169 | |||
170 | return $distance=$dis[$cb]; |
||
171 | } |
||
172 | |||
173 | |||
174 | public static function levenshteinDamerau($a, $b) |
||
175 | { |
||
176 | if (!is_string($a)||!is_string($b)) { |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | static $distance=0; |
||
181 | static $previous=array(); |
||
182 | if (array($a,$b)===$previous) { |
||
183 | return $distance; |
||
184 | } |
||
185 | $previous=array($a,$b); |
||
186 | $a=self::split($a); |
||
187 | $b=self::split($b); |
||
188 | $ca = count($a); |
||
189 | $cb = count($b); |
||
190 | $dis = range(0, $cb); |
||
191 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis, true); |
||
192 | |||
193 | return $distance=$dis[$cb]; |
||
194 | } |
||
195 | |||
196 | private static function BuildLevenshteinCostMatrix($a, $b, $ca, $cb, &$dis, $damerau=false) |
||
205 | } |
||
206 | } |
||
207 | |||
208 | private static function costMatrix(&$a, &$b, &$dis_new, &$dis, $damerau, $x, $y) |
||
209 | { |
||
210 | $c = ($a[$x-1] == $b[$y-1])?0:1; |
||
215 | } |
||
216 | } |
||
217 | } |
||
222 |