Conditions | 11 |
Paths | 10 |
Total Lines | 49 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
44 | public function isMatch($a, $b) |
||
45 | { |
||
46 | $percentage = null; |
||
47 | |||
48 | // Strip tags and check similarity |
||
49 | $aStripped = strip_tags($a); |
||
50 | $bStripped = strip_tags($b); |
||
51 | similar_text($aStripped, $bStripped, $percentage); |
||
52 | |||
53 | if ($percentage >= $this->similarityThreshold) { |
||
54 | return true; |
||
55 | } |
||
56 | |||
57 | // Check w/o stripped tags |
||
58 | similar_text($a, $b, $percentage); |
||
59 | if ($percentage >= $this->similarityThreshold) { |
||
60 | return true; |
||
61 | } |
||
62 | |||
63 | // Check common prefix/ suffix length |
||
64 | $aCleaned = trim($aStripped); |
||
65 | $bCleaned = trim($bStripped); |
||
66 | if (strlen($aCleaned) === 0 || strlen($bCleaned) === 0) { |
||
67 | $aCleaned = $a; |
||
68 | $bCleaned = $b; |
||
69 | } |
||
70 | if (strlen($aCleaned) === 0 || strlen($bCleaned) === 0) { |
||
71 | return false; |
||
72 | } |
||
73 | $prefixIndex = Preprocessor::diffCommonPrefix($aCleaned, $bCleaned); |
||
74 | $suffixIndex = Preprocessor::diffCommonSuffix($aCleaned, $bCleaned); |
||
75 | |||
76 | // Use shorter string, and see how much of it is leftover |
||
77 | $len = min(strlen($aCleaned), strlen($bCleaned)); |
||
78 | $remaining = $len - ($prefixIndex + $suffixIndex); |
||
79 | $strLengthPercent = $len / max(strlen($a), strlen($b)); |
||
80 | |||
81 | if ($remaining === 0 && $strLengthPercent > $this->lengthRatioThreshold) { |
||
82 | return true; |
||
83 | } |
||
84 | |||
85 | $percentCommon = ($prefixIndex + $suffixIndex) / $len; |
||
86 | |||
87 | if ($strLengthPercent > 0.1 && $percentCommon > $this->commonTextRatioThreshold) { |
||
|
|||
88 | return true; |
||
89 | } |
||
90 | |||
91 | return false; |
||
92 | } |
||
93 | } |
||
94 |