Completed
Push — master ( adeed7...b5464f )
by Akpé Aurelle Emmanuel Moïse
02:31
created
tests/Similar_textTest.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -9,11 +9,11 @@
 block discarded – undo
9 9
     {
10 10
         public function testSimilarText()
11 11
         {
12
-            $this->assertTrue(100.0===similarText('qwerty', 'ytrewq'));
13
-            $this->assertTrue(similarText('qwerty', 'ytreq')>=80);
12
+            $this->assertTrue(100.0 === similarText('qwerty', 'ytrewq'));
13
+            $this->assertTrue(similarText('qwerty', 'ytreq') >= 80);
14 14
             $this->assertTrue(similarButNotEqual('qwerty', 'ytrewq'));
15 15
             $this->assertTrue(areAnagrams('qwerty', 'ytrewq'));
16
-            $this->assertTrue(0.0===similarText('qwerty', ';lkjhg'));
16
+            $this->assertTrue(0.0 === similarText('qwerty', ';lkjhg'));
17 17
         }
18 18
     }
19 19
 }
Please login to merge, or discard this patch.
src/similar_text.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -145,7 +145,7 @@
 block discarded – undo
145 145
                     self::split($str)
146 146
             )
147 147
                         )
148
-                       ;
148
+                        ;
149 149
         }
150 150
         
151 151
         protected static function split($str)
Please login to merge, or discard this patch.
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -12,10 +12,10 @@  discard block
 block discarded – undo
12 12
     function SimilarText(
13 13
         $firstString,
14 14
         $secondString,
15
-        $round=2,
16
-        $insensitive=true,
17
-        &$stats=false,
18
-        $getParts=false
15
+        $round = 2,
16
+        $insensitive = true,
17
+        &$stats = false,
18
+        $getParts = false
19 19
                         ) {
20 20
         return EZAMA\similar_text::similarText(
21 21
             $firstString,
@@ -45,27 +45,27 @@  discard block
 block discarded – undo
45 45
         private function __construct()
46 46
         {
47 47
         }
48
-        public static function similarText($a, $b, $round=2, $insensitive=true, &$stats=false, $getParts=false)
48
+        public static function similarText($a, $b, $round = 2, $insensitive = true, &$stats = false, $getParts = false)
49 49
         {
50
-            if (!is_string($a)||!is_string($b)) {
50
+            if (!is_string($a) || !is_string($b)) {
51 51
                 return false;
52 52
             }
53 53
             if ($insensitive) {
54
-                $a=self::strtolower($a);
55
-                $b=self::strtolower($b);
54
+                $a = self::strtolower($a);
55
+                $b = self::strtolower($b);
56 56
             }
57 57
             /* prevent bad types and useless memory usage due to for example array instead of simple boolean */
58 58
             unset($insensitive);
59
-            $getParts=(bool)$getParts;
59
+            $getParts = (bool) $getParts;
60 60
             /*  ******************************************************************************************** */
61
-            $a=self::split($a);
62
-            $b=self::split($b);
63
-            $ca=count($a);
64
-            $cb=count($b);
65
-            if ($ca<$cb) {
66
-                $stats=self::getStats($cb, $a, self::_check($a, $b, $getParts, $round), $getParts, $round);
61
+            $a = self::split($a);
62
+            $b = self::split($b);
63
+            $ca = count($a);
64
+            $cb = count($b);
65
+            if ($ca < $cb) {
66
+                $stats = self::getStats($cb, $a, self::_check($a, $b, $getParts, $round), $getParts, $round);
67 67
             } else {
68
-                $stats=self::getStats($ca, $b, self::_check($b, $a, $getParts, $round), $getParts, $round);
68
+                $stats = self::getStats($ca, $b, self::_check($b, $a, $getParts, $round), $getParts, $round);
69 69
             }
70 70
             return $stats['similar'];
71 71
         }
@@ -73,49 +73,49 @@  discard block
 block discarded – undo
73 73
         protected static function _check($a, $b, $getParts, $round)
74 74
         {
75 75
             if ($getParts) {
76
-                $diff[]=array_diff($a, $b);
77
-                $diff[]=array_diff($b, $a);
76
+                $diff[] = array_diff($a, $b);
77
+                $diff[] = array_diff($b, $a);
78 78
             }
79
-            $diff[]=array_intersect($a, $b);
80
-            $diff[]=round(count(array_intersect(self::getParts($a, $c), self::getParts($b)))/$c*100, $round);
81
-            $diff[]=$a===$b;
79
+            $diff[] = array_intersect($a, $b);
80
+            $diff[] = round(count(array_intersect(self::getParts($a, $c), self::getParts($b))) / $c * 100, $round);
81
+            $diff[] = $a === $b;
82 82
             return $diff;
83 83
         }
84 84
         
85 85
         protected static function getStats($ca, $b, $diff, $getParts, $round)
86 86
         {
87 87
             if ($getParts) {
88
-                $stats['similar']=round(count($diff[2])*100/$ca, $round);
89
-                $stats['substr']=$diff[3];
90
-                $stats['contain']=($diff[2]===$b)?true:false;
91
-                $stats['equal']=$diff[4];
92
-                $stats['a-b']=$diff[0];
93
-                $stats['b-a']=$diff[1];
94
-                $stats['a&b']=$diff[2];
88
+                $stats['similar'] = round(count($diff[2]) * 100 / $ca, $round);
89
+                $stats['substr'] = $diff[3];
90
+                $stats['contain'] = ($diff[2] === $b) ?true:false;
91
+                $stats['equal'] = $diff[4];
92
+                $stats['a-b'] = $diff[0];
93
+                $stats['b-a'] = $diff[1];
94
+                $stats['a&b'] = $diff[2];
95 95
             } else {
96
-                $stats['similar']=round(count($diff[0])*100/$ca, $round);
97
-                $stats['substr']=$diff[1];
98
-                $stats['contain']=($diff[0]===$b)?true:false;
99
-                $stats['equal']=$diff[2];
96
+                $stats['similar'] = round(count($diff[0]) * 100 / $ca, $round);
97
+                $stats['substr'] = $diff[1];
98
+                $stats['contain'] = ($diff[0] === $b) ?true:false;
99
+                $stats['equal'] = $diff[2];
100 100
             }
101 101
             return $stats;
102 102
         }
103 103
 
104
-        protected static function getParts($b, &$c=0)
104
+        protected static function getParts($b, &$c = 0)
105 105
         {
106
-            $tmp='';
107
-            $c=0;
106
+            $tmp = '';
107
+            $c = 0;
108 108
             foreach ($b as $k=>$v) {
109
-                $tmp.=$v;
109
+                $tmp .= $v;
110 110
                 if (ctype_space($v)) {
111
-                    $parts[]=$tmp;
112
-                    $parts[]=$v;
113
-                    $c+=2;
114
-                    $tmp='';
111
+                    $parts[] = $tmp;
112
+                    $parts[] = $v;
113
+                    $c += 2;
114
+                    $tmp = '';
115 115
                 }
116 116
             }
117 117
             if (!empty($tmp)) {
118
-                $parts[]=$tmp;
118
+                $parts[] = $tmp;
119 119
                 $c++;
120 120
             }
121 121
             return $parts;
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
         {
137 137
             return join(
138 138
                 array_map(
139
-                    function ($val) {
139
+                    function($val) {
140 140
                         if (self::is_ascii($val)) {
141 141
                             return strtolower($val);
142 142
                         }
@@ -153,25 +153,25 @@  discard block
 block discarded – undo
153 153
             if (!is_string($str)) {
154 154
                 return false;
155 155
             }
156
-            static $split=[];
157
-            static $old='';
158
-            if ($old===$str) {
156
+            static $split = [];
157
+            static $old = '';
158
+            if ($old === $str) {
159 159
                 return $split;
160 160
             } else {
161
-                $old=$str;
162
-                $split=preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
161
+                $old = $str;
162
+                $split = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
163 163
                 return $split;
164 164
             }
165 165
         }
166 166
         
167 167
         public static function areAnagrams($a, $b)
168 168
         {
169
-            return  self::similarText($a, $b, 2, true, $check)?$check['similar'] === 100.0&&$check['contain']===true:false;
169
+            return  self::similarText($a, $b, 2, true, $check) ? $check['similar'] === 100.0 && $check['contain'] === true : false;
170 170
         }
171 171
         
172 172
         public static function similarButNotEqual($a, $b)
173 173
         {
174
-            return   self::similarText($a, $b, 2, true, $check)&&is_array($check)&&$check['equal']===true?false:true;
174
+            return   self::similarText($a, $b, 2, true, $check) && is_array($check) && $check['equal'] === true ?false:true;
175 175
         }
176 176
     }
177 177
 }
Please login to merge, or discard this patch.